1

I'm building a .NET Core console app that uses Simple Injector and SimpleRabbit to pubish messages to RabbitMQ.

I've run into a run time exception when I attempt to get an registered instance from a container. For guidance, I've been trying to follow the instructions in this link: SimpleInjector integration documentation

The code snippet below outlines what I'm trying to do between using a HostBuilder, configuring and registering the required services.

Main:

public static async Task Main(string[] args)
{
    var configurationRoot = new ConfigurationBuilder()
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .Build();

    // Initialize the SimpleInjector container
    var searchJobStatus = new SearchJobTrackerModel();
    Bootstrap.ConfigureServices(configurationRoot);
    var container = Bootstrap.container;

    var builder = new HostBuilder()
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
            config.AddJsonFile(
                "appsettings.json", optional: true, reloadOnChange: true);
        })
        .ConfigureServices((context, services) =>
        {
            services.AddSimpleInjector(container, options => { });
            services.AddRabbitConfiguration(
                    configurationRoot.GetSection("RabbitConfiguration"))
                .AddSubscriberConfiguration(
                    configurationRoot.GetSection("Subscribers"))
                .AddPublisherServices()
                .AddSubscriberServices()
                .AddSingletonMessageHandler<DiscoverySubscriberService>()
                .AddSingleton<IQueueManagementService, QueueManagementService>()
                .AddSingleton<IHostedService, DiscoveryConsoleService>();
        });

    await builder.RunConsoleAsync();
}

Bootstrap:

public class Bootstrap
{
    public static Container container;

    public static void ConfigureServices(IConfigurationRoot configurationRoot)
    {
        container = new SimpleInjector.Container();
        container.Options.ResolveUnregisteredConcreteTypes = false;
        container.Options.EnableAutoVerification = false;

        RabbitConfiguration rabbitConfiguration = new RabbitConfiguration();
        configurationRoot.GetSection("RabbitConfiguration").Bind(rabbitConfiguration);
        container.ConfigureServices(rabbitConfiguration);
    }
}

public static class ServicesInstaller
{
    public static void ConfigureServices(
        this Container container, RabbitConfiguration rabbitConfiguration)
    {
        container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();
        container.Options.ResolveUnregisteredConcreteTypes = false; 
       
        // Rabbit Configuration
        container.RegisterInstance<RabbitConfiguration>(rabbitConfiguration);
        container.RegisterSingleton<IMessagePublisher, MessagePublisher>();
    }
}

Publisher and mananager:

public class MessagePublisher : IMessagePublisher
{
    IPublishService publisher;
    public IMessagePublisher(IPublishService publisher)
    {
        this.publisher = publisher;
    }
}

public class Manager : IManager
{
    public Manager(IMessagePublisher publisher)
    {
    }
}

I attempt to get an IManager instance:

var manager = Container.GetInstance<IManager>();

I get the following runtime exception:

SimpleInjector.ActivationException HResult=0x80131500 Message=Error resolving the cross-wired IPublishService. You are trying to resolve a cross-wired service, but are doing so outside the context of an active (Async Scoped) scope. To be able to resolve this service the operation must run in the context of such scope. Please see https://simpleinjector.org/scoped for more information about how to manage scopes.

I think the issue is between the .AddPublisherServices() to add publisher services to my service collection and the constructor on the Manager class:

public Manager(IMessagePublisher publisher)

That is far as I've been able to narrow it down. Is there an error in the way I'm building the container and adding/registering services? Is AddPublisherServices using a scope that conflicts with the services defined in my container?

0

1 Answer 1

1

The exception states:

You are trying to resolve a ... service ... outside the context of an ... Async ... scope.

In other words, wrap the operation in a call to using (AsyncScopedLifestyle.BeginScope(container). For instance:

await using (AsyncScopedLifestyle.BeginScope(container))
{
    var manager = container.GetInstance<IManager>();

    await manager.DoSomethingWithMessages(myMessage);
}

For more information see the documentation.

1
  • I am not calling AsyncScopedLifestyle.BeginScope() explicitly in any of my code. The exception is getting thrown when I'm getting an instance of the interface; which of course forces constructor call calls on included dependencies.
    – JohnB
    May 18, 2021 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.