无法在NServiceBus - 6.x中使用CastleWindsor解析IMessageSession

时间:2016-11-15 20:50:43

标签: nservicebus

我正在使用NServiceBus v6和NServiceBus.Host。我无法解析messageSession对象。我错过了什么?

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 个答案:

答案 0 :(得分:3)

NServiceBus未在容器中注册

IMessageSession。启动端点时,您将获得IMessageSession的引用:

var endpointInstance = await Endpoint.Start(endpointConfiguration);
IMessageSession messageSession = endpointInstance as IMessageSession;

有关为什么IMessageSession未被NServiceBus注入容器的详细信息,请参阅https://docs.particular.net/nservicebus/upgrades/5to6/moving-away-from-ibus#dependency-injection