Uniform Session not working with MSDependency Injection [Solved]

Uniform Session throws ‘InvalidOperationException: Unable to resolve service for type ‘NServiceBus.UniformSession.IUniformSession’ while attempting to activate ‘WebApplication.Core.Controllers.DefaultController’.’

I am using the following sample : https://docs.particular.net/samples/web/send-from-mvc-controller/

I am declaring IUniformSession session as a parameter in the default controller.

I have replaced the existing Configure Services with :

public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();

        services.AddSingleton(o => endpoint);
        
        var endpointConfiguration = new EndpointConfiguration("Samples.MvcInjection.WebApplication");
        endpointConfiguration.UsePersistence<LearningPersistence>();
        endpointConfiguration.UseTransport<LearningTransport>();
        endpointConfiguration.EnableUniformSession();
        endpointConfiguration.UseContainer<ServicesBuilder>(customizations => { customizations.ExistingServices(services); });


        endpoint = Endpoint.Start(endpointConfiguration).GetAwaiter().GetResult();
    }

When I use the existing configure services (using Autofac) it works fine.

Edit: By looking at Support for Microsoft.Extensions.DependencyInjection? I was able to solve the problem by returning the container as follows:

public IServiceProvider ConfigureServices(IServiceCollection services)
    {

        services.AddMvc();

        IEndpointInstance endpointInstance = null;
        services.AddSingleton<IMessageSession>(_ => endpointInstance);

        var endpointConfiguration = new EndpointConfiguration("Samples.MvcInjection.WebApplication");
        endpointConfiguration.UsePersistence<LearningPersistence>();
        endpointConfiguration.UseTransport<LearningTransport>();
        endpointConfiguration.EnableUniformSession();
        endpointConfiguration.UseContainer<ServicesBuilder>(customizations => { customizations.ExistingServices(services); });


        UpdateableServiceProvider container = null;
        endpointConfiguration.UseContainer<ServicesBuilder>(c =>
        {
            c.ExistingServices(services);
            c.ServiceProviderFactory(sc =>
            {
                container = new UpdateableServiceProvider(sc);
                return container;
            });
        });

        endpointInstance = Endpoint.Start(endpointConfiguration).GetAwaiter().GetResult();

        return container;
    }
1 Like