Kim*_*rly 127
我想你的意思是你想在客户端看到XML,而不是在服务器上跟踪它.在这种情况下,您的答案在我上面链接的问题中,以及如何检查或修改客户端上的消息.但是,由于该文章的.NET 4版本缺少其C#,并且.NET 3.5示例中存在一些混淆(如果不是错误),这里它会针对您的目的进行扩展.
您可以使用IClientMessageInspector拦截消息:
using System.ServiceModel.Dispatcher;
public class MyMessageInspector : IClientMessageInspector
{ }
Run Code Online (Sandbox Code Playgroud)
该接口中的方法,BeforeSendRequest并AfterReceiveReply允许您访问请求和回复.要使用检查器,您需要将其添加到IEndpointBehavior:
using System.ServiceModel.Description;
public class InspectorBehavior : IEndpointBehavior
{
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.MessageInspectors.Add(new MyMessageInspector());
}
}
Run Code Online (Sandbox Code Playgroud)
您可以将该接口的其他方法保留为空实现,除非您也想使用它们的功能.阅读操作方法以获取更多详细信息.
实例化客户端后,将行为添加到端点.使用示例WCF项目中的默认名称:
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
client.Endpoint.Behaviors.Add(new InspectorBehavior());
client.GetData(123);
Run Code Online (Sandbox Code Playgroud)
设置断点MyMessageInspector.BeforeSendRequest(); request.ToString()重载以显示XML.
如果您要操纵消息,则必须处理消息的副本.有关详细信息,请参阅使用消息类.
感谢Zach Bonham在另一个问题上找到这些链接的答案.
OperationContext.Current.RequestContext.RequestMessage
Run Code Online (Sandbox Code Playgroud)
在处理请求期间,此上下文是可访问的服务器端.这不适用于单向操作
小智 6
我们只需跟踪请求消息即可.
OperationContext context = OperationContext.Current;
if (context != null && context.RequestContext != null)
{
Message msg = context.RequestContext.RequestMessage;
string reqXML = msg.ToString();
}
Run Code Online (Sandbox Code Playgroud)
我只是想将此添加到金伯利的答案中。也许它可以节省一些时间并避免由于未实现 IEndpointBehaviour 接口所需的所有方法而导致的编译错误。
此致
尼基
/*
// This is just to illustrate how it can be implemented on an imperative declarared binding, channel and client.
string url = "SOME WCF URL";
BasicHttpBinding wsBinding = new BasicHttpBinding();
EndpointAddress endpointAddress = new EndpointAddress(url);
ChannelFactory<ISomeService> channelFactory = new ChannelFactory<ISomeService>(wsBinding, endpointAddress);
channelFactory.Endpoint.Behaviors.Add(new InspectorBehavior());
ISomeService client = channelFactory.CreateChannel();
*/
public class InspectorBehavior : IEndpointBehavior
{
public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
// No implementation necessary
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.MessageInspectors.Add(new MyMessageInspector());
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
// No implementation necessary
}
public void Validate(ServiceEndpoint endpoint)
{
// No implementation necessary
}
}
public class MyMessageInspector : IClientMessageInspector
{
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
// Do something with the SOAP request
string request = request.ToString();
return null;
}
public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{
// Do something with the SOAP reply
string replySoap = reply.ToString();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
104647 次 |
| 最近记录: |