如何使用WCF服务参考来使用RPC样式的Web服务?

时间:2013-11-21 13:43:02

标签: c# wcf soap rpc soap-rpc-encoded

我正在编写一个调用Web服务来验证客户端的C#客户端。我使用添加服务引用将wsdl文件添加到我的项目中,并成功生成代理类。

我正在创建将使用的新对象实例:

authenticateAccessPortTypeClient client = new authenticateAccessPortTypeClient();
authDetails details = new authDetails();
returnResult result = new returnResult();

当需要对用户进行身份验证时,这是我的代码:

// This is details that needs to be passed in the header of the SOAP Envelope
details.key = "some key as string";
details.mode = "the mode as string";

// This is a parameter that is passed in the body of the SOAP Envelope
string memKey = "the member key as string";

result = client.authenticateAccess(details, memKey);

textBoxResult.Text = result.message;

我的肥皂反应如下:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:example="www.example.com">
   <soapenv:Header/>
   <soapenv:Body>
      <example:authenticateAccessResponse>
         <result>
            <message>some string</message>
         </result>
      </example:authenticateAccessResponse>
   </soapenv:Body>
</soapenv:Envelope>

returnResults在生成的代理类中看起来像这样:

public partial class returnResult : object, System.ComponentModel.INotifyPropertyChanged {

    private string messageField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=0)]
    public string message {
        get {
            return this.messageField;
        }
        set {
            this.messageField = value;
            this.RaisePropertyChanged("message");
        }
    }

    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName) {
        System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
        if ((propertyChanged != null)) {
            propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }
}

我继续收到错误:对象引用未设置为对象的实例而returnResult为null。

1 个答案:

答案 0 :(得分:1)

经过大量的谷歌搜索和调试,感谢评论这篇文章的用户,我解决了我的问题。

实际问题不是在客户端,而是在WSDL文件本身。我更改了WSDL文件的绑定样式以使用包含文字的文档。我的wsdl文件的类型结构已更改为以下内容:

<xsd:element name="nameOfType">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element minOccurs="1" maxOccurs="1" name="element1" type="xsd:string"/>
        <xsd:element minOccurs="1" maxOccurs="1" name="element2" type="xsd:string"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

complexType标签需要包装在另一个元素标签中以使用文字包装,并在第一个元素中设置name属性。

然后将消息标记更改为:

<wsdl:message name="messageName">
    <wsdl:part name="nameOfType" element="tns:nameOfType"/>
</wsdl:message>

*注意元素属性而不是类型属性

绑定看起来像这样:

<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="nameOfWebMethod">
  <soap:operation soapAction="nameOfWebMethod"/>
  <wsdl:input>
    <soap:body use="literal"
               encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
               namespace="urn:youNamespace:nameOfService"/>
    <soap:header message="tns:messageName" part="nameOfType" use="literal"/>
  </wsdl:input>
  <wsdl:output>
    <soap:body use="literal"
               encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
               namespace="urn:youNamespace:nameOfService"/>
  </wsdl:output>
</wsdl:operation>

感谢@John Saunders&amp; @Roy Dictus提供您的意见和建议。