我需要创建一个(WCF)客户端,与期望消息被签名的服务通信。由于我对WCF还比较陌生,所以我首先尝试设置一个简单的自托管服务和一个与此服务通信的客户端。
服务和客户端都有消息检查器,因此我可以看到传输的内容。
然而奇怪的是,客户端上的MessageInspector没有显示任何消息签名,而服务上的MessageInspector显示了安全头。
我的问题是,我是否可以影响MessageInspector被调用的时刻,我猜测它在WCF签署消息之前被调用。
我在客户端使用以下代码,没有额外的配置设置:
服务和客户端都有消息检查器,因此我可以看到传输的内容。
然而奇怪的是,客户端上的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;
}
}