使用预定义的Autofac容器配置NServicebus 4.7

时间:2015-06-25 15:50:41

标签: autofac nservicebus

我在这里有点不知所措。我正在开发一个托管的NServicebus(v4.7)Windows服务,并使用Autofac(v3.5)来做我自己的类型解析。鉴于NServicebus文档,我认为可以在NServicebus的端点配置中添加我自己的容器。但无论我做什么,我都无法让它发挥作用。我当前的endpointconfig如下:

using System;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Reflection;
using Autofac;
using HyFlo.Base;
using NServiceBus;
using NServiceBus.ObjectBuilder.Autofac;
using NServiceBus.ObjectBuilder.Common.Config;

namespace HyFlo.Service
{
    /// <summary>
    ///     EndpointConfig
    /// </summary>
    public class EndpointConfig : IConfigureThisEndpoint, INeedInitialization, AsA_Publisher
    {
        public void Init()
        {
            TraceWriter.Info("Scannning for Hyflo assemblies .. ");

            var hyfloAssemblies = Directory.GetFiles(Directory.GetCurrentDirectory(), "Hyflo.*.dll",
                                                     SearchOption.TopDirectoryOnly);

            TraceWriter.Info("Initializing Autofac with assemblies .. ");
            try
            {
                var builder = new ContainerBuilder();
                Assembly[] allAssemblies = hyfloAssemblies.Select(Assembly.LoadFile).ToArray();
                builder.RegisterAssemblyTypes(allAssemblies).AsImplementedInterfaces().InstancePerLifetimeScope();
                IContainer container = builder.Build();

                TraceWriter.Trace(
                    String.Format(
                        "Found {0} assembl{1}. Configuring NServicebus to use Autofac preinitialized with these assembl{1} ..",
                        allAssemblies.Count(), allAssemblies.Count() == 1 ? "y" : "ies"));

                Configure.With().UsingContainer(new AutofacObjectBuilder(container));
            }
            catch (Exception ex)
            {
                TraceWriter.Trace(String.Format("Exception occurred during initialization. Exception is: {0}\n{1}",
                                                ex.Message, ex.StackTrace));
            }

            TraceWriter.Info("Initializing database en msmqsubscription .. ");

            string databusBasePath = ConfigurationManager.AppSettings["DataBusBasePath"] ?? "";

            TraceWriter.Trace(String.Format("Setting Databus's basepath to '{0}' ..", databusBasePath));
            Configure.With().FileShareDataBus(databusBasePath);

            Configure.With().MsmqSubscriptionStorage();
        }
    }
}

此代码本身有效,但是当NServicebus初始化继续时会弹出一个异常:

  

无法执行安装程序:System.InvalidOperationException:无法为尚未配置的类型配置属性:NServiceBus.Un   icast.UnicastBus      at NServiceBus.ObjectBuilder.Autofac.AutofacObjectBuilder.ConfigureProperty(Type component,String property,Object value)in c:\ BuildAgent \ work \ 1b   05a2fea6e4cd32 \ src \ NServiceBus.Core \ ObjectBuilder \ Autofac \ AutofacObjectBuilder.cs:第0行      at NServiceBus.ObjectBuilder.Common.CommonObjectBuilder.ConfigureProperty [T](String propertyName,Object value)in c:\ BuildAgent \ work \ 1b05a2fea6e4c   d32 \ src \ NServiceBus.Core \ ObjectBuilder \ Common \ CommonObjectBuilder.cs:第110行      在c:\ BuildAgent \ work中的NServiceBus.Unicast.Config.FinalizeUnicastBusConfiguration.RegisterMessageOwnersAndBusAddress(IEnumerable'1 knownMessages)   \ 1b05a2fea6e4cd32 \ src \ NServiceBus.Core \ Unicast \ Config \ FinalizeUnicastBusConfiguration.cs:第57行      位于c:\ BuildAgent \ work \ 1b05a2fea6e4cd32 \ src \ NServiceBus.Core \ U中的NServiceBus.Unicast.Config.FinalizeUnicastBusConfiguration.FinalizeConfiguration()   nicast \ Config \ FinalizeUnicastBusConfiguration.cs:第24行      在NServiceBus.Configure。&lt;&gt; c__DisplayClass23`1.b__20(类型t)c:\ BuildAgent \ work \ 1b05a2fea6e4cd32 \ src \ NServiceBus.Core \ Configu   re.cs:第555行      在System.Collections.Generic.List'1.ForEach(Action'1动作)      at NServiceBus.Configure.ActivateAndInvoke [T](Action'1 action,Nullable'1 thresholdForWarning)在c:\ BuildAgent \ work \ 1b05a2fea6e4cd32 \ src \ NServiceB   us.Core \ Configure.cs:第561行      位于c:\ BuildAgent \ work \ 1b05a2fea6e4cd32 \ src \ NServiceBus.Core \ Configure.cs中的NServiceBus.Configure.Initialize():第361行      位于c:\ BuildAgent \ work \ 1b05a2fea6e4cd32 \ src \ NServiceBus.Hosting.Windows \ Inst中的NServiceBus.Hosting.Windows.Installers.WindowsInstaller.RunInstall()   allers \ WindowsInstaller.cs:第38行

无论我做什么,由于某种原因,NServicebus的初始化无法继续。通过使用Configure.With()。UsingContainer()方法看起来我自己的Autofac容器没有在NServicebus中正确配置,但我尝试了所有类型的选项,但每次归结为抛出相同的异常。我在这里做错了什么想法?

2 个答案:

答案 0 :(得分:0)

我找到了问题的答案。我扫描了几乎所有DLL都包含在Nservicebus的Autofac容器中。这还包括包含endpointconfig的DLL。现在,当我排除包含端点的DLL时,一切顺利。我现在已经从endpointconfig-DLL中隔离了几乎所有类,除了消息处理程序。现在一切顺利!

答案 1 :(得分:-1)

尝试更改代码:

Configure configure = Configure.With();
// rest of container initialization
configure.AutofacBuilder(container);
// other stuff
configure.FileShareDataBus(databusBasePath);
configure.MsmqSubscriptionStorage();

看看它是否有效。