2017-05-24 58 views
0

我需要自定義WCF服務中的每個方法 - 添加日誌記錄。無法將值添加到集合中,因爲集合已包含相同類型的項目...

所以,我安裝一個專用的操作行爲爲每個操作時,服務主機創建:

public class CoreServiceHostFactory : ServiceHostFactory 
{ 
    #region Overrides of ServiceHostFactory 

    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) 
    { 
     var host = base.CreateServiceHost(serviceType, baseAddresses); 
     var operationBehavior = new WcfOperationBehavior(); 
     host.Description.Endpoints 
      .SelectMany(e => e.Contract.Operations) 
      .ForEach(o => o.Behaviors.Add(operationBehavior)); 

     return host; 
    } 

    #endregion 
} 

它本地工作,但在QA部署時失敗,

的數值不被添加到集合中,因爲集合 已包含相同類型的項目: ...此集合僅支持 支持每種類型的一個實例。參數名:項目

web.config中的有關條文是:

<services> 
    <service name="DeviceServices.DeviceService" behaviorConfiguration="SLDeviceServices.ServiceBehavior"> 
     <endpoint address="" binding="basicHttpBinding" bindingConfiguration="StandardBindingSSL" contract="DeviceServices.IDeviceService"> 
     </endpoint> 
     <endpoint address="" binding="basicHttpBinding" bindingConfiguration="StandardBinding" contract="DeviceServices.IDeviceService"> 
     </endpoint> 
    </service> 
    <service name="DeviceServices.DeviceServiceJSON" behaviorConfiguration="SLDeviceServices.ServiceBehavior"> 
     <endpoint address="" binding="webHttpBinding" bindingConfiguration="StandardBindingSSL" contract="DeviceServices.DeviceServiceJSON" behaviorConfiguration="DeviceServices.JSONDeviceServiceBehavior"> 
     </endpoint> 
     <endpoint address="" binding="webHttpBinding" bindingConfiguration="StandardBinding" contract="DeviceServices.DeviceServiceJSON" behaviorConfiguration="DeviceServices.JSONDeviceServiceBehavior"> 
     </endpoint> 
    </service> 
</services> 
<behaviors> 
    <serviceBehaviors> 
     <behavior name="SLDeviceServices.ServiceBehavior"> 
      <serviceMetadata httpsGetEnabled="false" /> 
      <serviceDebug includeExceptionDetailInFaults="true" /> 
     </behavior> 
    </serviceBehaviors> 
    <endpointBehaviors> 
     <behavior name="DeviceServices.JSONDeviceServiceBehavior"> 
      <webHttp /> 
     </behavior> 
    </endpointBehaviors> 
</behaviors> 

我不明白是什麼問題。任何人?

回答

1

我找到了根本原因。在QA中,該服務有兩個端點 - http和https,但兩者都綁定到同一個合同。

因此,代碼

host.Description.Endpoints 
    .SelectMany(e => e.Contract.Operations) 
    .ForEach(o => o.Behaviors.Add(operationBehavior)); 

橫穿同一合同的兩倍。解決方法是:

host.Description.Endpoints 
    .Select(e => e.Contract) 
    .Distinct() 
    .SelectMany(c => c.Operations) 
    .ForEach(o => o.Behaviors.Add(operationBehavior)); 
相關問題