Hangfire, Autofac and WebApi

I recently found out about Hangfire but so far no luck. My project uses autofac, so I added HangFire.1.4.3 and HangFire.Autofac.1.1.0 nuget packages to my project. Following the documentation, I created a Startup class and registered Hangfire there

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

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

      

After that I updated the WebApiConfig and registered my Autofac container with Hangfire

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

      

When I try to get Hangfire running like

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

      

I am getting the following error

System.InvalidOperationException The type MyNamespace.IMyInterface does not contain a method with the signature MyMethod (IObject1) `in Hangfire.Storage.InvocationData.Deserialize () What could be the problem?

+3


source to share


1 answer


The problem lies in the way the fire is handled. It serializes parameters, and in the case of generics, it cannot deserialize it. The workaround creates an intermediary that can be easily serialized / desirialized and run the code you want.



0


source







All Articles