asp.net-web-api - Hangfire、Autofac 和 WebApi

标签 asp.net-web-api autofac hangfire

我最近了解了 Hangfire,但到目前为止还没有成功。我的项目使用 autofac,因此我已将 HangFire.1.4.3 和 HangFire.Autofac.1.1.0 nuget 包添加到我的项目中。随后是我创建了 Startup 类并在那里注册了 Hangfire 的文档

public void Configuration(IAppBuilder app)
{
      GlobalConfiguration.Configuration
                    .UseSqlServerStorage("NavigatorConnectionString");

       app.UseHangfireDashboard();
       app.UseHangfireServer();
}

之后我更新了 WebApiConfig 并在 Hangfire 中注册了我的 Autofac 容器

private static void RegisterDependencies(HttpConfiguration config)
{
     var builder = new ContainerBuilder();
     ...
     var container = builder.Build();
     ...
     config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
     Hangfire.GlobalConfiguration.Configuration.UseAutofacActivator(container);
}

当我尝试像这样运行 Hangfire 作业时

IObject someObject = MyObject();    
var jobId = BackgroundJob.Enqueue<IMyInterface>(x
                    => x.MyMethod(someObject));

出现以下错误

System.InvalidOperationException MyNamespace.IMyInterface 类型不包含签名为 MyMethod(IObject1) 的方法 在 Hangfire.Storage.InvocationData.Deserialize() 可能是什么问题?

最佳答案

问题在于 hangfire 的工作方式。它序列化参数,如果泛型无法将其反序列化。解决方法是创建一个可以轻松序列化/反序列化并运行您需要的代码的中介。

关于asp.net-web-api - Hangfire、Autofac 和 WebApi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31285996/

相关文章:

jquery - 如何在Web API Controller 上返回Json对象

.net - Autofac:发布/订阅扩展

c# - Bot Framework Autofac DI - 使用 context.Call() 时传递依赖项

c# - 自己的服务 - 没有注册类型的服务

c# - Hangfire.Autofac 与 MVC 应用程序 - 注入(inject)失败

c# - 将 WebAPI 函数限制为授权用户?

asp.net-mvc-4 - 带有外部数据和 TypeScript : compile error "Cannot set property ' gridDim' of undefined"的 Ng-grid

jquery - 是否应该使用网络服务来创建整个网站?

.NET Core 2.0 Autofac 注册类 InstancePerRequest 不起作用

c# - 在策略设计模式的情况下,Hangfire 服务器无法选择工作