在C#中调用具有复杂类型的SOAP API

时间:2015-04-10 01:41:34

标签: c# web-services soap wsdl

我是.Net的新手,所以如果我的问题定义不准确,我会提前道歉。

我正在尝试使用SOAP Web服务,而且我在为一个名为deliveryRoutingRequestEntries的元素构造复杂类型时遇到了问题。

我使用服务引用添加了WSDL,这是WSDL的一部分:

<wsdl:operation name="validateDeliveryAddress">
  <soap:operation soapAction="" style="document" />
  <wsdl:input name="validateDeliveryAddress">
    <soap:body use="literal" />
  </wsdl:input>
  <wsdl:output name="validateDeliveryAddressResponse">
    <soap:body use="literal" />
  </wsdl:output>
</wsdl:operation>

  <xs:complexType name="deliveryRoutingRequestEntries">
    <xs:sequence>
      <xs:element maxOccurs="unbounded" name="deliveryRoutingRequestEntry" type="tns:delRoutingRequestEntry" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="delRoutingRequestEntry">
    <xs:sequence>
      <xs:element minOccurs="0" name="addressValidationRequired" type="xs:boolean" />
      <xs:element name="customer" type="tns:customer" />
      <xs:element name="parcel" type="tns:parcel" />
      <xs:element minOccurs="0" name="services" type="tns:services" />
      <xs:element minOccurs="0" name="senderAddress" type="tns:senderAddress" />
      <xs:element minOccurs="0" name="productCode" type="xs:decimal" />
      <xs:element name="expectedDespatchDate" type="xs:date" />
      <xs:element minOccurs="0" name="requiredDate" type="xs:date" />
      <xs:element minOccurs="0" name="countryOfOrigin" type="xs:string" />
      <xs:element minOccurs="0" name="warehouseNo" type="xs:decimal" />
      <xs:element minOccurs="0" name="carrierCode" type="xs:string" />
      <xs:element minOccurs="0" name="deliveryMethod" type="xs:string" />
      <xs:element minOccurs="0" name="multiplePartsId" type="xs:string" />
    </xs:sequence>
  </xs:complexType>

这是我尝试调用validateDeliveryAddress:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HermesWebService;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        RoutingWebServiceClient objHermesWebService = new RoutingWebServiceClient();
        objHermesWebService.ClientCredentials.UserName.UserName = "abababab";
        objHermesWebService.ClientCredentials.UserName.Password = "abababab";

        deliveryRoutingRequest objRoutingRequest = new deliveryRoutingRequest();
        objRoutingRequest.clientId = "ABAB";
        objRoutingRequest.clientName = "ABAB";
        objRoutingRequest.userId = "ABABAB";
        objRoutingRequest.sourceOfRequest = "ABABAB";

        delRoutingRequestEntry objReqEntry = new delRoutingRequestEntry();

        customer objCust = new customer();
        objCust.customerReference1 = "12345";

        address objadr = new address();
        objadr.streetName = "7 Bridge Street";
        objadr.addressLine1 = "London Heathrow Airport";
        objadr.city = "London";
        objadr.region = "Hounslow";
        objadr.postCode = "ABA ABA";
        objadr.countryCode = "GB";

        parcel objpcl = new parcel();
        objpcl.weight = 100;
        objpcl.length = 0;
        objpcl.width = 0;
        objpcl.depth = 0;
        objpcl.girth = 0;
        objpcl.combinedDimension = 0;

        objCust.address = objadr;
        objReqEntry.customer = objCust;
        objReqEntry.parcel = objpcl;

        objRoutingRequest.deliveryRoutingRequestEntries = objReqEntry; (I receive an error here stating: Cannot implicitly convert type 'HermesWebService.delRoutingRequestEntry' to 'HermesWebService.delRoutingRequestEntry[]')

        var x = objHermesWebService.validateDeliveryAddress(objRoutingRequest);

    }
}

我遇到的问题是我无法将客户,地址和包裹添加到objRoutingRequest中,因此它可以包含在请求中。

希望这有一定道理,请告诉我是否需要其他信息。

1 个答案:

答案 0 :(得分:0)

(请在这里移动答案:)
您收到错误:objRoutingRequest.deliveryRoutingRequestEntries = objReqEntry;
你应该作为数组传递:
objRoutingRequest.deliveryRoutingRequestEntries = new [] {objReqEntry};