将azure移动服务autofac与nserviebus注册相结合



我正在尝试将azure移动服务的注册和nserviebus注册结合起来。当我试图将IBus注入控制器时,它不起作用。

public class AutofacConfig
{
      public static void Register(HttpConfiguration http, ContainerBuilder container)
      {
            var busConfiguration = new BusConfiguration();
            busConfiguration.UseContainer<AutofacBuilder>(c => c.ExistingLifetimeScope(container.Build()));
            var bus = Bus.Create(busConfiguration);
            bus.Start();
      }
}

我需要给集装箱打电话。Build(),以便将当前容器传递给NServiceBus。但这导致我的WebApiConfig类中的ServiceConfig.Initialize出现错误

public static class WebApiConfig
    {
        public static void Register()
        {
            // Use this class to set configuration options for your mobile service
            ConfigOptions options = new ConfigOptions();
            // Use this class to set WebAPI configuration options
            HttpConfiguration config = ServiceConfig.Initialize(new ConfigBuilder(options, AutofacConfig.Register));

            // To display errors in the browser during development, uncomment the following
            // line. Comment it out again when you deploy your service for production use.
            // config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
            Database.SetInitializer(new nos_devInitializer());
        }
    }

好的,我可以从dependencyresolver中解析ILifetimeScope。在ServiceConfig.Initialize和两个注册配对后调用此方法。

public class NServiceBusConfig
{
    public static void Register(HttpConfiguration config)
    {
        var resolver = (AutofacWebApiDependencyResolver)config.DependencyResolver;
        var scope = resolver.GetService<ILifetimeScope>();
        var busConfiguration = new BusConfiguration();
        busConfiguration.UseContainer<AutofacBuilder>(f => f.ExistingLifetimeScope(scope));
        var bus = Bus.Create(busConfiguration);
        bus.Start();
    }
}

相关内容

  • 没有找到相关文章

最新更新