【问题标题】:Where to start with WCF and wsdl file? [closed]从哪里开始使用 WCF 和 wsdl 文件? [关闭]
【发布时间】:2012-02-20 16:10:23
【问题描述】:

我需要创建一个服务供客户使用/消费(我认为消费是正确的术语)。

我是WCFweb services 的新手。

我已经完成了大部分教程,并且对一切的工作原理有所了解。

现在我有了这个 wsdl 文件:

    <wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
                  xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" 
                  xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
                  xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" 
                  xmlns:tns="http://196.37.139.18/IntegrationTest/ShopriteIntegrationService" 
                  xmlns:s="http://www.w3.org/2001/XMLSchema" 
                  xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" 
                  xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" 
                  xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
                  targetNamespace="http://196.37.139.18/IntegrationTest/ShopriteIntegrationService">
  <wsdl:types>
    <s:schema elementFormDefault="qualified" 
              targetNamespace="http://196.37.139.18/IntegrationTest/ShopriteIntegrationService">
      <s:element name="ChangePassword">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="serviceProviderGuid" type="s:string"/>
            <s:element minOccurs="0" maxOccurs="1" name="oldPassword" type="s:string"/>
            <s:element minOccurs="0" maxOccurs="1" name="newPassword" type="s:string"/>
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:element name="ChangePasswordResponse">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="ChangePasswordResult">
              <s:complexType mixed="true">
                <s:sequence>
                  <s:any/>
                </s:sequence>
              </s:complexType>
            </s:element>
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:element name="GetCampaignSchedulePlacements">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="serviceProviderGuid" type="s:string"/>
            <s:element minOccurs="0" maxOccurs="1" name="password" type="s:string"/>
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:element name="GetCampaignSchedulePlacementsResponse">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="GetCampaignSchedulePlacementsResult">
              <s:complexType mixed="true">
                <s:sequence>
                  <s:any/>
                </s:sequence>
              </s:complexType>
            </s:element>
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:element name="ConfirmDownload">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="serviceProviderGuid" type="s:string"/>
            <s:element minOccurs="0" maxOccurs="1" name="password" type="s:string"/>
            <s:element minOccurs="1" maxOccurs="1" name="dateOfLatestRecordRecieved" type="s:dateTime"/>
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:element name="ConfirmDownloadResponse">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="ConfirmDownloadResult">
              <s:complexType mixed="true">
                <s:sequence>
                  <s:any/>
                </s:sequence>
              </s:complexType>
            </s:element>
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:element name="UploadCampaignSchedulePlacements">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="serviceProviderGuid" type="s:string"/>
            <s:element minOccurs="0" maxOccurs="1" name="password" type="s:string"/>
            <s:element minOccurs="0" maxOccurs="1" name="xml" type="s:string"/>
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:element name="UploadCampaignSchedulePlacementsResponse">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="UploadCampaignSchedulePlacementsResult">
              <s:complexType mixed="true">
                <s:sequence>
                  <s:any/>
                </s:sequence>
              </s:complexType>
            </s:element>
          </s:sequence>
        </s:complexType>
      </s:element>
    </s:schema>
  </wsdl:types>
  <wsdl:message name="ChangePasswordSoapIn">
    <wsdl:part name="parameters" element="tns:ChangePassword"/>
  </wsdl:message>
  <wsdl:message name="ChangePasswordSoapOut">
    <wsdl:part name="parameters" element="tns:ChangePasswordResponse"/>
  </wsdl:message>
  <wsdl:message name="GetCampaignSchedulePlacementsSoapIn">
    <wsdl:part name="parameters" element="tns:GetCampaignSchedulePlacements"/>
  </wsdl:message>
  <wsdl:message name="GetCampaignSchedulePlacementsSoapOut">
    <wsdl:part name="parameters" element="tns:GetCampaignSchedulePlacementsResponse"/>
  </wsdl:message>
  <wsdl:message name="ConfirmDownloadSoapIn">
    <wsdl:part name="parameters" element="tns:ConfirmDownload"/>
  </wsdl:message>
  <wsdl:message name="ConfirmDownloadSoapOut">
    <wsdl:part name="parameters" element="tns:ConfirmDownloadResponse"/>
  </wsdl:message>
  <wsdl:message name="UploadCampaignSchedulePlacementsSoapIn">
    <wsdl:part name="parameters" element="tns:UploadCampaignSchedulePlacements"/>
  </wsdl:message>
  <wsdl:message name="UploadCampaignSchedulePlacementsSoapOut">
    <wsdl:part name="parameters" element="tns:UploadCampaignSchedulePlacementsResponse"/>
  </wsdl:message>
  <wsdl:portType name="ShopriteIntegrationTestoap">
    <wsdl:operation name="ChangePassword">
      <wsdl:input message="tns:ChangePasswordSoapIn"/>
      <wsdl:output message="tns:ChangePasswordSoapOut"/>
    </wsdl:operation>
    <wsdl:operation name="GetCampaignSchedulePlacements">
      <wsdl:input message="tns:GetCampaignSchedulePlacementsSoapIn"/>
      <wsdl:output message="tns:GetCampaignSchedulePlacementsSoapOut"/>
    </wsdl:operation>
    <wsdl:operation name="ConfirmDownload">
      <wsdl:input message="tns:ConfirmDownloadSoapIn"/>
      <wsdl:output message="tns:ConfirmDownloadSoapOut"/>
    </wsdl:operation>
    <wsdl:operation name="UploadCampaignSchedulePlacements">
      <wsdl:input message="tns:UploadCampaignSchedulePlacementsSoapIn"/>
      <wsdl:output message="tns:UploadCampaignSchedulePlacementsSoapOut"/>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="ShopriteIntegrationTestoap" type="tns:ShopriteIntegrationTestoap">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="ChangePassword">
      <soap:operation soapAction="http://196.37.139.18/IntegrationTest/ShopriteIntegrationService/ChangePassword" style="document"/>
      <wsdl:input>
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="GetCampaignSchedulePlacements">
      <soap:operation soapAction="http://196.37.139.18/IntegrationTest/ShopriteIntegrationService/GetCampaignSchedulePlacements" style="document"/>
      <wsdl:input>
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="ConfirmDownload">
      <soap:operation soapAction="http://196.37.139.18/IntegrationTest/ShopriteIntegrationService/ConfirmDownload" style="document"/>
      <wsdl:input>
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="UploadCampaignSchedulePlacements">
      <soap:operation soapAction="http://196.37.139.18/IntegrationTest/ShopriteIntegrationService/UploadCampaignSchedulePlacements" style="document"/>
      <wsdl:input>
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:binding name="ShopriteIntegrationServiceSoap12" type="tns:ShopriteIntegrationServiceSoap">
    <soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="ChangePassword">
      <soap12:operation soapAction="http://196.37.139.18/IntegrationTest/ShopriteIntegrationService/ChangePassword" style="document"/>
      <wsdl:input>
        <soap12:body use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap12:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="GetCampaignSchedulePlacements">
      <soap12:operation soapAction="http://196.37.139.18/IntegrationTest/ShopriteIntegrationService/GetCampaignSchedulePlacements" style="document"/>
      <wsdl:input>
        <soap12:body use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap12:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="ConfirmDownload">
      <soap12:operation soapAction="http://196.37.139.18/IntegrationTest/ShopriteIntegrationService/ConfirmDownload" style="document"/>
      <wsdl:input>
        <soap12:body use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap12:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="UploadCampaignSchedulePlacements">
      <soap12:operation soapAction="http://196.37.139.18/IntegrationTest/ShopriteIntegrationService/UploadCampaignSchedulePlacements" style="document"/>
      <wsdl:input>
        <soap12:body use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap12:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="ShopriteIntegrationService">
    <wsdl:port name="ShopriteIntegrationServiceSoap" binding="tns:ShopriteIntegrationTestoap">
      <soap:address location="http://jhbweb01/IntegrationTest/ShopriteIntegrationService.asmx"/>
    </wsdl:port>
    <wsdl:port name="ShopriteIntegrationServiceSoap12" binding="tns:ShopriteIntegrationServiceSoap12">
      <soap12:address location="http://jhbweb01/IntegrationTest/ShopriteIntegrationService.asmx"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

这就是我得到的:

[ServiceContract]
public interface IShopriteService
{
    [OperationContract]
    void ChangePassword(string serviceProviderGuid, string oldPassword, DateTime newPassword);

    [OperationContract]
    string ChangePasswordResponse();

    [OperationContract]
    void GetCampaignSchedulePlacements(string serviceProviderGuid, string password);

    [OperationContract]
    string GetCampaignSchedulePlacementsResponse();

    [OperationContract]
    void ConfirmDownload(string serviceProviderGuid, string password, DateTime dateOfLatestRecordRecieved);

    [OperationContract]
    string ConfirmDownloadResponse();

    [OperationContract]
    void UploadCampaignSchedulePlacements(string serviceProviderGuid, string password, string xml);

    [OperationContract]
    string UploadCampaignSchedulePlacementsResponse();
}

实现的方法:

public class ShopriteService : IShopriteService
    {
    public void ChangePassword(string serviceProviderGuid, string oldPassword, DateTime newPassword)
    {
        throw new NotImplementedException();
    }

    public string ChangePasswordResponse()
    {
        throw new NotImplementedException();
    }

    public void GetCampaignSchedulePlacements(string serviceProviderGuid, string password)
    {
        throw new NotImplementedException();
    }

    public string GetCampaignSchedulePlacementsResponse()
    {
        throw new NotImplementedException();
    }

    public void ConfirmDownload(string serviceProviderGuid, string password, DateTime dateOfLatestRecordRecieved)
    {
        throw new NotImplementedException();
    }

    public string ConfirmDownloadResponse()
    {
        throw new NotImplementedException();
    }

    public void UploadCampaignSchedulePlacements(string serviceProviderGuid, string password, string xml)
    {
        throw new NotImplementedException();
    }

    public string UploadCampaignSchedulePlacementsResponse()
    {
        throw new NotImplementedException();
    }
}

我什至不知道我是否走在正确的轨道上......

解释一下,wsdl 文件中发生了什么。

从这里到哪里去?

我可以使用某种结构吗? (如MVVM 结构)

【问题讨论】:

  • 我从这里去哪里是什么意思?你想达到什么目标?
  • @Rajesh 我需要创建一个服务供客户使用/消费(我认为消费是正确的术语)。
  • 看起来您已经实现了 wsdl 中定义的服务,而不是使用它的客户端。
  • @BenRobinson Ben,那我该怎么办?那我就完全走错了路……
  • 看起来不错,开始填写方法实现。

标签: c# wcf web-services wsdl


【解决方案1】:

请参阅有关如何创建 WCF 服务并将其与客户端应用程序一起使用的文章:

  1. Create and consume WCF service

  2. Consume a WCF Service

  3. Hosting and consuming WCF Service

  4. Video to create and consume WCF Service

【讨论】:

    【解决方案2】:

    WSDL 只不过是对您的服务的描述,它允许客户端将其作为 Web 服务使用。 所以:wsdl 反映了你的接口-> 你的接口是由一个类实现的。最后一步是为您的接口方法编写逻辑(即:ChangePassword 方法)。 另外,我觉得 ServiceContract 出了点问题,让我们以 ChangePassword 方法为例: 因为该方法将返回一个字符串,所以您只需定义以下内容:

    [OperationContract]
    string ChangePassword(string serviceProviderGuid, string oldPassword, DateTime newPassword);
    

    而不是

    [OperationContract]
    void ChangePassword(string serviceProviderGuid, string oldPassword, DateTime newPassword);
    
    [OperationContract]
    string ChangePasswordResponse();
    

    所以基本上将您的 WCF(或 Ws)视为一个传统的类库。它只允许在HTTP/SOAP上被调用和引用

    【讨论】:

    • 感谢乔治的回复。我添加 [OperationContract] string ChangePasswordResponse(); 方法的原因是因为它在 wsdl 文件中......我可以假设我不需要 wsdl 文件中的所有方法并结合一些方法,就像你在回答中所说的那样?我会结合这些方法不会破坏客户代码吗?他们不会寻找所有这些方法吗?
    • 嗨,Willem,wsdl 是您的服务发布的结果,所以您可以控制!现在您处于开发阶段,请随意更改它,客户端代码需要更改以反映 WSDL 中的更改,但我想这应该不是问题,直到 ws 是没有完全实现。
    【解决方案3】:

    我会说你正在路上!您可以使用任何您喜欢的设计模式来解决手头的问题。我通常将我的服务层设置为尽可能薄,并通过某种接口简单地抽象出底层逻辑,通常是 Façade,这并不是说这在你的情况下是正确的。

    对于 WSDL,它本质上是通过描述一组与您的服务公开的类型和端点相关的抽象来定义您的服务。您可以在W3C 阅读更多详细信息。通过 WSDL 客户端可以获取有关您使用它所需的服务的信息。

    【讨论】:

      【解决方案4】:

      您可以使用 XML spy 来了解 WSDL 如何映射到 WCF 服务以及 XSD 如何映射到数据协定 http://www.altova.com/simpledownload2c.html?gclid=CJeFgKGEr64CFSgntAod9m4oSA

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-09-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-07
        • 2011-03-23
        • 2010-12-25
        相关资源
        最近更新 更多