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