Unity.WCF要求默认构造函数

时间:2016-06-17 10:37:42

标签: c# wcf unity-container

实现Unity.WCF,如下所示:https://unitywcf.codeplex.com/

当我在使用无文件激活时托管服务时,它仍然要求我提供默认构造函数,而不是通过统一解析服务,我不确定我错过了什么。

WCFServiceFactory:

public class WcfServiceFactory : UnityServiceHostFactory
{
protected override void ConfigureContainer(IUnityContainer container)
{
        container.RegisterType<IPollerSVC, ProductionHostSVC>()
                 .RegisterType<PeriodicTaskFactory>()
                 .RegisterType<CancellationTokenSource>(new HierarchicalLifetimeManager())
                 .RegisterType<IProcessor, ThirdPartyProcessor>();      
}
}

的Web.config:

<?xml version="1.0"?>
<configuration>
  <appSettings/>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
    <httpRuntime/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https"/>
    </protocolMapping>
    <serviceHostingEnvironment>
      <serviceActivations>
        <add factory="IISHostThirdParty.WcfServiceFactory" relativeAddress="./ProductionHost.svc" service="Service.Common.ProductionHostSVC"/>
      </serviceActivations>
    </serviceHostingEnvironment>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
  -->
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>

服务:

    [ServiceContract(Namespace = "http://Services/IPollerService")]
    public interface IPollerSVC
    {
        Task PollingTask { get; }

    }

[ServiceBehavior(Name = "Services.SL.ThirdParty.ServiceHost", InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single, UseSynchronizationContext = true)]

public class ProductionHostSVC : IPollerSVC
{
    public ProductionHostSVC(IProcessor processor,
                             PeriodicTaskFactory taskFactory,
                             CancellationTokenSource ctks)
    {
        _ctks = ctks;

    }

    public Task PollingTask { get; }
}

1 个答案:

答案 0 :(得分:0)

相当古老但仍然有用的答案is here.

简而言之:如果使用InstanceProvider,则WCF不会调用InstanceContextMode.Single。因此,Unity.WCF不能用于实例化服务。