如何使用输入 C# 对象生成 SOAPRequest 并将 SOAP 响应反序列化为 C# OutputResponse 对象

问题描述 投票:0回答:1

我正在努力实现这两件事:

  1. 从此输入 C# 对象生成 SoapRequest
  2. 将 Big SoapResponse 反序列化为 C# 对象

我已经在Postman中测试了Input SOAPRequest。我直接传递了 CustomerID、CustomerName 和 CustomerNumber 作为输入 SOAPRequest 的 part。

但实际上,我必须使用 C#Object 动态生成此 SOAPRequest,并将接收到的 SOAPResponse 反序列化到 C#Object 中。

输入客户对象:

public class CustomerRequest
{
    public int CustomerID { get; set; }
    public string CustomerName { get; set; }
    public string CustomerNum { get; set; }     

}

输入 SOAP 请求:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:bsvc="urn:com.workday/bsvc">
   <soapenv:Header>
      <bsvc:Workday_Common_Header>
         <bsvc:Include_Reference_Descriptors_In_Response>1</bsvc:Include_Reference_Descriptors_In_Response>
      </bsvc:Workday_Common_Header>
   </soapenv:Header>
   <soapenv:Body>
      <bsvc:Get_Customers_Request bsvc:version="v42.0">
         <bsvc:Request_References>
            <bsvc:Customer_Reference bsvc:Descriptor="?">
               <bsvc:ID bsvc:type="Customer_ID">100001</bsvc:ID>
               <bsvc:Customer_Name>ShellGroup</bsvc:Customer_Name>
               <bsvc:Customer_Num>S0001</bsvc:Customer_Num>
            </bsvc:Customer_Reference>
         </bsvc:Request_References>
         <bsvc:Response_Group>
            <bsvc:Include_Reference>1</bsvc:Include_Reference>
            <bsvc:Include_Customer_Data>1</bsvc:Include_Customer_Data>
            <bsvc:Include_Customer_Balance>1</bsvc:Include_Customer_Balance>
            <bsvc:Include_Customer_Activity_Detail>1</bsvc:Include_Customer_Activity_Detail>
         </bsvc:Response_Group>
      </bsvc:Get_Customers_Request>
   </soapenv:Body>
</soapenv:Envelope>

输出C#对象:

public class CustomerResponse
{
    public int CustomerID { get; set; }
    public string WID { get; set; }
    public string CustomerCategory { get; set; }
    public string PaymentType { get; set; }
    public decimal YTDSalesAmt { get; set; }
    public decimal TotalBalance { get; set; }

}

输出 SOAPResponse:

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
   <env:Body>
      <wd:Get_Customers_Response wd:version="v42.0" xmlns:wd="urn:com.workday/bsvc">
         <wd:Request_References>
            <wd:Customer_Reference wd:Descriptor="DRS Test Customer">
               <wd:ID wd:type="WID">c367dfff098d100072372cd7d2b10000</wd:ID>
               <wd:ID wd:type="Customer_Reference_ID">DRS_Test_Customer</wd:ID>
               <wd:ID wd:type="Customer_ID">100001</wd:ID>
            </wd:Customer_Reference>
         </wd:Request_References>
         <wd:Response_Group>
            <wd:Include_Reference>0</wd:Include_Reference>
            <wd:Include_Customer_Data>0</wd:Include_Customer_Data>
            <wd:Include_Customer_Balance>1</wd:Include_Customer_Balance>
            <wd:Include_Customer_Activity_Detail>0</wd:Include_Customer_Activity_Detail>
         </wd:Response_Group>
         <wd:Response_Results>
            <wd:Total_Results>1</wd:Total_Results>
            <wd:Total_Pages>1</wd:Total_Pages>
            <wd:Page_Results>1</wd:Page_Results>
            <wd:Page>1</wd:Page>
         </wd:Response_Results>
         <wd:Response_Data>
            <wd:Customer>
               <wd:Customer_Balance_Data>
                  <wd:Default_Currency_Reference wd:Descriptor="USD">
                     <wd:ID wd:type="WID">9e996ffdd3e14da0ba7275d5400bafd4</wd:ID>
                     <wd:ID wd:type="Currency_ID">USD</wd:ID>
                     <wd:ID wd:type="Currency_Numeric_Code">840</wd:ID>
                  </wd:Default_Currency_Reference>
                  <wd:YTD_Sales_Amount>235649.87</wd:YTD_Sales_Amount>
                  <wd:Total_Balance>-107961.63</wd:Total_Balance>
                  <wd:Last_12_Months_Sales_Amount>235649.87</wd:Last_12_Months_Sales_Amount>
                  <wd:Overdue_Balance>-107961.63</wd:Overdue_Balance>
               </wd:Customer_Balance_Data>
            </wd:Customer>
         </wd:Response_Data>
      </wd:Get_Customers_Response>
   </env:Body>
</env:Envelope>
c# asp.net .net asp.net-mvc asp.net-web-api
1个回答
0
投票

您只需将soap服务附加为VS中的连接服务即可。 右键单击您的项目 ==> 添加服务引用 ==> 选择“WCF Web 服务”并按照表单插入 WSDL url 或 WSDL xml 文件。 之后,您需要的所有类和方法(包括响应类模型)将由 VS 生成。

您还可以使用 HttpClient 在正文中传递 xml 请求,但生成类模型需要更多精力。 如果您仍然喜欢这种方式,VS Past Special 可以帮助您生成类,例如您可以使用 postaman 进行调用,然后使用“pastespecial”复制 xml 结果粘贴到新的类文件中。

© www.soinside.com 2019 - 2024. All rights reserved.