问题
I am using NServiceBus v6, with NServiceBus.Host. I am not able to resolve the messageSession object. What am I missing?
var container2 = new WindsorContainer();
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
log4net.Config.XmlConfigurator.Configure(new FileInfo("logging.config"));
LogManager.Use<Log4NetFactory>();
endpointConfiguration.DefineEndpointName("Strategy.Service");
endpointConfiguration.UseSerialization<JsonSerializer>();
endpointConfiguration.UsePersistence<NHibernatePersistence>();
endpointConfiguration.UseTransport<SqlServerTransport>();
endpointConfiguration.Conventions()
.DefiningCommandsAs(x => x.Namespace != null && x.Namespace.Contains("Commands"))
.DefiningEventsAs(x => x.Namespace != null && x.Namespace.Contains("Events"));
endpointConfiguration.UseContainer<WindsorBuilder>(
customizations: customizations =>
{
customizations.ExistingContainer(container2);
});
endpointConfiguration.SendFailedMessagesTo("error");
endpointConfiguration.AuditProcessedMessagesTo("audit");
endpointConfiguration.DefineCriticalErrorAction(onCriticalError: async context =>
{
_logger.Fatal($"CRITICAL: {context.Error}", context.Exception);
await context.Stop()
.ConfigureAwait(false);
var output =
$"NServiceBus critical error: {Environment.NewLine}{context.Error}{Environment.NewLine}Shutting Down.";
Environment.FailFast(output, context.Exception);
});
var msgSessions = container2.Resolve<IMessageSession>(); // Cannot resolve IMessageSession.
回答1:
IMessageSession
is not registered in the container by NServiceBus. You will get a reference to the IMessageSession
when you start the endpoint:
var endpointInstance = await Endpoint.Start(endpointConfiguration);
IMessageSession messageSession = endpointInstance as IMessageSession;
See https://docs.particular.net/nservicebus/upgrades/5to6/moving-away-from-ibus#dependency-injection for more information about why IMessageSession
is not injected into the container by NServiceBus.
来源:https://stackoverflow.com/questions/40619450/cannot-resolve-imessagesession-with-castlewindsor-in-nservicebus-6-x