Xml WCF参数始终为空
我最近继承了一个WCF Web服务(LeadService.svc),它似乎接受SOAP。我正在尝试将XML文件中的数据推送到服务的方法TestLead(),但迄今为止没有成功 以下代码触发服务方法,但在服务端,传入的值(lead)始终为null。为了测试它,我运行了两个VisualStudio实例,一个运行服务,另一个运行消费测试代码。我对此已经讨论了一段时间,但似乎不知道如何成功地将值传递到服务方法中。在Fiddler中撰写帖子产生了相同的结果(lead为空值)。我应该注意,在这个场景中,修改服务代码本身不是一个选项 除了下面的第一段代码,即客户机代码之外,所有代码都来自服务 非常感谢您的建议Xml WCF参数始终为空,xml,wcf,web-services,Xml,Wcf,Web Services,我最近继承了一个WCF Web服务(LeadService.svc),它似乎接受SOAP。我正在尝试将XML文件中的数据推送到服务的方法TestLead(),但迄今为止没有成功 以下代码触发服务方法,但在服务端,传入的值(lead)始终为null。为了测试它,我运行了两个VisualStudio实例,一个运行服务,另一个运行消费测试代码。我对此已经讨论了一段时间,但似乎不知道如何成功地将值传递到服务方法中。在Fiddler中撰写帖子产生了相同的结果(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>
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>
Web.config
<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>
我使用soap ui调试服务调用。我不知道fiddler的正确设置。如果您在VisualStudio中启动服务项目,那么您所需要的只是端口。该值应类似于htttp://localhost:5301/myservice.svc. 然后在fiddler或soap ui中输入dev url,输入有效负载并调用正在调试的过程。这应该允许您设置断点并调试到您的服务中 如我所见,服务方法参数不匹配:在def中。您需要TestLead类型的参数,inethod-SimpleLead 参数名称和类型应匹配。在其他情况下,您将收到null 这种问题通常可以通过SvcTraceViewer.Exe找到
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>