我需要創建一個(WCF)客戶端,該客戶端與期望消息進行簽名的服務進行通信。因爲我對WCF很陌生,所以我首先嚐試設置一個簡單的selfhost服務和一個與此服務對話的客戶端。客戶端上的WCF MessageInspector正在顯示除了服務上的Messageinspector之外的其他消息
服務和客戶端都有消息檢查器,所以我可以看到會發生什麼事情。
但奇怪的是,客戶端上的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;
}
}