【问题标题】:WCF client receiving response with null value even though response has data即使响应有数据,WCF 客户端也会收到具有空值的响应
【发布时间】:2017-06-27 02:33:26
【问题描述】:

我正在以这种非常简单的方式使用 WCF 客户端:

        using (var client = new ExampleService_TestClient())
        {
            client.ClientCredentials.UserName.UserName = "user";
            client.ClientCredentials.UserName.Password = "pass";
            var request = new ExampleService_TestRequest(requestData);
            var response = client.ExampleService_Test(request); 
            // response.Response_MT is null
            // when it should have errorMsg="OK"
        }

数据已正确发送。

但是响应属性 errorMsg 为空

这是信封:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Inbound_MT xmlns="https://example.com/ExampeService/">
        <event_id xmlns="">1</event_id>
        <event_name xmlns="">insert</event_name>
        <event_date xmlns="">2017-06-22 16:09</event_date>
        <employee_number xmlns="">666</employee_number>
        <employee_first_name xmlns="">Chuck</employee_first_name>
        <employee_last_name xmlns="">Inserted Norris</employee_last_name>
    </Inbound_MT>
</s:Body>
</s:Envelope>

我使用 fiddler 看到的响应:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Header></soap:Header><soap:Body><Response_MT>
    <errorMsg>OK</errorMsg>
</Response_MT></soap:Body>
</soap:Envelope>

WSDL:

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:p1="https://example.com/ExampeService/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" targetNamespace="https://example.com/ExampeService/">
   <wsdl:types>
      <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="https://example.com/ExampeService/" targetNamespace="https://example.com/ExampeService/">
         <xsd:element name="Inbound_MT" type="Inbound_DT" />
         <xsd:element name="Response_MT" type="Response_DT" />
         <xsd:complexType name="Inbound_DT">
            <xsd:sequence>
               <xsd:element maxOccurs="1" minOccurs="1" name="event_id" type="xsd:integer" />
               <xsd:element maxOccurs="1" minOccurs="1" name="event_name" type="xsd:string" />
               <xsd:element maxOccurs="1" minOccurs="1" name="event_date" type="xsd:string" />
               <xsd:element maxOccurs="1" minOccurs="1" name="employee_number" type="xsd:string" />
               <xsd:element maxOccurs="1" minOccurs="0" name="employee_first_name" type="xsd:string" />
               <xsd:element maxOccurs="1" minOccurs="0" name="employee_last_name" type="xsd:string" />
            </xsd:sequence>
         </xsd:complexType>
         <xsd:complexType name="Response_DT">
            <xsd:sequence>
               <xsd:element name="errorMsg" type="xsd:string" />
            </xsd:sequence>
         </xsd:complexType>
      </xsd:schema>
   </wsdl:types>
   <wsdl:message name="Inbound_MT">
      <wsdl:documentation />
      <wsdl:part element="p1:Inbound_MT" name="Inbound_MT" />
   </wsdl:message>
   <wsdl:message name="Response_MT">
      <wsdl:documentation />
      <wsdl:part element="p1:Response_MT" name="Response_MT" />
   </wsdl:message>
   <wsdl:portType name="ExampleService_Test">
      <wsdl:documentation />
      <wsdl:operation name="ExampleService_Test">
         <wsdl:documentation />
         <wsdl:input message="p1:Inbound_MT" />
         <wsdl:output message="p1:Response_MT" />
      </wsdl:operation>
   </wsdl:portType>
   <wsdl:binding name="ExampleService_TestBinding" type="p1:ExampleService_Test">
      <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
      <wsp:Policy>
         <wsp:PolicyReference URI="#BN__binding" />
      </wsp:Policy>
      <wsdl:operation name="ExampleService_Test">
         <soap:operation soapAction="https://example.com/ExampeService//ExampleService_Test" />
         <wsdl:input>
            <soap:body use="literal" />
         </wsdl:input>
         <wsdl:output>
            <soap:body use="literal" />
         </wsdl:output>
      </wsdl:operation>
   </wsdl:binding>
   <wsdl:service name="SOME_GREAT_SERVICE">
      <wsdl:documentation>WS_GS</wsdl:documentation>
      <wsdl:port binding="p1:ExampleService_TestBinding" name="ExampleService_Test">
         <soap:address location="https://endpointaddress/cxf/ExampleService_Test" />
      </wsdl:port>
   </wsdl:service>
   <wsp:UsingPolicy required="true" />
   <wsp:Policy wsu:Id="BN__binding">
      <wsp:ExactlyOne>
         <wsp:All>
            <sp:TransportBinding xmlns:sp="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702">
               <wsp:Policy>
                  <sp:TransportToken>
                     <wsp:Policy>
                        <sp:HttpsToken>
                           <wsp:Policy>
                              <wsp:ExactlyOne>
                                 <sp:HttpBasicAuthentication />
                                 <sp:RequireClientCertificate />
                              </wsp:ExactlyOne>
                           </wsp:Policy>
                        </sp:HttpsToken>
                     </wsp:Policy>
                  </sp:TransportToken>
                  <sp:AlgorithmSuite>
                     <wsp:Policy>
                        <wsp:ExactlyOne>
                           <sp:Basic256 />
                           <sp:Basic192 />
                           <sp:Basic128 />
                           <sp:TripleDes />
                           <sp:Basic256Rsa15 />
                           <sp:Basic192Rsa15 />
                           <sp:Basic128Rsa15 />
                           <sp:TripleDesRsa15 />
                           <sp:Basic256Sha256 />
                           <sp:Basic192Sha256 />
                           <sp:Basic128Sha256 />
                           <sp:TripleDesSha256 />
                           <sp:Basic256Sha256Rsa15 />
                           <sp:Basic192Sha256Rsa15 />
                           <sp:Basic128Sha256Rsa15 />
                           <sp:TripleDesSha256Rsa15 />
                        </wsp:ExactlyOne>
                     </wsp:Policy>
                  </sp:AlgorithmSuite>
                  <sp:Layout>
                     <wsp:Policy>
                        <sp:Strict />
                     </wsp:Policy>
                  </sp:Layout>
               </wsp:Policy>
            </sp:TransportBinding>
         </wsp:All>
      </wsp:ExactlyOne>
   </wsp:Policy>
</wsdl:definitions>

【问题讨论】:

    标签: c# wcf


    【解决方案1】:

    根据您的 wsdl,您的返回值似乎是 XML 而不是对象(我没有看到数据协定)。由于响应是 XML,因此您不能使用点符号直接引用响应变量。您需要首先将响应解析为您可以理解和检查的对象。

    您是否在 var 响应上设置了断点以查看它是否是包含 XML 的字符串?

    您可以这样做并立即使用 xpath 访问响应的节点:

    var response = new XmlDocument();
    response.LoadXml(client.ExampleService_Test(request));
    
    var errorMsg = response.SelectSingleNode("//Response_MT/errorMsg");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-12
      • 1970-01-01
      • 2020-08-13
      • 1970-01-01
      相关资源
      最近更新 更多