• WCF揭秘(一)——简单的WCF开发实例


    一、WCF是什么

    WCF是微软为了实现各个开发平台之间的无疑缝连接而开发一种崭新工具,它是为分布式处理而开发。WCF将DCOM、Remoting、Web Service、WSE、MSMQ、AJAX服务、TCP开发集成在一起,从而降低了分布式系统开发者的学习曲线,并统一了开发标准。

    二、WCF的优点

    第一,开发的统一性。WCF是对于ASMX, Remoting,Enterprise Service,WSE,MSMQ,TCP开发等技术的整合。WCF是由托管代码编写,无论你是使用TCP通讯,Rmoting通讯还是Web Service ,我们都可以使用统一的模式进行开发,利用WCF来创建面向服务的应用程序。

    第二,WCF能够实现多方互操作。它是使用 SOAP通信机制,这就保证了系统之间的互操作性,即使是运行不同开发语言,也可以跨进程、跨机器甚至于跨平台的通信。例如:使用J2EE的服务器(如WebSphere,WebLogic),应用程序可以在Windows操作系统进行调用,也可以运行在其他的 操作系统,如Sun Solaris,HP Unix,Linux等等。

    第三,提供高效的安全与可信赖度,它可以使用不同的安全认证将WS-Security,WS-Trust和WS-SecureConversation等添加到SOAP消息中。在SOAP的header中增加了WS-ReliableMessaging允许 可信赖的端对端通信。而建立在WS-Coordination和WS-AtomicTransaction之上的基于SOAP格式交换的信息,则支持两阶段的事务提交(two-phase commit transactions)。

    第四,WCF支持多支消息交换模式,如请求-应答,单工,双工等等。另外WCF还支持对等网——利用啮合网络址,客户端能在没有中心控制的情况下找到彼此并实现相互通信。
    总括来说,WCF是实现SOA的的一个优秀选择,利用WCF能够实现跨平台,跨语言的无缝连接,从而实现Web服务的相互调用。
     
    三、简单开发实例

    在WCF里,各个Application之间的通信是由EndPoint来实现的,EndPoint是WCF实现通信的核心要素。一个WCF Service可由多个EndPoint集合组成,每个EndPoint只能有一种绑定,就是说EndPoint就是通信的入口,客户端和服务端通过 EndPoint交换信息。

      < service name = " " > 
             < endpoint address = ""  binding = " wsHttpBinding "  contract = " myNamespace.IService " > 
             </ endpoint >

    </service>

    Endpoint由三部分组成:(A) Address 地址,(B)Binding 绑定,(C)Contract 契约。

    • A(Address): 通过一个URI唯一地标识一个Endpoint,并告诉WCF service的调用者如何找到这个Endpoint。
    • B(Binding): 定义了与数据传输相关的传输协议,消息编码,通信模式,可靠性,安全性,事务,互操作性等信息。Framewrok3.5里已经包括以下几种绑定:

    • C(Contract):它是有关服务响应的操作及进出消息的格式的语法描述,系统就是通过Contract实现操作的。

    下面为大家讲解一下Hello World的开发例子

    服务器端:

     using  System; 
     using  System.ServiceModel; 
     namespace myNamespace 
     {   
    
           //在服务器端定义在一个服务契约
    
           [ServiceContract(Namespace  =   " myNamespace " )] 
           public interface IService 
          { 
             [OperationContract] 
             String HelloWorld(); 
          }
    
         //实现契约
    
          public class MyService:IService
    
         {
    
                public String HelloWorld(string name)
    
                {
    
                       return "Hello World"+Name;
    
                 }
    
         }
    
    }
    View Code

    服务既可以在代码中实现,也可以在配置文件中实现

    <services>
       <service behaviorConfiguration="ServiceBehavior" name="Service">
    
         //行为可以影响运行是操作的WCF类,它不公在客户端和服务器启动WCF运行时被执行,还可以在二者之间流动消息时被执行。
        <endpoint address="" binding="wsHttpBinding" contract="IService">
         <identity>
          <dns value="localhost" />
         </identity>
        </endpoint>
    
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
    
          //mexHttpBinding定义了WCF的元数据。当没有定义元数据时,服务依然能够执行,但不能在HTTP中被发现。
        </service>
    </services>
    
    <behaviors>
       <serviceBehaviors>
          <behavior name="ServiceBehavior">
            <serviceMetadata httpGetEnabled="true" />
            <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
       </serviceBehaviors>
    </behaviors>
    View Code

    客户端:

    通过Add Service Reference引用服务地址

    添加配置文件:

    <system.serviceModel> 
       <bindings> 
         <basicHttpBinding> 
            <binding name="wsHttpBinding_IService" closeTimeout="00:01:00" 
                  openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
                  allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" 
                  maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
    
                  messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"  useDefaultWebProxy="true"> 
             <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
               maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
              <security mode="None">
    
                 <transport clientCredentialType="None" proxyCredentialType="None"  realm="" /> 
                 <message clientCredentialType="UserName" algorithmSuite="Default" /> 
            </security> 
          </binding> 
       </basicHttpBinding> 
    </bindings> 
    <client> 
        <endpoint address="http://localhost/myNamespace.IService.svc"   binding="wsHttpBinding"
    
             bindingConfiguration="wsHttpBinding_IService" 
    
           contract="myNamespace.IService" name="wsHttpBinding_IService" /> 
    </client> 
    </system.serviceModel> 
    View Code

    最后通过代理直接调用

    static void Main(string[] args)
    {
        ServiceClient client=new ServiceClient();
        string data=client.HelloWorld("Leslie");
        Console.Writeline(data);
        Console.Read();
    }
    View Code

    原文地址:http://www.cnblogs.com/leslies2/archive/2011/01/26/1934163.html

  • 相关阅读:
    [bzoj 2151]种树(贪心)
    [bzoj 1026]windy数(数位DP)
    [BZOJ2038]小Z的袜子(莫队算法)
    spark调优
    从底层谈WebGIS 原理设计与实现(一):开篇
    css兼容性记录
    H5新标签
    AMD 与CMD
    rem 响应 js函数
    SVN服务器搭建和使用(一)
  • 原文地址:https://www.cnblogs.com/CSharpLover/p/5690805.html
Copyright © 2020-2023  润新知