【问题标题】:Asp.net web api with autofac and Hangfire带有 autofac 和 Hangfire 的 Asp.net web api
【发布时间】:2015-05-06 23:10:36
【问题描述】:

我最近升级到了新版本的 Hangfire,我正在努力尝试使用 autofac 和 Hangfire 设置我的 webapi。我正在使用 Autofac Hangfire 集成版本 1.1 和 Hangfire 1.4.2。我正在使用 Owin 托管。我不断收到以下错误:

请求的服务“IFoo”尚未注册。为避免此异常,请注册一个组件以提供服务,使用 IsRegistered() 检查服务注册,或使用 ResolveOptional() 方法解决可选依赖项。

这是我自己的启动配置。我所有的注册都是在 AutofacStandardModule 类中进行的

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        //we will have the firewall block all CE endpoints from the outside instead
        //ConfigureOAuthTokenConsumption(app);

        var storage = new SqlServerStorage("connection string");

        JobStorage.Current = storage;

        app.UseHangfireServer(new BackgroundJobServerOptions(),storage);
        app.UseHangfireDashboard("/Hangfire",new DashboardOptions(),storage);
        var builder = new ContainerBuilder();
        builder.RegisterModule(new AutofacStandardModule()); 
        var container = builder.Build();

        GlobalConfiguration.Configuration.UseAutofacActivator(container);
        }
}

另外,这是我的 web api 配置类。不过,我也看不出我应该如何在这里配置 Hangfire..

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config, Autofac.Module moduleToAppend)
    {
        config.MapHttpAttributeRoutes();

        config.EnableCors();
        config.EnableSystemDiagnosticsTracing();

        var builder = new ContainerBuilder(); 

        builder.RegisterAssemblyTypes(
            Assembly.GetExecutingAssembly())
                .Where(t =>
                    !t.IsAbstract && typeof(ApiController).IsAssignableFrom(t))
                .InstancePerLifetimeScope();

        builder.RegisterModule(
            new AutofacStandardModule()); 

        if (moduleToAppend != null)
        {
            builder.RegisterModule(moduleToAppend);
        }

        var container = builder.Build();

        config.DependencyResolver = new AutofacWebApiDependencyResolver(
            container);

        //Hangfire.GlobalConfiguration.Configuration.UseAutofacActivator(container);

        //JobActivator.Current = new AutofacJobActivator(container);
        }
}

【问题讨论】:

    标签: autofac owin hangfire


    【解决方案1】:

    我解决了这个问题,我在排队时似乎没有足够清楚地指定我的工作是哪种类型。

    所做的是改变

    _jobClient.Enqueue(
                () => _foo.Bar(fooId, fooId2));
    

    ..进入..

    _jobClient.Enqueue<IFoo>(x => x.Bar(fooId, fooId2));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多