【问题标题】:What is the correct way of injecting the bus if using owinselfhost?如果使用 owinselfhost,注入总线的正确方法是什么?
【发布时间】:2015-04-14 21:29:47
【问题描述】:

我有一个发送 nservicebus 消息的 asp.net web api 控制器,即 Bus.Send()。这个 api 控制器是使用 owinselfhost 包自行托管的。

如果我使用 owinselfhost,注入总线的正确方法是什么?

--编辑--

这是代码.. 我现在使用 autofac.. 拥有这些仍然会给我在总线上的 null ref 异常...

参考:

http://docs.autofac.org/en/latest/integration/webapi.html#owin-integration

http://docs.particular.net/nservicebus/containers/

--编辑--

public void Configuration(IAppBuilder app)
    {
        var builder = new ContainerBuilder();
        //this two controllers are from two separate class libraries.
        builder.RegisterApiControllers(typeof(Test1Controller).Assembly);
        builder.RegisterApiControllers(typeof(Test2Controller).Assembly);

        var config = new HttpConfiguration();
        config.MapHttpAttributeRoutes();

        var container = builder.Build();
        config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

        Configure.With()
            .UsingContainer<AutofacObjectBuilder>()
            .UnicastBus()
            .SendOnly();

        app.UseAutofacMiddleware(container);
        app.UseAutofacWebApi(config);
        app.UseWebApi(config);
    }

【问题讨论】:

  • 您使用的是什么容器?我用 Autofac 做到这一点没有任何问题,使用 Autofac 模块用于 Owin 和 WebAPI 完全没有问题。
  • 你能分享一下这是如何使用 autofac 完成的吗? TIA
  • 创建一个容器,根据您使用的功能和 Autofac 文档为您的 Web 框架进行所有注册,然后按照文档中的说明使用带有 NServiceBus 的 Autofac 容器。我不知道你用的是什么,Owin 只是 web 服务器和 web 框架之间的一层,我不知道你用的是什么 web 框架,注册是特定于框架的。
  • @AlexeyZimarev 见上面修改过的问题..

标签: asp.net asp.net-web-api nservicebus owin self-hosting


【解决方案1】:

你要求 NServiceBus 使用 Autofac,但你没有给它你的容器。如果你在那里传递你的容器,你将把总线注入你的控制器。

Configure.With()
        .UsingContainer<AutofacObjectBuilder>(container)
        .UnicastBus()
        .SendOnly();

【讨论】:

  • 感谢您的帮助.. 我之前做过但没有用.. 我在 nsb 4.6.1 上并想办法更新到 4.7.5.. 位更新后一切都对我有用现在..再次感谢您的帮助..
猜你喜欢
  • 2013-11-06
  • 1970-01-01
  • 1970-01-01
  • 2019-07-09
  • 2015-08-10
  • 1970-01-01
  • 2012-04-16
  • 1970-01-01
  • 2020-01-21
相关资源
最近更新 更多