WCF参数始终为空

时间:2013-04-15 01:16:29

标签: xml wcf web-services

我最近继承了一个似乎接受SOAP的WCF Web服务(LeadService.svc)。到目前为止,我正在努力将数据从XML文件推送到服务的方法TestLead()中。

以下代码触发服务方法,但在服务端,传入(lead)的值始终为null。为了测试它,我正在运行两个Visual Studio实例,一个运行服务,另一个运行消耗测试代码。我一直在咀嚼这一点,似乎无法弄清楚如何成功地将值传递给服务方法。在Fiddler中编写POST产生了相同的结果(铅的空值)。我应该注意,在这种情况下,修改服务代码本身不是一个选项。

所有代码均来自服务,但下面的第一部分除外,即客户代码。

非常感谢任何建议。


客户端

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; }
    }
}

的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>

2 个答案:

答案 0 :(得分:0)

我使用soap ui来调试服务调用。我不知道fiddler中的正确设置。如果您在visual studio中启动服务项目,那么您只需要端口。该值应类似于htttp:// localhost:5301 / myservice.svc。从那里输入fiddler或soap ui中的dev url并输入有效负载并调用正在调试的过程。这应该允许您设置断点并调试到您的服务。

答案 1 :(得分:0)

正如我所看到的,服务方法参数不匹配:在def中。你期望TestLead类型的patameter,inethod - SimpleLead。

参数名称和类型应匹配。在其他情况下,您将收到null。

这种问题可以通过SvcTraceViewer.Exe

找到