WCF架构入门-用VS2008构建WCF(转)

WCF架构入门-用VS2008构建WCF

    根据微软官方的解释,WCF(之前的版本名为“Indigo”)是使用托管代码建立和运行面向服务(Service Oriented)应用程序的统一框架。它使得开发者能够建立一个跨平台的安全、可信赖、事务性的解决方案,且能与已有系统兼容协作。WCF是微软分布式应用程序开发的集大成者,它整合了.Net平台下所有的和分布式系统有关的技术,例如.Net Remoting、ASMX、WSE和MSMQ。以通信(Communiation)范围而论,它可以跨进程、跨机器、跨子网、企业网乃至于 Internet;以宿主程序而论,可以以ASP.NET,EXE,WPF,Windows Forms,NT Service,COM+作为宿主(Host)。WCF可以支持的协议包括TCP,HTTP,跨进程以及自定义,安全模式则包括SAML, Kerberos,X509,用户/密码,自定义等多种标准与模式。也就是说,在WCF框架下,开发基于SOA的分布式系统变得容易了,微软将所有与此相关的技术要素都包含在内,掌握了WCF,就相当于掌握了叩开SOA大门的钥匙。

   本文就是要通过一个简单的例子,介绍WCF架构的搭建方法和步骤。本文使用的开发工具是VS2008。

  WCF架构包括三大部分:服务、宿主进程和客户端。其中服务和宿主进程属于服务端。

一、服务(Service)

  服务主要包括契约和服务的实现。

1.1 契约

  WCF的所有服务必须公开契约。契约是与平台无关的,描述服务功能的标准方式。主要有服务契约、数据契约、消息契约和错误契约。

  本文着重介绍一下服务契约的定义。我们所提供的服务是一个加法计算(Add)。首先我们定义契约。

view plaincopy to clipboardprint?
using System;  
 
using System.Collections.Generic;  
 
using System.Linq;  
 
using System.Text;  
 
using System.ServiceModel;  
 
namespace manual_WCF  
 
{  
 
    [<FONT color=#339900>ServiceContract</FONT>]  
 
    interface IMyProcess  
 
    {  
 
        [<FONT color=#339900>OperationContract</FONT>]  
 
        int Add(int a, int b);  
 
    }  
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

namespace manual_WCF

{

    [ServiceContract]

    interface IMyProcess

    {

        [OperationContract]

        int Add(int a, int b);

    }

}  我们可以将[ServiceContract]属性应用到接口或者类上,但是通常我们将其应用于接口上,而不是类上。

  另外,即使接口应用了[ServiceContract]属性,并不意味它的所有成员都是契约的一部分。我们必须使用[OperationContract]属性显式地标注哪些方法需要暴露为WCF契约的一部分。

1.2 服务的实现

  服务实现,其实就是对契约(接口)的实现。

view plaincopy to clipboardprint?
<FONT color=#000000><PRE class=csharp name="code"><STRONG>using System;  
 
using System.Collections.Generic;  
 
using System.Linq;  
 
using System.Text;  
 
 
 
namespace manual_WCF  
 
{  
 
    class MyProcess:IMyProcess  
 
    {  
 
        public int Add(int a, int b)  
 
        {  
 
            Console.WriteLine("Received Add({0},{1}) Returnning:{2}",a,b,a+b);  
 
            return a + b;  
 
        }  
 
    }  
 
}</STRONG></PRE>  
</FONT> 

view plaincopy to clipboardprint?<STRONG>using System;     using System.Collections.Generic;     using System.Linq;     using System.Text;         namespace manual_WCF     {         class MyProcess:IMyProcess         {             public int Add(int a, int b)             {                 Console.WriteLine("Received Add({0},{1}) Returnning:{2}",a,b,a+b);                 return a + b;             }         }     }</STRONG>  using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace manual_WCF

{

    class MyProcess:IMyProcess

    {

        public int Add(int a, int b)

        {

            Console.WriteLine("Received Add({0},{1}) Returnning:{2}",a,b,a+b);

            return a + b;

        }

    }

}
二、宿主进程(Host)

  WCF服务不可能凭空存在。每个WCF服务必须托管(Hosting)在Windows进程中,该进程就称为宿主进程(Host Process)。宿主可以由IIS提供,也可以有WindowsForm程序或者Console提供,也可以由Windows服务提供。

view plaincopy to clipboardprint?
<FONT color=#000000><STRONG>using System;  
 
using System.ServiceModel;  
 
using System.ServiceModel.Description;  
 
using System.Net;  
 
 
 
namespace manual_WCF  
 
{  
 
    class Program  
 
    {  
 
        static void Main(string[] args)  
 
        {  
 
            Uri baseAddress = new Uri("http://localhost:8001/");  
 
 
 
            ServiceHost Host = new ServiceHost(typeof(MyProcess), baseAddress);  
 
    
 
            Host.AddServiceEndpoint(typeof(IMyProcess),new BasicHttpBinding(),"manualWCF");  
 
 
 
            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();  
 
            smb.HttpGetEnabled = true;  
 
            Host.Description.Behaviors.Add(smb);  
 
 
 
            Host.Open();  
 
            
 
 
 
            Console.WriteLine("The manual WCF is running at " + baseAddress.ToString()+"manualWCF");  
 
 
 
            Console.WriteLine("Press <ENTER> to terminate");  
 
            Console.ReadLine();  
 
 
 
            Host.Close();  
 
           
 
        }  
 
    }  
 
}</STRONG></FONT> 

using System;

using System.ServiceModel;

using System.ServiceModel.Description;

using System.Net;

 

namespace manual_WCF

{

    class Program

    {

        static void Main(string[] args)

        {

            Uri baseAddress = new Uri("http://localhost:8001/");

 

            ServiceHost Host = new ServiceHost(typeof(MyProcess), baseAddress);

 

            Host.AddServiceEndpoint(typeof(IMyProcess),new BasicHttpBinding(),"manualWCF");

 

            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();

            smb.HttpGetEnabled = true;

            Host.Description.Behaviors.Add(smb);

 

            Host.Open();

         

 

            Console.WriteLine("The manual WCF is running at " + baseAddress.ToString()+"manualWCF");

 

            Console.WriteLine("Press <ENTER> to terminate");

            Console.ReadLine();

 

            Host.Close();

        

        }

    }

}view plaincopy to clipboardprint?
<FONT color=#000000> </FONT><FONT color=#3366ff><STRONG>三、客户端(Client)</STRONG></FONT> 

 三、客户端(Client)  若要调用WCF服务的操作,客户端需要首先导入服务契约到客户端本地描述(Native Representation)中。如果客户端使用WCF,调用操作的常见做法是使用代理。代理是一个CLR类,它公开了一个单独的CLR接口用以表示服务的契约。代理完全封装了服务的每一个方面:服务的位置、实现技术、运行时平台以及通信传输。

3.1 生成代理

  使用命令行工具SvcUtil.exe 生成代理

svcutil.exe /language:c# http://localhost:8001

  运行以上命令会生成两个文件MyProcess.cs和output.config。将这两个文件导入到客户端工程中(将output.config改名为app.config)。

3.2 调用服务操作

  此时,就可以像使用本地方法一样使用WCF服务中的操作了

view plaincopy to clipboardprint?
<FONT color=#000000>using System;  
 
using System.Collections.Generic;  
 
using System.Linq;  
 
using System.Text;  
 
 
 
namespace Client_manual  
 
{  
 
    class Program  
 
    {  
 
        static void Main(string[] args)  
 
        {  
 
            MyProcessClient client = new MyProcessClient();  
 
            Console.WriteLine(client.Add(3,5));  
 
            Console.ReadLine();  
 
        }  
 
    }  
 
}</FONT> 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Client_manual

{

    class Program

    {

        static void Main(string[] args)

        {

            MyProcessClient client = new MyProcessClient();

            Console.WriteLine(client.Add(3,5));

            Console.ReadLine();

        }

    }

}

代码:本文中的代码可以在以下地址中下载  http://download.csdn.net/source/530750

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/sahusoft/archive/2008/07/08/2625324.aspx

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章