Hangfire FileNotFoundException: 无法加载文件或程序集DynamicProxyGenAssembly。

3

我在使用HangFire设置后台任务时遇到了问题。虽然任务被成功设置,但是当我打开HangFire仪表板时,却发现以下异常错误

System.IO.FileNotFoundException: 找不到文件或程序集“DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null”或它的某一个依赖项。系统无法找到指定的文件。

所有接口都是使用AutoFac进行依赖注入注册。我在startup.cs中进行如下配置:

我在startup中有如下代码:

  GlobalConfiguration.Configuration.UseSqlServerStorage("Hangfire");
  app.UseHangfireDashboard();
  app.UseHangfireServer();

我有以下代码来安排任务:

我有以下代码来安排任务

 public class ScheduleAppService : IScheduleAppService
  {
    private readonly IRunCommandAppService _runCommandAppService;

    public ScheduleAppService(IRepository<Schedule> repository, IAdHocTemplateRunnerAppService adHocTemplateRunner) : base(repository)
    {
      _adHocTemplateRunner = adHocTemplateRunner;
    }

    public async Task CreateSchedule(ScheduleDto schedule)
    {
      input.Schedule.JobId = BackgroundJob.Schedule(
        () => _runCommandAppService.AddTemplate(
          new Template{ RunId = Guid.NewGuid().ToString(), TemplateId = schedule.Id }), schedule.Start);
    }
}

被调用的代码在这里。
 public class RunCommandAppService  : IRunCommandAppService 
  {
    private readonly IRepository<Template> _templateRepo;

    public RunCommandAppService (IRepository<Template> templateRepo)
    {
      _templateRepo = templateRepo;
    }

    public void AddTemplate(Template input)
    {
      try
      {
        Run(input);
      }
      finally
      {
        SetRunComplate(input.RunId);
      }
    }

我已经按照Hangfire的所有依赖项进行了操作:http://www.nuget.org/packages/HangFire/,但是找不到对应的引用。你确定它不是你的某个服务或存储库的依赖项吗?在启动Hangfire之前,你是否绑定了所有的依赖项? - undefined
谢谢你的评论。当你说绑定时,是指使用autofac进行设置吗?那么如何为hangfire进行设置呢? - undefined
你是在启动 Hangfire 之前还是之后创建 ContainerBuilder? - undefined
在调用hangfire之前,同样的方法在不使用hangfire时运行良好。我会在调用hangfire配置之前检查容器是否已经准备就绪,并且一切都在那里。 - undefined
3个回答

0

我也遇到了同样的问题。但是我尝试将包含BackgroundJob.Enqueue的方法移动到一个vm类中。当我想要调用这个方法时,我会新建一个vm类。然后使用vm来调用这个方法。这样就能成功运行了。我认为原因是某些资源同时被两个对象使用。总之,它可以运行。希望这能帮到你。

我的异常信息如下:

warn: Hangfire.AutomaticRetryAttribute[0]
  Failed to process the job '15': an exception occurred. Retry attempt 3 of 10 will be performed in 00:01:01.
`System.IO.FileNotFoundException: 未能加载文件或程序集“DynamicProxyGenAssembly2,     Version=0.0.0.0, Culture=neutral, PublicKeyToken=null”或它的某一个依赖项。系统找不到指定的文件。
文件名:“DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null”
   在 System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName, ObjectHandleOnStack type)
   在 System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName)
   在 System.Type.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase)
   在 Hangfire.Storage.InvocationData.Deserialize()

=== 预绑定状态信息 ===
日志: DisplayName = DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
 (Fully-specified)
日志: Appbase = file:///D:/Code/Test/bin/Debug/net461/
日志: 初始 PrivatePath = NULL
调用程序集: Hangfire.Core, Version=1.6.19.0, Culture=neutral, PublicKeyToken=null。`
===
日志: 此绑定从 default 加载上下文开始。
日志: 正在使用应用程序配置文件: D:\Code\Test\bin\Debug\net461\ Test.exe.Config
日志: 使用主机配置文件:
日志: 使用 C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config 的计算机配置文件。
日志: 此时没有为引用应用策略(私有、自定义、分部或基于位置的程序集绑定)。
日志: 相同的绑定已出现过,因 hr = 0x80070002 而失败。

0

可能与你的上下文不同,但我认为在这里添加它仍然是值得的:
-Hangfire服务器作为Windows服务运行;
-Hangfire仪表板在ASP.NET MVC 5应用程序之上运行;
-作业实际上正在运行,但仪表板一直显示FileNotFoundException;

解决方案:在仪表板Web应用程序中添加对缺失程序集(即:'DynamicProxyGenAssembly2')的引用。

相同问题由@reggieboyYEAH报告并以相同方式解决。
详细信息请参见:https://github.com/HangfireIO/Hangfire/issues/558


-1
input.Schedule.JobId = BackgroundJob.Schedule<IAdHocTemplateRunnerAppService >(
            x =>
                x.AddTemplate (new Template{ RunId = Guid.NewGuid().ToString(), TemplateId = schedule.Id }), input.Schedule.Start);

4
请提供一些细节,说明您的代码如何解决 OP 的问题。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接