.NET Generic Host for .NET Core Handlers

hosting
netcore

(Josh Taylor) #1

Hi All,

Been using NServiceBus for the last couple of months with .NET Core, and it’s been great.

We have a dedicated handler project for our handlers, and this works well.

One thing I don’t like (coming as a non .NET developer) is how it’s done.

We have our Program.cs that uses Autofac, and looks like the following:

namespace Foobar.Handlers
{
    internal class Program
    {
        private static async Task Main()
        {
            var endpointConfiguration = FoobarEndpoint.Setup();

            var builder = new ContainerBuilder();

            IEndpointInstance endpoint = null;
            builder.Register(x => endpoint)
                .As<IEndpointInstance>()
                .SingleInstance();

            var fooClient = new FooClient();
            builder.RegisterInstance<IFooClient>(fooClient);

            var container = builder.Build();

            endpointConfiguration.UseContainer<AutofacBuilder>(customizations =>
            {
                customizations.ExistingLifetimeScope(container);
            });

            endpoint = await Endpoint.Start(endpointConfiguration)
                .ConfigureAwait(false);
            Thread.Sleep(Timeout.Infinite);
            Log.CloseAndFlush();
        }
    }
}

This is just a simple example, but it works quite well. The concerns I have about this are to do with teardown, but it’s not much of an issue now (in development) and will add teardown as needed.

However, we recently started trialing .NET Generic Host for another console app, and it’s been working well.

That’s when I started to do some digging into using this, as I believe it could be quite a good solution for hosting endpoints, as it has features like logging, configuration, DI, start/stop handlers/etc.

Has anyone used this, or have any recommendations?


(Daniel Marbach) #2

Hi Josh,
Sorry for the late reply. We are actively tracking the Generic Host and think it is a promising piece of technology. Depending on how it gets adopted we might even be evaluating it later as one of our default hosting choices. That being said right now it is a bit too early to jump to conclusions from our side.

But as you said it is a great choice for hosting endpoints. You can use the IHostedService abstraction to start and stop endpoints or even come up with generic option approaches for multi-endpoint hosting.

Regards
Daniel