2013-07-02 30 views
2

我正在看實現IDispatchMessageInpector & IClientMessageInpector查看AfterReceiveRequest和BeforeSendRequest方法中的消息對象。 我的要求是在WCF服務的代碼級進行更改。沒有配置更改。 如何將此行爲附加到所有調用此服務和服務自己的端點。正在實施IContractBehaviour幫助我嗎?如何使用ServiceHostFactory將行爲添加到IIS託管的WCF服務中的客戶端端點

編輯1: WCF服務託管在IIS上。是否有可能通過代碼添加行爲?

編輯2: 好像使用ServiceHostFactory我們可以實現這一點。 如何將行爲添加到在webconfig中定義的客戶端端點?

+0

看看Carlos Figueira的博客 - 他有一個關於WCF可擴展性的系列代碼示例 - [WCF Extensibility](http://blogs.msdn.com/b/carlosfigueira/存檔/ 2011/03/14/wcf-extensibility.aspx) – Tim

+0

我正在看博客,作爲WCF的新手,我只想看看我是否採取正確的路徑:) –

回答

3

是的,可以爲託管在IIS中的服務添加行爲。行爲不關心服務的託管環境。 Carlos Figueira的博客提供了可應用於服務,終端,合同和運營的所有類型行爲的示例。我試着爲我服務的樣例代碼託管在IIS(在web.config文件中定義端點) - 配置文件在這裏需要補充的行爲ExtensionElement

public class MyEndpointBehavior : BehaviorExtensionElement, IEndpointBehavior 
{ 
      public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) 
      { 
       Console.WriteLine("applying dispatch behavior"); 
       endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new MyInspector()); 
       endpointDispatcher.DispatchRuntime.OperationSelector = new MyOperationSelector(); 
      } 

     public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) 
     { 
     } 

     public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) 
     { 
     } 

     public void Validate(ServiceEndpoint endpoint) 
     { 
     } 

     public override Type BehaviorType 
     { 
      get { return this.GetType(); } 
     } 

     protected override object CreateBehavior() 
     { 
      return new MyEndpointBehavior(); 
     } 
} 

    public class MyOperationSelector : IDispatchOperationSelector 
    { 
     public string SelectOperation(ref Message message) 
     { 
      Console.WriteLine("good luck"); 
      string action = message.Headers.Action; 
      return action.Substring(action.LastIndexOf('/') + 1); 
     } 
    } 

    public class MyInspector : IDispatchMessageInspector 
    { 

     public object AfterReceiveRequest(ref Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext) 
     { 
      return (Message) request; 
     } 

     public void BeforeSendReply(ref Message reply, object correlationState) 
     { 
     } 
    } 
} 
與行爲

配置文件添加爲擴展元素 -

<system.serviceModel> 
    <services> 
    <service name="RouteToServiceA.Service1"> 
    <endpoint address="Service1" binding="basicHttpBinding" contract="RouteToServiceA.IService1" behaviorConfiguration="testEndPoint" /> 
    </service> 
</services> 
<behaviors> 
    <serviceBehaviors> 
    <behavior> 
     <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> 
     <serviceMetadata httpGetEnabled="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="true" /> 
    </behavior> 
    </serviceBehaviors> 
    <endpointBehaviors> 
    <behavior name="testEndPoint"> 
     <testBehavior /> 
    </behavior> 
    </endpointBehaviors> 
</behaviors> 
<extensions> 
    <behaviorExtensions> 
    <add name="testBehavior" type="RouteToServiceA.MyEndpointBehavior, RouteToServiceA" /> 
    </behaviorExtensions> 
</extensions> 
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
</system.serviceModel> 
+0

您可以同樣添加相關行爲到客戶端元素(IEndpointBehavior,IContractBehavior,IOperationBehavior),並將它們添加到配置文件的behaviorextensions部分,如上例所述。 – vibhu

+0

我正在尋找沒有任何配置更改的方法 –

0

使用ServiceHostFactory我們可以添加服務行爲,而將行爲配置添加到配置中的客戶端端點似乎無法實現。所以我正在進行配置更改

相關問題