user3541236
user3541236

Reputation: 171

NServiceBus and ApiController

i try to configure my NServiceBus for a WebApi. I've tried this one: https://coderkarl.wordpress.com/2012/03/16/injecting-nservicebus-into-asp-net-webapi/

The Problem is the Syntax has been changed in the newest NServiceBus-Versin. I can't use the Functions for the Configure-Class because they will be removed in further Versions. The new way to configure the Bus is using the BusConfiguration-Class but i have no idea how.

Here is the older Code:

public static Configure ForWebApi(this Configure configure)
{
    // Register our http controller activator with NSB
    configure.Configurer.RegisterSingleton(typeof(IHttpControllerActivator),
        new NSBHttpControllerActivator());

    // Find every http controller class so that we can register it
    var controllers = Configure.TypesToScan
        .Where(t => typeof(IHttpController).IsAssignableFrom(t));

    // Register each http controller class with the NServiceBus container
    foreach (Type type in controllers)
        configure.Configurer.ConfigureComponent(type, ComponentCallModelEnum.Singlecall);

    // Set the WebApi dependency resolver to use our resolver
    GlobalConfiguration.Configuration.ServiceResolver.SetResolver(new NServiceBusResolverAdapter(configure.Builder));

    // Required by the fluent configuration semantics
    return configure;
}

And Application_Start():

 AreaRegistration.RegisterAllAreas();

// Use LocalDB for Entity Framework by default
Database.DefaultConnectionFactory = new SqlConnectionFactory("Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True");

RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);

BundleTable.Bundles.RegisterTemplateBundles();

Configure.WithWeb()
    .DefaultBuilder()
    .ForWebApi()   // <------ here is the line that registers it
    .Log4Net()
    .XmlSerializer()
    .MsmqTransport()
        .IsTransactional(false)
        .PurgeOnStartup(false)
    .UnicastBus()
        .ImpersonateSender(false)
    .CreateBus()
    .Start();

Does someone has managed it for the NServiceBus Version 5?

Upvotes: 0

Views: 1541

Answers (3)

Dennis van der Stelt
Dennis van der Stelt

Reputation: 2178

As wlabaj says, the documentation on the particular website says it all. Almost.

We use AutoFac so we don't need any direct reference to IBus or ISendOnlyBus and therefor we do this

ContainerBuilder builder = new ContainerBuilder();
var container = builder.Build();
configuration.UseContainer<AutofacBuilder>(x => x.ExistingLifetimeScope(container));

What we do in WebAPI and ASP.NET applications is this

NServiceBus.Bus.CreateSendOnly(configuration);

Because it's not a good practice to expect reply messages to come back after sending them.

Upvotes: 1

wlabaj
wlabaj

Reputation: 468

Here you can see 3.0 vs 4.0 vs 5.0 configuration syntax. At the top of the page you have a link to download code samples.

The examples are for ASP .NET though, so you'll need to tweak it slightly for WebAPI. Let me know if you need further help with that.

Upvotes: 1

Alexey Zimarev
Alexey Zimarev

Reputation: 19640

ForWebApi was never a part of NServiceBus, this was an extension method from the sample that was used to configure NServiceBus dependency resolver to instantiate controllers. The way how it was done is shown here.

There is no need to use NServiceBus resolver since it is just a wrapper around another container. By default it uses Autofac, so you can just use Autofac to work for you in the whole application.

Autofac WebAPI integration is properly described in the documentation.

NServiceBus documentation has a page about using your own container.

This is a very well known setup that you can easily implement.

Upvotes: 0

Related Questions