热门话题:使用WCF实现SOA面向服务编程(二)——实现简单的WCF开发实例

来源:互联网 发布:node.js使用教程 编辑:程序博客网 时间:2024/05/22 05:29

前面为大家介绍过WCF的特点,现在再讲解一下WCF基础概念。

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

 

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

        < endpoint address = "basic"  binding = " basicHttpBinding "  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(string name);
      }

 

     //实现契约

      public class MyService:IService

     {

            public String HelloWorld(string name)

            {

                   return "Hello World"+Name;

             }

     }

}

 

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

<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>
//行为分为服务行为,端点行为,操作行为,回调行为4种,它们可以用于实现服务的授权,审计,序列化,事务流等操作
<behaviors>
   <serviceBehaviors>
      <behavior name="ServiceBehavior">
        <serviceMetadata httpGetEnabled="true" />    
        //开放MEX的元数据,这样就可以HTTP上暴露了元数据,使客户端获取元数据资料。
        <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
   </serviceBehaviors>
</behaviors>

 

客户端:

通过Add Service Reference引用服务地址

 

 

客户端将自动添加配置文件:
<system.serviceModel>
   <bindings>
     <basicHttpBinding>
      //设置basicHtppBinding,设定其连接时间,过期时间,发送时间,接收时间及最大数据量等等属性。
        <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>

最后通过代理直接调用
static void Main(string[] args)
{
    ServiceClient client=new ServiceClient();
    string data=client.HelloWorld("Leslie");
    Console.Writeline(data);
    Console.Read();
}

 

朋友,恭喜你,一个最简单Hello World的WCF已经实现。
然而,如果你要开发一个Web系统,你不可能将每一个类都编写成一个*.svc文件,那应该怎么才能更灵活地运用WCF实现网络通讯呢?

下一章将为你详细介绍如何使用AJAX+WCF实现灵活的页面开发

 

对NET系统开发有兴趣的朋友,请加入QQ群:NET技术开发联盟   59557329 一起讨论  点击这里加入此群

 


热门话题:使用WCF实现SOA面向服务编程(一)——SOA的概念
热门话题:使用WCF实现SOA面向服务编程(二)——实现简单的WCF开发实例
热门话题:使用WCF实现SOA面向服务编程(三)—— 使用AJAX+WCF实现页面开发
热门话题:使用WCF实现SOA面向服务编程(四)—— 通过Web服务调用Workflow工作流(基础实例) 
热门话题:使用WCF实现SOA面向服务编程(五)—— 通过Web服务调用Workflow工作流(开发持久化工作流)
热门话题:使用WCF实现SOA面向服务编程(六)—— 通过InvokeWebServiceActivity在Workflow工作流中调用Web服务
热门话题:使用WCF实现SOA面向服务编程(七)—— WF与WCF互相调用(利用ReceiveActivity把WF发布为WCF)