客户端上的WCF Message Inspector正在显示除服务上的Message Inspector之外的其他消息

时间:2011-06-09 09:17:28

标签: c# wcf inspector

我需要创建一个(WCF)客户端,该客户端与期望对消息进行签名的服务进行通信。由于我对WCF很陌生,我首先尝试设置一个简单的自助服务和一个与该服务对话的客户端。

服务和客户都有消息检查员,所以我可以看到线路上的内容。

但奇怪的是客户端上的MessageInspector没有显示任何消息签名,而服务上的MessageInspector显示的是Security头。

我的问题是,我可以影响messageinspector被调用的那一刻,我想它是在WCF签署消息之前调用的。

我在客户端使用下面的代码,没有其他配置设置:

EndpointAddress address = new EndpointAddress("http://localhost:8001/Someplace/CalculatorService");
WSHttpBinding wsbinding = new WSHttpBinding(SecurityMode.Message);

ChannelFactory<ICalculator> factory = new ChannelFactory<ICalculator>(wsbinding, address);
factory.Endpoint.Behaviors.Add(new MyBehaviour());
factory.Credentials.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, 
    StoreName.My,X509FindType.FindBySubjectName, "MyCertificate");
factory.Credentials.ServiceCertificate.SetDefaultCertificate(StoreLocation.CurrentUser, 
    StoreName.AddressBook, X509FindType.FindBySubjectName, "MyCertificate");

ICalculator client = factory.CreateChannel();
var total = client.Add(10, 20);

....

class MyInspector : IClientMessageInspector

   public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
   {
       Console.WriteLine("IClientMessageInspector.AfterReceiveReply called.");
       Console.WriteLine("Message: {0}", reply.ToString());
   }
   public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel)
   {
       Console.WriteLine("IClientMessageInspector.BeforeSendRequest called.");
       Console.WriteLine("Message: {0}", request.ToString());
       return null;
   }

class MyBehaviour : IEndpointBehavior

   public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
   {
       return;
   }
   public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
   {
       clientRuntime.MessageInspectors.Add(new MyInspector());
   }
   public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
   {
   }
   public void Validate(ServiceEndpoint endpoint)
   {
       return;
   }
}

1 个答案:

答案 0 :(得分:1)

否则您无法控制何时在管道中调用消息检查器。而不是使用消息检查器来检查消息,而是使用WCF消息跟踪+ svctraceviewer.exe。

http://msdn.microsoft.com/en-us/library/ms733025.aspx

http://msdn.microsoft.com/en-us/library/ms732023.aspx