创建NServiceBus总线时出现“对象引用未设置”错误

时间:2012-11-05 22:34:42

标签: nservicebus

我在制作公交车时遇到了问题。当我从传递到With()函数(“MyAssembly.LetterProcessor”)的集合中删除最后一个程序集时,它在本节中没有中断,尽管它稍后会中断。错误消息仅是“对象引用未设置为对象的实例”。没有内在的例外。

我正在使用NSB 3.2.0.0。

    NServiceBus.SetLoggingLibrary.Log4Net(log4net.Config.XmlConfigurator.Configure);

    var assemblies = new List<Assembly> { Assembly.Load("NServiceBus"), Assembly.Load("NServiceBus.Core"), Assembly.Load("MyAssembly.LetterProcessor") };
    var cfg = NServiceBus.Configure.With(assemblies);

    cfg = cfg.DefineEndpointName("MyAssembly.letterprocessor");
    cfg = cfg.DefaultBuilder();
    cfg = cfg.DefiningMessagesAs(t => t.Namespace != null &&
                                      (
                                          t.Namespace.Equals("MyAssembly.LetterProcessor.NSB.BatchSaga")
                                          || t.Namespace.Equals("MyAssembly.LetterProcessor.NSB.FileSaga")
                                      )
        );
    cfg = cfg.MessageForwardingInCaseOfFault();
    cfg = cfg.Log4Net();
    cfg = cfg.StructureMapBuilder();
    cfg = cfg.XmlSerializer();
    cfg = cfg.MsmqTransport();
    cfg = cfg.IsTransactional(true);
    cfg = cfg.IsolationLevel(System.Transactions.IsolationLevel.Serializable);
    cfg = cfg.Sagas();
    cfg = cfg.NHibernateSagaPersister();
    var cfgu = cfg.UnicastBus();
    cfgu = cfgu.LoadMessageHandlers();
    var sb = cfgu.CreateBus();  // <-- breaks here.
    var bus = sb.Start();

...和堆栈跟踪:

    at System.Reflection.Emit.CustomAttributeBuilder.EmitValue(BinaryWriter writer, Type type, Object value)
    at System.Reflection.Emit.CustomAttributeBuilder.InitCustomAttributeBuilder(ConstructorInfo con, Object[] constructorArgs, PropertyInfo[] namedProperties, Object[] propertyValues, FieldInfo[] namedFields, Object[] fieldValues)
    at System.Reflection.Emit.CustomAttributeBuilder..ctor(ConstructorInfo con, Object[] constructorArgs, PropertyInfo[] namedProperties, Object[] propertyValues)
    at NServiceBus.MessageInterfaces.MessageMapper.Reflection.MessageMapper.BuildCustomAttribute(Object customAttribute)
    at NServiceBus.MessageInterfaces.MessageMapper.Reflection.MessageMapper.AddCustomAttributeToProperty(Object customAttribute, PropertyBuilder propBuilder)
    at NServiceBus.MessageInterfaces.MessageMapper.Reflection.MessageMapper.CreateTypeFrom(Type t, ModuleBuilder moduleBuilder)
    at NServiceBus.MessageInterfaces.MessageMapper.Reflection.MessageMapper.GenerateImplementationFor(Type interfaceType, ModuleBuilder moduleBuilder)
    at NServiceBus.MessageInterfaces.MessageMapper.Reflection.MessageMapper.InitType(Type t, ModuleBuilder moduleBuilder)
    at NServiceBus.MessageInterfaces.MessageMapper.Reflection.MessageMapper.InitType(Type t, ModuleBuilder moduleBuilder)
    at NServiceBus.MessageInterfaces.MessageMapper.Reflection.MessageMapper.InitType(Type t, ModuleBuilder moduleBuilder)
    at NServiceBus.MessageInterfaces.MessageMapper.Reflection.MessageMapper.Initialize(IEnumerable`1 types)
    at NServiceBus.Serializers.XML.Config.MessageTypesInitializer.Run()
    at NServiceBus.Configure.<Initialize>b__d(IWantToRunWhenConfigurationIsComplete o)
    at System.Collections.Generic.List`1.ForEach(Action`1 action)
    at NServiceBus.Configure.Initialize()
    at NServiceBus.Configure.CreateBus()
    at MyAssembly.LetterProcessor.Service.Tests.Tests.EndpointInit() in C:\svn\MyAssembly\LetterProcessor\trunk\src\MyAssembly.LetterProcessor.Service.Tests\Tests.cs:line 72>

1 个答案:

答案 0 :(得分:0)

已发现问题。这是因为我在与IMessage实现相同的命名空间中混合了Handler类。 有两种解决方案:(1)我可以将消息和处理程序分成不同的命名空间/程序集,或者(2)我可以通过IMessage指定DefiningMessagesAs函数过滤器。

第二个选项如下:

    var bus = NServiceBus.Configure.With(assemblies)
       .DefineEndpointName("myApp.letterprocessor")
       .DefiningMessagesAs(t => t.GetType().GetInterfaces().Contains(typeof(IMessage))) // <-- fixes my problem
       ... etc