我听说可以在客户端和服务器wcf端放置检查器,它可以拦截消息并在发布到客户端/服务器之前修改它的内容。我想知道是否可能,如果是,那么如何?
如果数据刚刚通过,我怎么能在两端使用我们自己的逻辑加密/解密数据。我搜索谷歌有一些关于这个主题的好文章,但不幸的是我没有。所以如果有人知道任何讨论如何开发这种检查员并在wcf客户端部署的网址。服务器然后请与我分享。感谢
答案 0 :(得分: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
<强>两步强>
在您的应用配置中,您必须添加如下所示的行为定义
<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
通常情况下,https连接会为您完成这项工作link
但如果您需要一种棘手的方法来实现这一点,您可以使用一个公共类,它将成为您所有通信的主要类,并在其中一个属性中加密您的消息
像这样的东西
[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
}
}