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?