3

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

服务和客户端都有消息检查器,所以我可以看到发生了什么。

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

我的问题是,我可以影响调用 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;
   }
}
4

1 回答 1

1

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

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

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

于 2011-06-09T09:46:43.473 回答