我最近了解了 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/