How to use/add nservicebus endpoint from net core startup class

hi,
the project is a core 3.1 web app that relies heavily on plugins loaded at runtime.
i am trying to build a plugin that has his own startup class and needs to publish/send messages when some events are fired.
as per the docs and samples, the only way to do that is to instantiate a host in the programs.cs\main entrypoint which is in the main application. this would break the plugin architecture we use.

is there a way to add nservicebus and create the endpoints, all from startup class ?

thank you.

Hi @scharada

For .NET Core 3.1 and ASP.NET Core 3.1, please use the NServiceBus.Extensions.Hosting plugin which integrates with the generic host. You can find the documentation and samples here: NServiceBus.Extensions.Hosting • Particular Docs

@Tim thanks for your reply. i’ve been looking at that but it still does not solve the case .
as per the doc and the sample using the generic host or using a container, it looks like i still have to call
.UseNServiceBus from the hostbuilder method in programs.cs. that’s exactly what i don’t want to do , unless i am missing something .
i’d like to be able to instantiate nservicebus, from a startup class that resides in a plugin and called at runtime. nservicebus is not even referenced int eh main app , just in the plugin…
the plugin’s role is to listen for events in the main app and fire messages a different queues. on the other side, several services will process the messages.
any help on how to achieve this is really appreciated.

to be able to integrate with the generic host, NServiceBus needs access to the host builder to register itself as a hosted service.

How do you currently load your other plugins?

@Tim

that’s exactly the issue here. it seems like if this is not done, there is no way to add nservicebus to an already built host and be able to send/publish messages , hence my question …

my plugins are net core libraries that have a startup class each. when the plugin is installed, the main app detects if a startup class exists and f it does it executes “configureServices and Configure” after which, if a ConfigureContainer(ContainerBuilder builder) method exists, it gets called last.

@Tim
if this can help , what i am looking for is a way to do something like this:

public void ConfigureServices(IServiceCollection services)
{
    services.**AddNServiceBus**(context =>
    {
        var endpointConfiguration = new EndpointConfiguration("Samples.ASPNETCore.Sender");
        var transport = endpointConfiguration.UseTransport<RabbitMQTransport>();
        transport.Routing().RouteToEndpoint(
            assembly: typeof(MyMessage).Assembly,
            destination: "Samples.ASPNETCore.Endpoint");

        endpointConfiguration.SendOnly();

    })

An NServiceBus endpoint can’t solely be configured by ConfigureServices as it also needs access to the IServiceProvider managed by the generic host.
It looks like you already built your own mechanism to detect other assemblies and invoke them, can’t you change that logic to pass the IHostBuilder instead of only support IServiceCollection and ContainerBuilder based integration? At this point, this should only be a different value from the generic hosting infrastructure and all that is need to configure an NServiceBus endpoint (note that only one NServiceBus endpoint per host process is supported at the moment).

another overload on the startup would probably do it. i’ll give it a shot .

Just as an FYI. With ASP.NET Core you don’t necessarily need to stick to the startup class concept. You can use the host builder and configure everything there without needing a startup class.

@danielmarbach true and i know that, except you missed the plugin part. the main app has no dependency to nservicebus at all and is not even aware of it. only the plugin does and fires messages based on captured events triggered by other services. so i must keep my startup classes . that’s how i register required services in each plugin.