使用NServiceBus 3对象构建器解决Unity 2容器问题

时间:2011-12-28 01:40:05

标签: unity-container nservicebus

我在使用Unity 2的NServiceBus 3.0.0 beta 2预发布对象构建器时收到以下错误。据我所知,它似乎是AutowireEnabledInjectionProperty中的CommonObjectBuilder问题助洗剂。

NSB的配置如下:

public class NServiceBusContainerExtension : UnityContainerExtension
{
    protected override void Initialize()
    {
        var busConfig = NServiceBus.Configure.WithWeb()
            .Log4Net()
            .UnityBuilder(Container)
            .MsmqTransport()
                .IsTransactional(true)
                .PurgeOnStartup(false)
            .XmlSerializer()
            .UnicastBus();

        var bus = busConfig
                    .CreateBus()
                    .Start();

        Container.RegisterType<IPublisher, NServiceBusPublisher>(new InjectionConstructor(bus));
    }
}

错误:

[ArgumentNullException: Value cannot be null.
Parameter name: typeToCreate]
   Microsoft.Practices.Unity.Utility.Guard.ArgumentNotNull(Object argumentValue, String argumentName) +55
   NServiceBus.ObjectBuilder.Unity.AutowireEnabledInjectionProperty.AddPolicies(Type typeToCreate, Type implementationType, String name, IPolicyList policies) +87
   Microsoft.Practices.Unity.UnityContainer.RegisterType(Type from, Type to, String name, LifetimeManager lifetimeManager, InjectionMember[] injectionMembers) +405
   Microsoft.Practices.Unity.InjectedMembers.ConfigureInjectionFor(Type serviceType, Type implementationType, String name, InjectionMember[] injectionMembers) +66
   Microsoft.Practices.Unity.InjectedMembers.ConfigureInjectionFor(Type typeToInject, InjectionMember[] injectionMembers) +22
   NServiceBus.ObjectBuilder.Unity.ConfigureComponentAdapter.ConfigureProperty(String name, Object value) +325
   NServiceBus.ObjectBuilder.Unity.UnityObjectBuilder.ConfigureProperty(Type concreteComponent, String property, Object value) +338
   NServiceBus.ObjectBuilder.Common.ComponentConfig.NServiceBus.ObjectBuilder.IComponentConfig.ConfigureProperty(String name, Object value) +120
   NServiceBus.ObjectBuilder.Common.ComponentConfig`1.NServiceBus.ObjectBuilder.IComponentConfig<T>.ConfigureProperty(Expression`1 property, Object value) +266
   NServiceBus.ObjectBuilder.Common.Config.ConfigureCommon.With(Configure config, IContainer container) +633
   NServiceBus.ConfigureUnityBuilder.UnityBuilder(Configure config, IUnityContainer container) +112

2 个答案:

答案 0 :(得分:1)

在调用.CreateBus()之前,您需要在容器中注册所有新类型。

答案 1 :(得分:0)

我想接受Udi's回答,因为他是男人。但是,尽管尝试注册我们需要的每种类型,但我始终无法使Unity 2构建器工作。

在一周左右之后,我最终回到使用DefaultBuilder,将我们预构建的Unity容器传递到ServiceLocator,并将IServiceLocator作为单例注册到NSB的默认构建器。它运作得很好。