Configuring NServiceBus with ASP.NET MVC4 and StructureMap

断了今生、忘了曾经 提交于 2019-12-11 18:39:52

问题


I created an ASP.NET MVC 4 web application by installing structuremap and structuremap.mvc4 from nuget. I then added the nservicebus and nservicebus.structuremap packages, also from from nuget.

I've created a couple mvc4 apps with structuremap before with no problems, and I've followed a few tutorials for basic pub/sub with nservicebus and structuremap and got it to work.

However, putting nservicebus into mvc4 with structuremap doesn't work very well when I tried. The problem I'm encountering is that the nservice bus tutorials for mvc and DI with MVC listed below seem to want the developers to use the built in Dependency Injection container.

http://support.nservicebus.com/customer/portal/articles/894008
http://support.nservicebus.com/customer/portal/articles/894123

If you've used structure map before, the nuget package nicely adds the resolver and runs initial setup using WebActivator. After that, I'm not sure what happens to the container, so I can't use it to set the NServiceBus Builder in Configure.With().StructureMapBuilder(StructureMap Container).

What do I have to do in order to make nservicebus and structuremap play nice in asp.net MVC4? I know its operator error, but I'm not sure what to do at this point? Also, doesn't version 3 have a messageendpointconfig somewhere with some nifty configuration roles? Does that not apply in a web environment? I figured I could just get a ref to the container and go, but that doesn't seem to be the case.

EDIT: added some code

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        AuthConfig.RegisterAuth();

        Configure.With()
            .StructureMapBuilder(ObjectFactory.Container) //this obviously won't work
            .JsonSerializer()
            .Log4Net()
            .MsmqTransport()
                .IsTransactional(false)
                .PurgeOnStartup(true)
            .UnicastBus()
                .ImpersonateSender(false)
            .CreateBus()
            .Start(() => Configure.Instance.ForInstallationOn<NServiceBus.Installation.Environments.Windows>().Install());

    }

EDIT 2: Even if I get past the issue with StructureMap by moving where I set the dependency resolver to Application_Start(), NServiceBus still wants configuration for MessageForwardingInCaseOfFaultConfig and MsmqTransportConfig.

I think I remember that those properties are supposed to be automatically configured by the roles set through the interfaces on the endpoint config?


回答1:


You need to add these sections to your web.config file:

<configuration>
  <configSections>
    <section name="MessageForwardingInCaseOfFaultConfig" type="NServiceBus.Config.MessageForwardingInCaseOfFaultConfig, NServiceBus.Core" />
    <section name="MsmqTransportConfig" type="NServiceBus.Config.MsmqTransportConfig, NServiceBus.Core"/>
  </configSections>

  <MessageForwardingInCaseOfFaultConfig ErrorQueue="error"/>
  <MsmqTransportConfig NumberOfWorkerThreads="1" MaxRetries="5"/>
</configuration>

NServiceBus v3 does not create these automatically!



来源:https://stackoverflow.com/questions/15486503/configuring-nservicebus-with-asp-net-mvc4-and-structuremap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!