2014-04-02 85 views
0

我聽說可以將檢查器放在客戶端和服務器wcf端,它可以截獲消息並在發佈到客戶端/服務器之前修改它的內容。我想知道是否有可能,如果是的話,那麼如何?如何把檢查員同時在客戶端和服務器端WCF

當數據剛剛通過時,我怎麼能用我們自己的邏輯在兩端加密/解密數據。我搜索谷歌有關這個話題的一些很好的寫作,但不幸的是我沒有。所以如果有人知道任何有關討論如何開發這種檢查員並在wcf客戶端&服務器上部署的url,請與我分享。感謝

回答

0

所有你需要的是遵循這一步驟

1步

在你的代碼應該添加MessageInspectorExtension

class MessageInspectorExtension : BehaviorExtensionElement 
    { 
     public override Type BehaviorType 
     { 
      get { return typeof(MessageInspector); } 
     } 

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

MessageInspector將盡一切你所需要的工作它應該這樣定義

//here the `MessageInspector` class showing what are the interfaces responsable for doing this 
public class MessageInspector : IDispatchMessageInspector, IServiceBehavior, IEndpointBehavior 
    { 
    } 

您所需要的電源的方法是

public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext) 
     {} 

Which is called after an inbound message has been received but before the message is dispatched to the intended operation 

public void BeforeSendReply(ref Message reply, object correlationState) 
     { 
      if (reply.IsFault) 
      {//do here } 
      else do your code here 
     } 

wich is called after the operation has returned but before the reply message is sent 

2步

在你的應用程序配置必須添加行爲定義像下面

<behaviors> 
      <endpointBehaviors>   
      <behavior name="ServiceBehaviorWithInterceptor"> 
       <ServiceInspector/> 
      </behavior> 
      </endpointBehaviors> 
     <behaviors> 
<!--Extensions--> 
    <extensions> 
      <behaviorExtensions> 
      <add name="ServiceInspector" type="yourNameSpace.MessageInspector.MessageInspectorExtension, assemblyname, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/> 
      </behaviorExtensions> 
     </extensions> 

「3步

<service name="yourServiceContractFullName" behaviorConfiguration="ServiceBehaviorWithInterceptor"> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://yourserver:port/root/yourServiceContract" /> 
      </baseAddresses> 
     </host> 
     <endpoint address="" binding="basicHttpBinding" bindingConfiguration="basicHttpsBindingConfig" contract="YourContract" /> 
     </service> 

Update

how could i encrypt/decrypt data with our own logic at both end when data just passed 通常你看到這個link
HTTPS連接將做的工作,但如果你需要一個棘手的方式實現這一點,你可以使用一個通用的類,它將成爲你所有通信的主要類,並在一個o中加密你的消息f,將它的屬性

像這樣的事情

[DataContract] 
    public class BaseCustomEncMessage 
    { 
     private Object _object = null; 
     [DataMemberAttribute] 
     public Object CipheredMessage { get { return _object; } } 
     public void Cipher(object obj) 
     { 
      //here you can use 3DES for example and serialize your instance as an object 

     } 
    } 
+0

有MessageInspectorExtension&MessageInspector之間沒有任何關係。 – Thomas

+0

有沒有任何文章可以從我可以得到完整的源代碼。我應該使用哪個關鍵字來搜索谷歌上述場景。 – Thomas

+0

您可以在http://go.microsoft.com/fwlink/?LinkId=87352上找到完整的示例,下載並安裝示例後,嘗試查找\ WCF \ Extensibility \ MessageInspectors \ SchemaValidation \ CS –