C# 从哪里开始使用WCF和wsdl文件?

C# 从哪里开始使用WCF和wsdl文件?,c#,wcf,web-services,wsdl,C#,Wcf,Web Services,Wsdl,我需要创建一个服务供客户使用/消费(我认为消费是正确的术语) 我是WCF和web服务的新手 我已经完成了大部分教程,并且了解了一些事情是如何进行的 现在我有了这个wsdl文件: <wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/"

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

我是
WCF
web服务的新手

我已经完成了大部分教程,并且了解了一些事情是如何进行的

现在我有了这个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>
实施方法:

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
结构一样)

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)看作是一个传统的类库。它只允许通过

调用和引用。请参阅有关如何创建WCF服务并使用客户端应用程序的文章:


  • 我得说你已经上路了!您可以使用任何您喜欢的设计模式来解决手头的问题。我通常将我的服务层设置为尽可能薄,并通过某种类型的接口(通常是一个外观)简单地抽象出底层逻辑,这并不是说在您的情况下这是正确的


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

    您可以使用XMLSpy来了解映射到WCF服务的WSDL和映射到数据契约的XSD的方式

    你说我从这里到哪里是什么意思?您想要实现什么?@Rajesh我需要创建一个服务供客户端使用/消费(我认为消费是正确的术语)。看起来您已经实现了wsdl中定义的服务,而不是让客户端使用它。@BenRobinson Ben,那么我应该怎么做?然后我完全走错了方向…看起来不错,开始填写方法实现。谢谢Giorgio的回复。我添加
    [OperationContract]字符串ChangePasswordResponse()的原因方法无效,因为它位于wsdl文件中。。。我是否可以假设我不需要wsdl文件中的所有方法,而是将您在回答中所述的一些方法组合起来?我组合这些方法不会破坏客户端代码吗?他们不是在寻找所有这些方法吗?嗨,Willem,wsdl是您服务发布的结果,所以您可以控制它!现在您正处于开发阶段,您可以随意更改它,客户机代码将需要更改以反映WSDL中的更改,但我想在ws未完全实现之前,这不应该是一个问题。
    
    [OperationContract]
    void ChangePassword(string serviceProviderGuid, string oldPassword, DateTime newPassword);
    
    [OperationContract]
    string ChangePasswordResponse();