将NServiceBus从2.6升级到3.3后,CommandService开始在IStartableBus.Start()
上抛出System.NullReferenceException: Object reference not set to an instance of an object.
。小型研究表明,Configure.CreateBus()
返回的 UnicastBus 具有 null 作为传输属性值。
Global.asax.cs中有NSB Config:
IBus bus = Configure.With()
.DefineEndpointName(endpointName)
.UnityBuilder(unityContainer)
.AddLogger()
.BinarySerializer()
.MsmqTransport()
.PurgeOnStartup(false)
.IsTransactional(true)
.IsolationLevel(TransactionIsolationLevel)
.TransactionTimeout(TimeSpan.FromMinutes(TransactionTimeout))
.UnicastBus()
.ImpersonateSender(true)
.LoadMessageHandlers()
.MsmqSubscriptionStorage()
.InstallNcqrs()
.CreateBus()
.Start();
public class NcqrsNsbConfigure : NServiceBus.Configure
{
private NsbCommandService _commandService;
private InProcessEventBus _inProcessEventBus;
public static NcqrsNsbConfigure InstallNcqrs(NServiceBus.Configure config)
{
var configNcqrs = new NcqrsNsbConfigure();
configNcqrs.Install(config);
return configNcqrs;
}
public void Install(NServiceBus.Configure config)
{
Builder = config.Builder;
Configurer = config.Configurer;
NcqrsEnvironment.Configure(new NsbEnvironmentConfiguration(Builder));
var compositeBus = new CompositeEventBus();
_inProcessEventBus = new SafeInProcessEventBus();
compositeBus.AddBus(_inProcessEventBus);
compositeBus.AddBus(new NsbEventBusWrapper());
NcqrsEnvironment.SetDefault<IEventBus>(compositeBus);
_commandService = new NsbCommandService();
var safeCommandService = new NSBCommandService(NcqrsEnvironment.Get<IBus>(), _commandService);
config.Configurer.RegisterSingleton(typeof(Ncqrs.Commanding.ServiceModel.ICommandService), safeCommandService);
}
public NcqrsNsbConfigure RegisterExecutor<TCommand>(ICommandExecutor<TCommand> executor) where TCommand : Ncqrs.Commanding.ICommand
{
_commandService.RegisterExecutor(executor);
return this;
}
public NcqrsNsbConfigure RegisterInProcessEventHandler<TEvent>(IEventHandler<TEvent> handler) where TEvent : Ncqrs.Eventing.IEvent
{
_inProcessEventBus.RegisterHandler(handler);
return this;
}
public NcqrsNsbConfigure RegisterInProcessEventHandler(Type eventType, Action<Ncqrs.Eventing.IEvent> handler)
{
_inProcessEventBus.RegisterHandler(eventType, handler);
return this;
}
public NcqrsNsbConfigure RegisterAllInProcessEventHandlers(Assembly asm)
{
_inProcessEventBus.RegisterAllHandlersInAssembly(asm);
return this;
}
}
有没有人遇到过这个问题并找到了解决方案?
使用过NCQRS,但我猜你正在泄漏真正的配置对象。
当你调用.InstallNcqrs()
时(这不应该是一个扩展方法,以使流畅的配置编译吗?),你没有返回传递的NServiceBus.Configure对象,而是返回你自己的NcqrsNsbConfigure对象。当然,您提供了生成器和配置器以匹配传入的配置对象,但也许您正在泄漏 NServiceBus 管道的其余部分现在期望的其他元素。
我会尝试重新排列您的代码,以便您初始化所需的 NCQRS 内容,但随后返回原始配置对象。您没有使用返回的对象来调用任何其他流畅的方法来配置 NCQRS,所以我认为这应该不是问题。