我有一个简单的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。我没有看到如何使用此工具设置邮件大小参数。所以,我想自己捕获这些信息。
关于我如何做到这一点的任何想法?
答案 0 :(得分:7)
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在服务端编写记录器。
答案 1 :(得分:1)
喜欢它,帮了很多忙。但是在将来,您可能想告诉我们可能需要的using
语句。
对于其他人,您需要包括:
Using System.ServiceModel;
Using System.ServiceModel.Dispatcher;
Using System.ServiceModel.Channels;
Using System.ServiceModel.Description;