客户端上的WCF MessageInspector显示的消息不是服务上的MessageInspecter
本文关键字:服务 MessageInspecter 显示 WCF MessageInspector 客户端 消息 | 更新日期: 2023-09-27 17:58:40
我需要创建一个(WCF)客户端,该客户端与期望对消息进行签名的服务进行通信。由于我是WCF的新手,我首先尝试设置一个简单的自主机服务和一个与该服务对话的客户端。
服务和客户都有消息检查器,所以我可以看到线路上发生了什么。
然而,奇怪的是,客户端上的MessageInspector没有显示消息的任何签名,而服务上的message检查器显示的是Security标头。
我的问题是,我能影响消息检查器被调用的那一刻吗?我想它是在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;
}
}
不,您无法控制在管道中何时调用消息检查器。不要使用消息检查器来检查消息,而是使用WCF消息跟踪+svctraceviewer.exe。
http://msdn.microsoft.com/en-us/library/ms733025.aspx
http://msdn.microsoft.com/en-us/library/ms732023.aspx