簡體   English   中英

不含Svc服務的Autofac WCF注冊異常

[英]Autofac WCF registration exception with svc-less service

我正在嘗試將Autofac設置為我正在從事的新WCF項目的DI容器。 我們正在使用無svc的配置和自托管。 如果沒有Autofac並僅使用窮人的DI,一切都會按預期運行,但是當我將Autofac添加到混音中時,情況就會變糟。

我用來設置所有內容的代碼是:

  public class CustomServiceHostFactory : ServiceHostFactory
{
    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        var container = InitializeDIContainer();
        var customHost = new CustomServiceHost(serviceType, baseAddresses);
        // Exception is being thrown on the line below
        customHost.AddDependencyInjectionBehavior(serviceType, container);
        return customHost;
    }

    private static IContainer InitializeDIContainer()
    {
        var builder = new ContainerBuilder();
        builder.RegisterAssemblyTypes(typeof (AccountRepository).Assembly)
            .Where(t => t.Name.EndsWith("Repository"))
            .As(t => t.GetInterfaces().FirstOrDefault(
                i => i.Name == "I" + t.Name));

        builder.RegisterAssemblyTypes(typeof (AccountService).Assembly)
            .Where(t => t.Name.EndsWith("Service"))
            .As(t => t.GetInterfaces().FirstOrDefault(
                i => i.Name == "I" + t.Name));

        builder.RegisterType<tktktktkDbContext>().As<IDataContextAsync>();
        builder.RegisterType<UnitOfWork>().As<IUnitOfWorkAsync>();

        var container = builder.Build();
        return container;
    }
}

運行此命令時,出現以下異常:

An exception of type 'System.ArgumentException' occurred in Autofac.Integration.Wcf.dll but was not handled in user code.
Additional information: The service contract type 'tktktktk.Services.AccountClassService' has not been registered in the container.

當我在代碼中放置斷點並檢查容器時,我可以在ComponentRegistry.Registrations集合中看到我的所有服務,包括“ tktktktk.Services.AccountClassService”對象。

我已經嘗試過重新整理我的web.config文件以使用“ Autofac.Integration.Wcf.AutofacServiceHostFactory”工廠,但是隨后我的應用程序失敗了,甚至還沒有到達這一點。

我以為我錯過了某處的位置,但是卻茫然不知所措。 任何幫助將不勝感激!

UPDATE

我將web.config修改為使用“ Autofac.Integration.Wcf.AutofacServiceHostFactory”,如圖所示。 我現在收到以下錯誤:

WebHost failed to process a request.
 Sender Information: System.ServiceModel.ServiceHostingEnvironment+HostingManager/45653674
 Exception: System.ServiceModel.ServiceActivationException: The service '/Account/AccountClassService.svc' cannot be activated due to an exception during compilation.  The exception message is: The AutofacServiceHost.Container static property must be set before services can be instantiated.. ---> System.InvalidOperationException: The AutofacServiceHost.Container static property must be set before services can be instantiated.

更新2

我嘗試了以下答案中的建議,但出現了與上述相同的錯誤。 我注意到的一件事,當我在代碼中添加AutofacServiceHost.Container = container ,將無法編譯。 我將其切換為AutofacHostFactory.Container ,並且編譯良好。 另外,通過將web.config更改為使用Autofac.Integration.Wcf.AutofacServiceHostFactory ,我不再在此代碼中遇到任何斷點,這表明它現在已被完全繞過。

我還嘗試了無svc的配置。 我按照文檔中的步驟進行操作,並收到以下錯誤消息:

The service 'MyService' configured for WCF is not registered with the Autofac container. 

事實證明,所有內容均已正確注冊,但是web.config中的“添加工廠”元素需要該服務的程序集名稱,就像.svc文件存在時一樣:

<add factory="Autofac.Integration.Wcf.AutofacServiceHostFactory" relativeAddress="~/MyService.svc" service="NameSpace.Service, AssemblyName" />

Autofac文檔中沒有提及或在示例中顯示。 此外,Visual Studio將抱怨並在屬性下划線,但是該服務將正確運行。

這是您使用自定義主機的情況,另一方面,Autofac內置了ServiceHost作為開箱即用的功能。

您需要設置屬性Container of AutofacServiceHost類:

 public class CustomServiceHostFactory : ServiceHostFactory
    {
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            var container = InitializeDIContainer();

            // Add this line and try again.
            AutofacServiceHost.Container = container;

            var customHost = new CustomServiceHost(serviceType, baseAddresses);

            customHost.AddDependencyInjectionBehavior(serviceType, container);
            return customHost;
        }

        private static IContainer InitializeDIContainer()
        {
            var builder = new ContainerBuilder();
            builder.RegisterAssemblyTypes(typeof (AccountRepository).Assembly)
                .Where(t => t.Name.EndsWith("Repository"))
                .As(t => t.GetInterfaces().FirstOrDefault(
                    i => i.Name == "I" + t.Name));

            builder.RegisterAssemblyTypes(typeof (AccountService).Assembly)
                .Where(t => t.Name.EndsWith("Service"))
                .As(t => t.GetInterfaces().FirstOrDefault(
                    i => i.Name == "I" + t.Name));

            builder.RegisterType<tktktktkDbContext>().As<IDataContextAsync>();
            builder.RegisterType<UnitOfWork>().As<IUnitOfWorkAsync>();

            var container = builder.Build();
            return container;
        }
    }

我自己遇到了這個問題。 我設法通過使用Named樣式注冊解決了這個問題

```

 builder.RegisterType<YourServiceType>()
            .Named<object>("myservice");

 //and in your config:..

<serviceHostingEnvironment multipleSiteBindingsEnabled="true" >
  <serviceActivations>
    <add relativeAddress="~/serviceurl.svc" service="myservice" factory="Autofac.Integration.Wcf.AutofacServiceHostFactory"/>
  </serviceActivations>
</serviceHostingEnvironment>

```

請注意,就我而言,我沒有使用apsnetcompatibilitymode。

我認為這是AutofacHostFactory中的錯誤,它使用錯誤的servicename標識符來解析wcfservice類型。 使用Named注冊樣式,您基本上可以確保注冊的類型已知,並且AutofacHostFactory使用的名稱匹配

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM