-->

Configuring NServiceBus with ASP.NET MVC4 and Stru

2019-09-03 10:41发布

问题:

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!