设为首页 收藏本站
查看: 1594|回复: 0

[经验分享] WCF入门示例二:承载于IIS中的WCF示例程序

[复制链接]

尚未签到

发表于 2015-8-13 12:50:51 | 显示全部楼层 |阅读模式
  上一个示例程序演示承载于托管代码中的WCF服务,这个示例将演示承载于IIS中的WCF服务.跟上一个示例一样我们先从创建一个WCF服务程序开始.为了方便演示,我们这个项目就在第一个项目的基础上创建.
  这里先提供完整的项目图做参考
DSC0000.png
  
  1.用VS2010打开WcfServiceDemo, 右键WcfServiceDemo解决方案-->Add-->New Project.安照下面的配置填写:
   DSC0001.png
  因为我们的WCF服务承载于IIS中,可以删除program.cs文件,并在IISHostedCalService项目属性中,将Console Application改成 Class Library.因为在删掉program.cs文件后,项目中没有main方法,如果不改成 Class Library,编译会报错.
  修改方法:右键IISHostedCalService项目-->Property
DSC0002.png
  
  2.引入System.ServiceModel.dll. 因为在下面创建服务协定的时候需要用到此类库.右键单击References-->Add References. 在弹出的对话框中,按照下面选择,点击OK按钮.
   DSC0003.png
  3.在应用程序目录中创建 App_Code 子目录。
  4.现在创建一个服务协定,也就是新建一个接口.为接口应用ServiceContractAttribute特性, 为接口中的4个方法应用OperationContractAttribute特性.
  这也是WCF协定于普通接口不同之处.带有[ServiceContract]特性的接口说明是WCF公开的协议;带有[OperationContract]特性的方法说明是协议公开的方法. 完整代码如下:
  在 App_Code 子目录中创建名为 ICalculator.cs 的代码文件
  ICalculator.cs


DSC0004.gif DSC0005.gif 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-->右键网站-->添加网站. 在弹出的对话框中按照下面的填写
DSC0006.png
  我本机的IIS版本号是7.5,在创建一个新网站之后,IIS会自动帮你添加一个默认的应用程序池,需要注意的是要确定应用程序池的.NET FrameWork 也是4.0,不然可能会报错.
DSC0007.png
  到此,WCF服务程序已经配置好,我们看看运行效果:
  打开IIS-->网站-->WcfCalService-->内容视图-->右键service.svc-->浏览.可以看熟悉的WCF service说明页面.
DSC0008.png
  点击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.客户端调用程序这里就先不创建了,有兴趣的同学可以参考示例一,过程是一样的.
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-98481-1-1.html 上篇帖子: 在IIS 6 中使用HTTP压缩(Compression)遇到的一个问题 下篇帖子: 在IIS中寄存已有WCF服务
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表