zoukankan      html  css  js  c++  java
  • WCF入门示例二:承载于IIS中的WCF示例程序

    上一个示例程序演示承载于托管代码中的WCF服务,这个示例将演示承载于IIS中的WCF服务.跟上一个示例一样我们先从创建一个WCF服务程序开始.为了方便演示,我们这个项目就在第一个项目的基础上创建.

    这里先提供完整的项目图做参考

    1.用VS2010打开WcfServiceDemo, 右键WcfServiceDemo解决方案-->Add-->New Project.安照下面的配置填写:

    因为我们的WCF服务承载于IIS中,可以删除program.cs文件,并在IISHostedCalService项目属性中,将Console Application改成 Class Library.因为在删掉program.cs文件后,项目中没有main方法,如果不改成 Class Library,编译会报错.

    修改方法:右键IISHostedCalService项目-->Property

    2.引入System.ServiceModel.dll. 因为在下面创建服务协定的时候需要用到此类库.右键单击References-->Add References. 在弹出的对话框中,按照下面选择,点击OK按钮.

    3.在应用程序目录中创建 App_Code 子目录。

    4.现在创建一个服务协定,也就是新建一个接口.为接口应用ServiceContractAttribute特性, 为接口中的4个方法应用OperationContractAttribute特性.

    这也是WCF协定于普通接口不同之处.带有[ServiceContract]特性的接口说明是WCF公开的协议;带有[OperationContract]特性的方法说明是协议公开的方法. 完整代码如下:

    在 App_Code 子目录中创建名为 ICalculator.cs 的代码文件

    ICalculator.cs

    View Code
    using System;
    using System.ServiceModel;
    
    namespace Microsoft.ServiceModel.Samples
    {
        [ServiceContract]
        public interface ICalculator
        {
            [OperationContract]
            double Add(double n1, double n2);
            [OperationContract]
            double Subtract(double n1, double n2);
            [OperationContract]
            double Multiply(double n1, double n2);
            [OperationContract]
            double Divide(double n1, double n2);
        }
    }

    5.实现服务协定,也就是创建一个类,并实现接口的方法.

    这个类需要继承于ICalculator接口,并实现ICalculator接口的方法.这个类的方法会由WCF服务提供给客户端调用.

    在 App_Code 子目录中创建名为 CalculatorService.cs 的代码文件

     CalculatorService.cs

    View Code
    using System;
    using System.ServiceModel;
    
    namespace Microsoft.ServiceModel.Samples
    {
        public class CalculatorService : ICalculator
        {
            public double Add(double n1, double n2)
            {
                return n1 + n2;
            }
            public double Subtract(double n1, double n2)
            {
                return n1 - n2;
            }
            public double Multiply(double n1, double n2)
            {
                return n1 * n2;
            }
            public double Divide(double n1, double n2)
            {
                return n1 / n2;
            }
        }
    }

    到这一步,服务器端的代码编程已经完成; 这时需要新建两个文件,service.svc,Web.config;我们在浏览器中访问WCF服务时,需要service.svc作为入口,Web.config是IIS的标准配置文件.

    6.在项目中添加servcie.svc文件.

    servcie.svc

    <%@ServiceHost language=c# Debug="true" Service="Microsoft.ServiceModel.Samples.CalculatorService"%>

    7.在项目中添加Web.config文件.

    Webconfig

    View Code
    <?xml version="1.0"?>
    <configuration>
      <system.serviceModel>
        <services>
          <service name="Microsoft.ServiceModel.Samples.CalculatorService" behaviorConfiguration="MyServiceTypeBehaviors" >
            <endpoint address=""
                      binding="wsHttpBinding"
                      contract="Microsoft.ServiceModel.Samples.ICalculator" />
            <!--<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>-->
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior name="MyServiceTypeBehaviors"  >
              <serviceMetadata httpGetEnabled="true"  />
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>
      <system.web>
        <compilation targetFramework="4.0"/>
        <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
      </system.web>
    </configuration>

    到这一步,已经完成了所有代码和配置文件,下面需要做的就是把WCF程序配置到IIS就可以了.

    8.将IISHostedCalService配置到IIS

    打开IIS-->右键网站-->添加网站. 在弹出的对话框中按照下面的填写

    我本机的IIS版本号是7.5,在创建一个新网站之后,IIS会自动帮你添加一个默认的应用程序池,需要注意的是要确定应用程序池的.NET FrameWork 也是4.0,不然可能会报错.

    到此,WCF服务程序已经配置好,我们看看运行效果:

    打开IIS-->网站-->WcfCalService-->内容视图-->右键service.svc-->浏览.可以看熟悉的WCF service说明页面.

    点击URL进去看详细的调用信息.

    View Code
    <?xml version="1.0" encoding="UTF-8"?>
    -<wsdl:definitions xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:tns="http://tempuri.org/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://tempuri.org/" name="CalculatorService">-<wsp:Policy wsu:Id="WSHttpBinding_ICalculator_policy">-<wsp:ExactlyOne>-<wsp:All>-<sp:SymmetricBinding xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">-<wsp:Policy>-<sp:ProtectionToken>-<wsp:Policy>-<sp:SecureConversationToken sp:IncludeToken="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/AlwaysToRecipient">-<wsp:Policy><sp:RequireDerivedKeys/>-<sp:BootstrapPolicy>-<wsp:Policy>-<sp:SignedParts><sp:Body/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="To"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="From"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="FaultTo"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="ReplyTo"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="MessageID"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="RelatesTo"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="Action"/></sp:SignedParts>-<sp:EncryptedParts><sp:Body/></sp:EncryptedParts>-<sp:SymmetricBinding>-<wsp:Policy>-<sp:ProtectionToken>-<wsp:Policy>-<sp:SpnegoContextToken sp:IncludeToken="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/AlwaysToRecipient">-<wsp:Policy><sp:RequireDerivedKeys/></wsp:Policy></sp:SpnegoContextToken></wsp:Policy></sp:ProtectionToken>-<sp:AlgorithmSuite>-<wsp:Policy><sp:Basic256/></wsp:Policy></sp:AlgorithmSuite>-<sp:Layout>-<wsp:Policy><sp:Strict/></wsp:Policy></sp:Layout><sp:IncludeTimestamp/><sp:EncryptSignature/><sp:OnlySignEntireHeadersAndBody/></wsp:Policy></sp:SymmetricBinding>-<sp:Wss11><wsp:Policy/></sp:Wss11>-<sp:Trust10>-<wsp:Policy><sp:MustSupportIssuedTokens/><sp:RequireClientEntropy/><sp:RequireServerEntropy/></wsp:Policy></sp:Trust10></wsp:Policy></sp:BootstrapPolicy></wsp:Policy></sp:SecureConversationToken></wsp:Policy></sp:ProtectionToken>-<sp:AlgorithmSuite>-<wsp:Policy><sp:Basic256/></wsp:Policy></sp:AlgorithmSuite>-<sp:Layout>-<wsp:Policy><sp:Strict/></wsp:Policy></sp:Layout><sp:IncludeTimestamp/><sp:EncryptSignature/><sp:OnlySignEntireHeadersAndBody/></wsp:Policy></sp:SymmetricBinding>-<sp:Wss11 xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy"><wsp:Policy/></sp:Wss11>-<sp:Trust10 xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">-<wsp:Policy><sp:MustSupportIssuedTokens/><sp:RequireClientEntropy/><sp:RequireServerEntropy/></wsp:Policy></sp:Trust10><wsaw:UsingAddressing/></wsp:All></wsp:ExactlyOne></wsp:Policy>-<wsp:Policy wsu:Id="WSHttpBinding_ICalculator_Add_Input_policy">-<wsp:ExactlyOne>-<wsp:All>-<sp:SignedParts xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy"><sp:Body/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="To"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="From"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="FaultTo"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="ReplyTo"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="MessageID"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="RelatesTo"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="Action"/></sp:SignedParts>-<sp:EncryptedParts xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy"><sp:Body/></sp:EncryptedParts></wsp:All></wsp:ExactlyOne></wsp:Policy>-<wsp:Policy wsu:Id="WSHttpBinding_ICalculator_Add_output_policy">-<wsp:ExactlyOne>-<wsp:All>-<sp:SignedParts xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy"><sp:Body/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="To"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="From"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="FaultTo"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="ReplyTo"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="MessageID"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="RelatesTo"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="Action"/></sp:SignedParts>-<sp:EncryptedParts xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy"><sp:Body/></sp:EncryptedParts></wsp:All></wsp:ExactlyOne></wsp:Policy>-<wsp:Policy wsu:Id="WSHttpBinding_ICalculator_Subtract_Input_policy">-<wsp:ExactlyOne>-<wsp:All>-<sp:SignedParts xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy"><sp:Body/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="To"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="From"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="FaultTo"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="ReplyTo"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="MessageID"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="RelatesTo"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="Action"/></sp:SignedParts>-<sp:EncryptedParts xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy"><sp:Body/></sp:EncryptedParts></wsp:All></wsp:ExactlyOne></wsp:Policy>-<wsp:Policy wsu:Id="WSHttpBinding_ICalculator_Subtract_output_policy">-<wsp:ExactlyOne>-<wsp:All>-<sp:SignedParts xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy"><sp:Body/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="To"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="From"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="FaultTo"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="ReplyTo"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="MessageID"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="RelatesTo"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="Action"/></sp:SignedParts>-<sp:EncryptedParts xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy"><sp:Body/></sp:EncryptedParts></wsp:All></wsp:ExactlyOne></wsp:Policy>-<wsp:Policy wsu:Id="WSHttpBinding_ICalculator_Multiply_Input_policy">-<wsp:ExactlyOne>-<wsp:All>-<sp:SignedParts xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy"><sp:Body/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="To"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="From"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="FaultTo"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="ReplyTo"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="MessageID"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="RelatesTo"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="Action"/></sp:SignedParts>-<sp:EncryptedParts xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy"><sp:Body/></sp:EncryptedParts></wsp:All></wsp:ExactlyOne></wsp:Policy>-<wsp:Policy wsu:Id="WSHttpBinding_ICalculator_Multiply_output_policy">-<wsp:ExactlyOne>-<wsp:All>-<sp:SignedParts xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy"><sp:Body/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="To"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="From"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="FaultTo"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="ReplyTo"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="MessageID"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="RelatesTo"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="Action"/></sp:SignedParts>-<sp:EncryptedParts xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy"><sp:Body/></sp:EncryptedParts></wsp:All></wsp:ExactlyOne></wsp:Policy>-<wsp:Policy wsu:Id="WSHttpBinding_ICalculator_Divide_Input_policy">-<wsp:ExactlyOne>-<wsp:All>-<sp:SignedParts xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy"><sp:Body/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="To"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="From"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="FaultTo"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="ReplyTo"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="MessageID"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="RelatesTo"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="Action"/></sp:SignedParts>-<sp:EncryptedParts xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy"><sp:Body/></sp:EncryptedParts></wsp:All></wsp:ExactlyOne></wsp:Policy>-<wsp:Policy wsu:Id="WSHttpBinding_ICalculator_Divide_output_policy">-<wsp:ExactlyOne>-<wsp:All>-<sp:SignedParts xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy"><sp:Body/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="To"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="From"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="FaultTo"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="ReplyTo"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="MessageID"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="RelatesTo"/><sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="Action"/></sp:SignedParts>-<sp:EncryptedParts xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy"><sp:Body/></sp:EncryptedParts></wsp:All></wsp:ExactlyOne></wsp:Policy>-<wsdl:types>-<xsd:schema targetNamespace="http://tempuri.org/Imports"><xsd:import namespace="http://tempuri.org/" schemaLocation="http://szx-station168:8001/service.svc?xsd=xsd0"/><xsd:import namespace="http://schemas.microsoft.com/2003/10/Serialization/" schemaLocation="http://szx-station168:8001/service.svc?xsd=xsd1"/></xsd:schema></wsdl:types>-<wsdl:message name="ICalculator_Add_InputMessage"><wsdl:part name="parameters" element="tns:Add"/></wsdl:message>-<wsdl:message name="ICalculator_Add_OutputMessage"><wsdl:part name="parameters" element="tns:AddResponse"/></wsdl:message>-<wsdl:message name="ICalculator_Subtract_InputMessage"><wsdl:part name="parameters" element="tns:Subtract"/></wsdl:message>-<wsdl:message name="ICalculator_Subtract_OutputMessage"><wsdl:part name="parameters" element="tns:SubtractResponse"/></wsdl:message>-<wsdl:message name="ICalculator_Multiply_InputMessage"><wsdl:part name="parameters" element="tns:Multiply"/></wsdl:message>-<wsdl:message name="ICalculator_Multiply_OutputMessage"><wsdl:part name="parameters" element="tns:MultiplyResponse"/></wsdl:message>-<wsdl:message name="ICalculator_Divide_InputMessage"><wsdl:part name="parameters" element="tns:Divide"/></wsdl:message>-<wsdl:message name="ICalculator_Divide_OutputMessage"><wsdl:part name="parameters" element="tns:DivideResponse"/></wsdl:message>-<wsdl:portType name="ICalculator">-<wsdl:operation name="Add"><wsdl:input message="tns:ICalculator_Add_InputMessage" wsaw:Action="http://tempuri.org/ICalculator/Add"/><wsdl:output message="tns:ICalculator_Add_OutputMessage" wsaw:Action="http://tempuri.org/ICalculator/AddResponse"/></wsdl:operation>-<wsdl:operation name="Subtract"><wsdl:input message="tns:ICalculator_Subtract_InputMessage" wsaw:Action="http://tempuri.org/ICalculator/Subtract"/><wsdl:output message="tns:ICalculator_Subtract_OutputMessage" wsaw:Action="http://tempuri.org/ICalculator/SubtractResponse"/></wsdl:operation>-<wsdl:operation name="Multiply"><wsdl:input message="tns:ICalculator_Multiply_InputMessage" wsaw:Action="http://tempuri.org/ICalculator/Multiply"/><wsdl:output message="tns:ICalculator_Multiply_OutputMessage" wsaw:Action="http://tempuri.org/ICalculator/MultiplyResponse"/></wsdl:operation>-<wsdl:operation name="Divide"><wsdl:input message="tns:ICalculator_Divide_InputMessage" wsaw:Action="http://tempuri.org/ICalculator/Divide"/><wsdl:output message="tns:ICalculator_Divide_OutputMessage" wsaw:Action="http://tempuri.org/ICalculator/DivideResponse"/></wsdl:operation></wsdl:portType>-<wsdl:binding name="WSHttpBinding_ICalculator" type="tns:ICalculator"><wsp:PolicyReference URI="#WSHttpBinding_ICalculator_policy"/><soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/>-<wsdl:operation name="Add"><soap12:operation style="document" soapAction="http://tempuri.org/ICalculator/Add"/>-<wsdl:input><wsp:PolicyReference URI="#WSHttpBinding_ICalculator_Add_Input_policy"/><soap12:body use="literal"/></wsdl:input>-<wsdl:output><wsp:PolicyReference URI="#WSHttpBinding_ICalculator_Add_output_policy"/><soap12:body use="literal"/></wsdl:output></wsdl:operation>-<wsdl:operation name="Subtract"><soap12:operation style="document" soapAction="http://tempuri.org/ICalculator/Subtract"/>-<wsdl:input><wsp:PolicyReference URI="#WSHttpBinding_ICalculator_Subtract_Input_policy"/><soap12:body use="literal"/></wsdl:input>-<wsdl:output><wsp:PolicyReference URI="#WSHttpBinding_ICalculator_Subtract_output_policy"/><soap12:body use="literal"/></wsdl:output></wsdl:operation>-<wsdl:operation name="Multiply"><soap12:operation style="document" soapAction="http://tempuri.org/ICalculator/Multiply"/>-<wsdl:input><wsp:PolicyReference URI="#WSHttpBinding_ICalculator_Multiply_Input_policy"/><soap12:body use="literal"/></wsdl:input>-<wsdl:output><wsp:PolicyReference URI="#WSHttpBinding_ICalculator_Multiply_output_policy"/><soap12:body use="literal"/></wsdl:output></wsdl:operation>-<wsdl:operation name="Divide"><soap12:operation style="document" soapAction="http://tempuri.org/ICalculator/Divide"/>-<wsdl:input><wsp:PolicyReference URI="#WSHttpBinding_ICalculator_Divide_Input_policy"/><soap12:body use="literal"/></wsdl:input>-<wsdl:output><wsp:PolicyReference URI="#WSHttpBinding_ICalculator_Divide_output_policy"/><soap12:body use="literal"/></wsdl:output></wsdl:operation></wsdl:binding>-<wsdl:service name="CalculatorService">-<wsdl:port name="WSHttpBinding_ICalculator" binding="tns:WSHttpBinding_ICalculator"><soap12:address location="http://szx-station168:8001/service.svc"/>-<wsa10:EndpointReference><wsa10:Address>http://szx-station168:8001/service.svc</wsa10:Address>-<Identity xmlns="http://schemas.xmlsoap.org/ws/2006/02/addressingidentity"><Spn>host/SZX-station168</Spn></Identity></wsa10:EndpointReference></wsdl:port></wsdl:service></wsdl:definitions>

    9.到这里已经可以像示例一里创建一个客户端调用程序. 我们这里暂时先不创建客户端程序.这里讨论一下WCF和WebService的关系.我们知道WebService是承载于IIS中的,而这里WCF也能承载于IIS中. 在.NET中,其实说白了WebService是WCF的一部分. 就像在.NET FrameWork下,你可以创建控制台应用程序,Windows Form程序, WebForm程序一样; 那怎么样才能在WCF框架内创建一个标准的WebService程序呢? 很简单,只要修改WCF的绑定方式就可以了.

    将Web.config配置文件下

            <endpoint address=""
                      binding="wsHttpBinding"
                      contract="Microsoft.ServiceModel.Samples.ICalculator" />

    修改成

            <endpoint address=""
                      binding="basicHttpBinding"
                      contract="Microsoft.ServiceModel.Samples.ICalculator" />

    注意binding部分.

    我们打开浏览器, 重新访问http://szx-station168:8001/service.svc?wsdl

    看到WSDL已经变成标准WebService描述了.

    View Code
    <?xml version="1.0" encoding="UTF-8"?>
    -<wsdl:definitions xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:tns="http://tempuri.org/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://tempuri.org/" name="CalculatorService">-<wsdl:types>-<xsd:schema targetNamespace="http://tempuri.org/Imports"><xsd:import namespace="http://tempuri.org/" schemaLocation="http://szx-station168:8001/service.svc?xsd=xsd0"/><xsd:import namespace="http://schemas.microsoft.com/2003/10/Serialization/" schemaLocation="http://szx-station168:8001/service.svc?xsd=xsd1"/></xsd:schema></wsdl:types>-<wsdl:message name="ICalculator_Add_InputMessage"><wsdl:part name="parameters" element="tns:Add"/></wsdl:message>-<wsdl:message name="ICalculator_Add_OutputMessage"><wsdl:part name="parameters" element="tns:AddResponse"/></wsdl:message>-<wsdl:message name="ICalculator_Subtract_InputMessage"><wsdl:part name="parameters" element="tns:Subtract"/></wsdl:message>-<wsdl:message name="ICalculator_Subtract_OutputMessage"><wsdl:part name="parameters" element="tns:SubtractResponse"/></wsdl:message>-<wsdl:message name="ICalculator_Multiply_InputMessage"><wsdl:part name="parameters" element="tns:Multiply"/></wsdl:message>-<wsdl:message name="ICalculator_Multiply_OutputMessage"><wsdl:part name="parameters" element="tns:MultiplyResponse"/></wsdl:message>-<wsdl:message name="ICalculator_Divide_InputMessage"><wsdl:part name="parameters" element="tns:Divide"/></wsdl:message>-<wsdl:message name="ICalculator_Divide_OutputMessage"><wsdl:part name="parameters" element="tns:DivideResponse"/></wsdl:message>-<wsdl:portType name="ICalculator">-<wsdl:operation name="Add"><wsdl:input message="tns:ICalculator_Add_InputMessage" wsaw:Action="http://tempuri.org/ICalculator/Add"/><wsdl:output message="tns:ICalculator_Add_OutputMessage" wsaw:Action="http://tempuri.org/ICalculator/AddResponse"/></wsdl:operation>-<wsdl:operation name="Subtract"><wsdl:input message="tns:ICalculator_Subtract_InputMessage" wsaw:Action="http://tempuri.org/ICalculator/Subtract"/><wsdl:output message="tns:ICalculator_Subtract_OutputMessage" wsaw:Action="http://tempuri.org/ICalculator/SubtractResponse"/></wsdl:operation>-<wsdl:operation name="Multiply"><wsdl:input message="tns:ICalculator_Multiply_InputMessage" wsaw:Action="http://tempuri.org/ICalculator/Multiply"/><wsdl:output message="tns:ICalculator_Multiply_OutputMessage" wsaw:Action="http://tempuri.org/ICalculator/MultiplyResponse"/></wsdl:operation>-<wsdl:operation name="Divide"><wsdl:input message="tns:ICalculator_Divide_InputMessage" wsaw:Action="http://tempuri.org/ICalculator/Divide"/><wsdl:output message="tns:ICalculator_Divide_OutputMessage" wsaw:Action="http://tempuri.org/ICalculator/DivideResponse"/></wsdl:operation></wsdl:portType>-<wsdl:binding name="BasicHttpBinding_ICalculator" type="tns:ICalculator"><soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>-<wsdl:operation name="Add"><soap:operation style="document" soapAction="http://tempuri.org/ICalculator/Add"/>-<wsdl:input><soap:body use="literal"/></wsdl:input>-<wsdl:output><soap:body use="literal"/></wsdl:output></wsdl:operation>-<wsdl:operation name="Subtract"><soap:operation style="document" soapAction="http://tempuri.org/ICalculator/Subtract"/>-<wsdl:input><soap:body use="literal"/></wsdl:input>-<wsdl:output><soap:body use="literal"/></wsdl:output></wsdl:operation>-<wsdl:operation name="Multiply"><soap:operation style="document" soapAction="http://tempuri.org/ICalculator/Multiply"/>-<wsdl:input><soap:body use="literal"/></wsdl:input>-<wsdl:output><soap:body use="literal"/></wsdl:output></wsdl:operation>-<wsdl:operation name="Divide"><soap:operation style="document" soapAction="http://tempuri.org/ICalculator/Divide"/>-<wsdl:input><soap:body use="literal"/></wsdl:input>-<wsdl:output><soap:body use="literal"/></wsdl:output></wsdl:operation></wsdl:binding>-<wsdl:service name="CalculatorService">-<wsdl:port name="BasicHttpBinding_ICalculator" binding="tns:BasicHttpBinding_ICalculator"><soap:address location="http://szx-station168:8001/service.svc"/></wsdl:port></wsdl:service></wsdl:definitions>

    现在的问题是,basicHttpBinding 和wsHttpBinding有什么不同? basicHttpBinding绑定下,WCF发布的是标准的WebService服务,其他开发平台的开发人员可以很方便的调用,也知道应该怎样调用. 但WebService本身也有一些不足,或者不够完善的地方,比如不支持事务,不安全等, 于是.NET提供了一套自己的绑定方式wsHttpBinding, 说了是.NET自己提供的, 功能是多了,效率可能也高了,但因为它还不是国际标准,其他开发平台的编程语言不支持,人家也不知道怎么调用. 所以WCF的跨平台应该分两种情况

    如果服务提供方和服务调用方都是.NET平台下, 可以使用wsHttpBinding绑定;

    如果服务提供方和服务调用方是不同的开发平台,一个是.NET,另一个是JAVA,那就只能用basicHttpBinding绑定;

    而微软现在已经不推荐传统的ASP.NET WebService编程, 推荐使用WCF创建WebService服务.

    当然WCF还提供了其他的绑定方式,这里不作细究;

    9.客户端调用程序这里就先不创建了,有兴趣的同学可以参考示例一,过程是一样的.

  • 相关阅读:
    《天才在左,疯子在右》
    MVC思想概述
    java文件读写
    HTTP协议简单笔记
    自学Python_Day01
    Linux基础介绍篇
    PHP学习 Day_01
    Linux中部分命令英语全拼
    Linux学习基础命令(三)
    Linux学习基础命令(二)
  • 原文地址:https://www.cnblogs.com/dongdonggege/p/2834049.html
Copyright © 2011-2022 走看看