如何为导入的WSDL的方法参数赋值

时间:2013-08-20 14:25:17

标签: c# asp.net soap wsdl

在C#2008应用程序中,我收到错误消息“对象引用未设置为对象的实例”。 导致此问题发生的代码行如下:

insert.forminputdata.freeformcontrols.PrimaryAccID.Value = "24";

我在wsdl中有一个方法,它接受输入请求如下。

<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP:Body>
    <CreateTransaction xmlns="http://schemas.cordys.com/default">
      <ns0:forminputdata xmlns:ns0="http://schemas.cordys.com/1.0/xforms/processapi">
        <ns0:freeformcontrols>
          <ns0:PrimaryAccID display_name="Primary Acc ID">PARAMETER</ns0:PrimaryAccID>
          <ns0:SecondaryAccID display_name="Secondary Acc ID">PARAMETER</ns0:SecondaryAccID>
        </ns0:freeformcontrols>
      </ns0:forminputdata>
    </CreateTransaction>
  </SOAP:Body>

完整的wsdl是

<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:tns="http://schemas.cordys.com/default" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns="http://schemas.xmlsoap.org/wsdl/" name="ExecuteBPM.ExecuteTransaction" targetNamespace="http://schemas.cordys.com/default" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
  <wsdl:types>
    <xsd:schema xmlns:inst="http://schemas.cordys.com/bpm/instance/1.0" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns1="http://schemas.cordys.com/1.0/xforms/processapi" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://schemas.cordys.com/default" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <xsd:import namespace="http://schemas.cordys.com/1.0/xforms/processapi" />
      <xsd:import namespace="http://schemas.cordys.com/bpm/instance/1.0" />
      <xsd:import namespace="http://schemas.cordys.com/1.0/xforms/processapi" />
      <xsd:element name="CreateTransaction">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element ref="tns1:forminputdata" />
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="CreateTransactionResponse">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="data">
              <xsd:complexType>
                <xsd:all>
                  <xsd:element name="instance_id">
                    <xsd:annotation>
                      <xsd:documentation>This output parameter contains the unique instance identifier for the business-process.</xsd:documentation>
                    </xsd:annotation>
                  </xsd:element>
                  <xsd:element minOccurs="0" name="PAGEFLOWDATA">
                    <xsd:annotation>
                      <xsd:documentation>This optional output parameter contains the response message from the business-process and this is available only in case of page flow processes.</xsd:documentation>
                    </xsd:annotation>
                    <xsd:complexType>
                      <xsd:sequence>
                        <xsd:any minOccurs="0" maxOccurs="unbounded" processContents="skip" />
                      </xsd:sequence>
                    </xsd:complexType>
                  </xsd:element>
                </xsd:all>
              </xsd:complexType>
            </xsd:element>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    </xsd:schema>
    <xsd:schema xmlns:inst="http://schemas.cordys.com/bpm/instance/1.0" xmlns="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://schemas.cordys.com/bpm/instance/1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <xsd:import namespace="http://schemas.cordys.com/1.0/xforms/processapi" />
      <xsd:element name="headerInput">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="target" type="xsd:string" />
            <xsd:element name="source">
              <xsd:complexType>
                <xsd:attribute name="type" type="xsd:string" />
              </xsd:complexType>
            </xsd:element>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="bpm">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="instance_id" type="xsd:string" />
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    </xsd:schema>
    <xsd:schema xmlns:inst="http://schemas.cordys.com/bpm/instance/1.0" xmlns="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://schemas.cordys.com/1.0/xforms/processapi" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <xsd:element name="forminputdata">
        <xsd:complexType>
          <xsd:all>
            <xsd:element name="freeformcontrols">
              <xsd:complexType>
                <xsd:all>
                  <xsd:element name="PrimaryAccID">
                    <xsd:complexType>
                      <xsd:simpleContent>
                        <xsd:extension base="xsd:string">
                          <xsd:attribute default="Primary Acc ID" name="display_name" type="xsd:string" use="optional" />
                        </xsd:extension>
                      </xsd:simpleContent>
                    </xsd:complexType>
                  </xsd:element>
                  <xsd:element name="SecondaryAccID">
                    <xsd:complexType>
                      <xsd:simpleContent>
                        <xsd:extension base="xsd:string">
                          <xsd:attribute default="Secondary Acc ID" name="display_name" type="xsd:string" use="optional" />
                        </xsd:extension>
                      </xsd:simpleContent>
                    </xsd:complexType>
                  </xsd:element>
                </xsd:all>
              </xsd:complexType>
            </xsd:element>
          </xsd:all>
        </xsd:complexType>
      </xsd:element>
    </xsd:schema>
  </wsdl:types>
  <wsdl:message xmlns:inst="http://schemas.cordys.com/bpm/instance/1.0" name="CreateTransactionInput">
    <wsdl:part name="body" element="tns:CreateTransaction" />
  </wsdl:message>
  <wsdl:message xmlns:inst="http://schemas.cordys.com/bpm/instance/1.0" name="CreateTransactionOutput">
    <wsdl:part name="body" element="tns:CreateTransactionResponse" />
  </wsdl:message>
  <wsdl:message xmlns:inst="http://schemas.cordys.com/bpm/instance/1.0" name="HeaderOutput">
    <wsdl:part name="BPMHeaderOutput" element="inst:bpm" />
  </wsdl:message>
  <wsdl:portType xmlns:inst="http://schemas.cordys.com/bpm/instance/1.0" name="ExecuteTransaction">
    <wsdl:operation name="CreateTransaction">
      <wsdl:input message="tns:CreateTransactionInput" />
      <wsdl:output message="tns:CreateTransactionOutput" />
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding xmlns:inst="http://schemas.cordys.com/bpm/instance/1.0" name="ExecuteTransaction" type="tns:ExecuteTransaction">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
    <wsdl:operation name="CreateTransaction">
      <soap:operation soapAction="" style="document" />
      <wsdl:input>
        <soap:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal" />
        <soap:header message="tns:HeaderOutput" part="BPMHeaderOutput" use="literal" />
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service xmlns:inst="http://schemas.cordys.com/bpm/instance/1.0" name="ExecuteTransactionService">
    <wsdl:port name="ExecuteTransactionPort" binding="tns:ExecuteTransaction">
      <soap:address location="http://srv-ind-dvm28aw/cordys/com.eibus.web.soap.Gateway.wcp?organization=o=ewm,cn=cordys,cn=EWM_CU6,o=vanenburg.com&amp;SAMLart=MDFgZEgICDQ+u+3viuskQtM5/APNP1YhIhFVVVo/fvRCxKezFVk1Ltvx" />
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

在.cs文件中,我试图将这些参数设置为

using System;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Data;
using System.Xml;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
    public Service()
    {
    }
    srv_ind_dvm28aw.ExecuteTransactionService soapClient = new srv_ind_dvm28aw.ExecuteTransactionService();
    [WebMethod]
    public void Transaction()
    {
        srv_ind_dvm28aw.CreateTransaction insert = new srv_ind_dvm28aw.CreateTransaction();
        srv_ind_dvm28aw.CreateTransactionResponse response = new srv_ind_dvm28aw.CreateTransactionResponse();
        try
        {
             insert.forminputdata.freeformcontrols.PrimaryAccID.Value = "24";
             insert.forminputdata.freeformcontrols.SecondaryAccID.Value = "22";
            response = soapClient.CreateTransaction(insert);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }

}

不使用set param行,我可以成功调用该方法(即没有以下几行)

 insert.forminputdata.freeformcontrols.PrimaryAccID.Value = "24";
 insert.forminputdata.freeformcontrols.SecondaryAccID.Value = "22";

这是我用肥皂方法设置参数的方法是正确的吗?请指导我哪里出错了。 insert.forminputdata的确切错误抛出为null。

1 个答案:

答案 0 :(得分:1)

您需要初始化对象。它应该是这样的:

insert.forminputdata = new forminputdata();
insert.forminputdata.freeformcontrols = new forminputdataFreeformcontrols();
insert.forminputdata.freeformcontrols.PrimaryAccID = new forminputdataFreeformcontrolsPrimaryAccID();
insert.forminputdata.freeformcontrols.PrimaryAccID.Value = "24";