若要使用此解决方案,您需要使用 "Add Service Reference" 而不是 "Add Web Reference" 功能,它可以用于 ASMX 或 WCF 服务。(您需要使用 .NET Framework 3.X 来使用此功能)
本文将帮助您将服务引用添加到 C# 项目中。
为拦截请求和响应的 XML,实现以下两个类:
public class InspectorBehavior : IEndpointBehavior
{
public string LastRequestXML {
get
{
return myMessageInspector.LastRequestXML;
}
}
public string LastResponseXML {
get
{
return myMessageInspector.LastResponseXML;
}
}
private MyMessageInspector myMessageInspector = new MyMessageInspector();
public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
}
public void Validate(ServiceEndpoint endpoint)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.MessageInspectors.Add(myMessageInspector );
}
}
public class MyMessageInspector : IClientMessageInspector
{
public string LastRequestXML { get; private set; }
public string LastResponseXML { get; private set; }
public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{
LastResponseXML = reply.ToString();
}
public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
{
LastRequestXML = request.ToString();
return request;
}
}
然后,将调用代码更改为:
MyTestServiceSoapClient client = new MyTestServiceSoapClient()
var requestInterceptor = new InspectorBehavior()
client.Endpoint.Behaviors.Add(requestInterceptor )
client.DoSomething("param1", "param2", "param3")
string requestXML = requestInterceptor.LastRequestXML
string responseXML = requestInterceptor.LastResponseXML
****编辑****
这与服务器端技术无关,您可以将其用于WCF,ASMX,PHP等Web服务,我已在以下网站上进行了测试:
http://www.w3schools.com/webservices/tempconvert.asmx
并得到以下XML:
requestXML=
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/CelsiusToFahrenheit</Action>
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<CelsiusToFahrenheit xmlns="http://tempuri.org/">
<Celsius>50</Celsius>
</CelsiusToFahrenheit>
</s:Body>
</s:Envelope>
响应XML =
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<s:Header xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" />
<soap:Body>
<CelsiusToFahrenheitResponse xmlns="http://tempuri.org/">
<CelsiusToFahrenheitResult>122</CelsiusToFahrenheitResult>
</CelsiusToFahrenheitResponse>
</soap:Body>
</soap:Envelope>
****编辑 2****
"Add Web Reference" 不是专为 ASMX 设计的,也不是 ASMX 客户端技术,而 "Add Service Reference" 也不是 WCF 客户端技术。你可以使用这两个工具来添加对于 ASMX、WCF、JSP 或 PHP 开发的 Web 服务的引用。如果你需要使用 "Add Service Reference" 来添加引用,你需要应用程序使用 .Net Framework 3.5。
这篇文章 提到:
在 Visual Studio 中使用 “Add Web Reference” 对话框时,将生成一个客户端代理,并将其添加到 Visual Studio 项目中。通常用于 ASMX 服务,但是您也可以使用 “Add Web Reference” 对话框为 WCF 服务创建客户端代理。但是,您需要手动输入服务 URL,并且生成的代理使用 XML 序列化,这是唯一支持的序列化类型。要为支持数据契约序列化器的 WCF 服务创建客户端代理,可以使用 Svcutil.exe 工具或使用 Visual Studio 开发工具的 .NET Framework 3.x 的 "Add Service Reference" 功能。