我有一个简单的c#3.5 .Net控制台应用程序,它连接到服务引用.一切正常 – 打电话和接收回复,但现在我被告知要查看消息中的Soap标题.
我发现.Net WebService Studio非常棒,并且会显示Soap请求和Soap响应.
对于响应,它显示如下:
ResponseCode: 200 (OK) Content-Language:en-US Content-Length:30048 Content-Type:text/xml; charset=utf-8 Date:Mon,25 Jan 2010 19:57:47 GMT Server:WebSphere Application Server/6.1 <?xml version="1.0" encoding="utf-16"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Header /> <soapenv:Body>
如何在我的应用程序中生成类似的东西?
我有兴趣看到的响应是一个不同的方法,它返回一个足够大的消息来炸毁WebService Studio.我没有看到如何使用此工具设置邮件大小参数.所以,我想自己捕获这些信息.
有关如何做到这一点的任何想法?
解决方法
WCF通过
config file进行跟踪,或者您可以实现自己记录消息的行为.
添加如下行为:
Service1SoapClient client = new Service1SoapClient(); client.Endpoint.Behaviors.Add( new MessageInspectionBehavior()); client.HelloWorld();
和代码:
class MessageInspectionBehavior : IClientMessageInspector,IEndpointBehavior { public void Validate(ServiceEndpoint endpoint) { } public void AddBindingParameters(ServiceEndpoint endpoint,BindingParameterCollection bindingParameters) { } public void ApplyDispatchBehavior(ServiceEndpoint endpoint,EndpointDispatcher endpointDispatcher) { } public void ApplyClientBehavior(ServiceEndpoint endpoint,ClientRuntime clientRuntime) { clientRuntime.MessageInspectors.Add(this); } public object BeforeSendRequest(ref Message request,IClientChannel channel) { //Write request message Console.WriteLine(request.ToString()); return null; } public void AfterReceiveReply(ref Message reply,object correlationState) { // Write out http headers foreach (var property in reply.Properties) { if (!(property.Value is HttpResponseMessageProperty)) continue; var httpProperties = (HttpResponseMessageProperty)property.Value; foreach (KeyValuePair<object,object> kvp in httpProperties.Headers) { Console.WriteLine(kvp.Key + ":" + kvp.Value); } } // Write result message Console.WriteLine(reply.ToString()); } }
类似地,您可以使用IDispatchMessageInspector和IServiceBehavior在服务端编写记录器.