开发者

WCF MessageInspector on client is showing an other message than the Messageinspector on dthe service

开发者 https://www.devze.com 2023-03-11 06:50 出处:网络
I need to create a (WCF) client that communicates with a service that expects the messages to be signed. Since I\'m quite new to WCF I first tried to setup a simple selfhost service and a client that

I need to create a (WCF) client that communicates with a service that expects the messages to be signed. Since I'm quite new to WCF I first tried to setup a simple selfhost service and a client that talks to this servive.

Both the service and client have message inspectors so I can see what's going over the line.

The strange thing however is that the MessageInspector on the client is NOT showing any signing of the message, while the MessageInspector on the service is showing a Security header.

My question is, can I influence the moment the messageinspector get called, I guess that it is called before WCF signed the message.

I use the folowing code on then client side, with no additional config settings:

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(Serv开发者_JS百科iceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
   {
   }
   public void Validate(ServiceEndpoint endpoint)
   {
       return;
   }
}


No you can't control when in the pipeline the message inspectors are called. Instead of using message inspectors to inspect the messages, use WCF message tracing + svctraceviewer.exe instead.

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消