我最近继承了一个似乎接受 SOAP 的 WCF Web 服务 (LeadService.svc)。到目前为止,我正在尝试将 XML 文件中的数据推送到服务的方法 TestLead() 中,但没有成功。
以下代码触发了服务方法,但在服务端传入的值(lead)始终为null。为了测试它,我运行了两个 Visual Studio 实例,一个运行服务,另一个运行正在使用的测试代码。我已经对此进行了一段时间的研究,但似乎无法弄清楚如何成功地将值传递到服务方法中。在 Fiddler 中编写 POST 产生了相同的结果(lead 为空值)。我应该注意,在这种情况下,修改服务代码本身不是一个选项。
所有代码都来自服务,除了下面的第一部分,它是客户端代码。
任何意见是极大的赞赏。
客户
using (var client = new WebClient())
{
var data = File.ReadAllText(Server.MapPath("~/App_Data/data.xml"));
client.Headers.Add("Content-Type", "text/xml;charset=utf-8");
client.Headers.Add("SOAPAction", "\"http://tempuri.org/ILeadService/TestLead\"");
client.Encoding = Encoding.UTF8;
try
{
var response = client.UploadString("http://localhost:54881/LeadService.svc/", data);
Response.Write(response);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
数据
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<TestLead xmlns="http://tempuri.org/">
<firstName>test</firstName>
</TestLead>
</soap:Body>
</soap:Envelope>
服务
ServiceResponse TestLead(SimpleLead lead) // this is always null
{
// ...
return sr;
}
WSDL
<wsdl:definitions name="LeadService" targetNamespace="http://tempuri.org/">
<wsdl:types>
<xsd:schema targetNamespace="http://tempuri.org/Imports">
<xsd:import schemaLocation="http://localhost:54881/LeadService.svc?xsd=xsd2" namespace="http://tempuri.org/"/>
<xsd:import schemaLocation="http://localhost:54881/LeadService.svc?xsd=xsd0" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/>
<xsd:import schemaLocation="http://localhost:54881/LeadService.svc?xsd=xsd1" namespace="http://schemas.datacontract.org/2004/07/123Services"/>
</xsd:schema>
</wsdl:types>
<wsdl:message name="ILeadService_TestLead_InputMessage">
<wsdl:part name="parameters" element="tns:TestLead"/>
</wsdl:message>
<wsdl:message name="ILeadService_TestLead_OutputMessage">
<wsdl:part name="parameters" element="tns:TestLeadResponse"/>
</wsdl:message>
<wsdl:portType name="ILeadService">
<wsdl:operation name="TestLead">
<wsdl:input wsaw:Action="http://tempuri.org/ILeadService/TestLead" message="tns:ILeadService_TestLead_InputMessage"/>
<wsdl:output wsaw:Action="http://tempuri.org/ILeadService/TestLeadResponse" message="tns:ILeadService_TestLead_OutputMessage"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="BasicHttpBinding_ILeadService" type="tns:ILeadService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="TestLead">
<soap:operation soapAction="http://tempuri.org/ILeadService/TestLead" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="LeadService">
<wsdl:port name="BasicHttpBinding_ILeadService" binding="tns:BasicHttpBinding_ILeadService">
<soap:address location="http://localhost:54881/LeadService.svc"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
合同
namespace 123Services
{
[ServiceContract]
public interface ILeadService
{
[OperationContract]
ServiceResponse TestLead(SimpleLead lead);
}
[DataContract]
public class SimpleLead
{
[DataMember]
public string FirstName { get; set; }
}
}
网页配置
<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<bindings>
<wsHttpBinding>
<binding name="123Services.LeadService">
<security mode="None">
<message clientCredentialType="UserName"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<services>
<service name="123Services.LeadService" behaviorConfiguration="123Services.LeadServiceBehavior">
<endpoint address="" binding="basicHttpBinding" contract="123Services.ILeadService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="123Services.LeadServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>