使用Websharp Service Locator簡化分布式系統開發
2024-07-21 02:21:15
供稿:網友
 
使用websharp service locator
簡化分布式系統開發
什么是websharp service locator
對于多層的應用系統來說,我們通常把它們劃分成客戶端、應用服務層和數據庫。在應用服務層,我們需要考慮至少兩個方面的問題:
ü 如何實現業務邏輯
ü 如何向客戶端提供服務。
我們可能使用多種技術來實現服務的提供:webservice、.net remoting、甚至ejb等。如此多的實現技術,帶來的很大的靈活性,但同時也帶來了問題,其中一個就是,有多少種服務端技術,就得有多少種相應的客戶端訪問技術。甚至,在某些分布式應用系統中,應用邏輯使用不同的技術開發,存在于不同的機器上,有的存在于客戶機本機,有的使用.net remoting開發,存在于局域網內,有的使用因特網上的web service,有的時候,我們希望相同的業務邏輯能夠支持不同的客戶端。
在這種情況下,我們需要一個一致的服務訪問編程模型,以統合不同的服務訪問模式,簡化系統的開發和部署。websharp service locator(以下簡稱wsl)提供了這樣一種能力,開發人員只需要定義服務訪問接口,就可以使用一致的方式透明的訪問這些服務,而不用理會這些服務之間的不同點??蚣軙詣由稍L問遠程服務需要的代理。
websharp是sourceforge上的一個新的開源項目,目標是提供一個.net環境下的輕量級的應用系統框架,它包含了三個主要內容:一個o/r mapping框架,一個aop框架,以及一個service locator。service locator目前還只完成本地程序集定位器、webservice定位器和.net remoting定位器的初步開發,但是,我們已經可以使用它提供的框架功能來為我們的開發提供助力。websharp service locator下面的目標是實現對j2ee的訪問??梢詮?http://www.sourceforge.net/projects/websharp/ 下載所有源代碼。
websharp service locator的主要接口
 wsl是一個輕量級的框架,非常易于使用和擴展。如果想使用wsl,那么只有一個類需要打交道:servicelocator,它的定義如下:
public abstract class servicelocator
{ 
 public static object findservice(string servicename,type clientinterface)
}
 
 如果你想用自己的定位器擴展這個框架,那么,只有一個接口需要擴展:iservicelocator。這個接口非常簡單,只有一個方法:
public interface iservicelocator
{
 object findservice(string servicename,type clientinterface);
}
 
websharp service locator的配置文件
 需要在三個地方配置wsl。
 首先,在configsections節中,注冊wsl配置文件處理類的的相關信息,配置方法如下:
<configsections>
 <section name="websharp.enterprise" 
 type="websharp.enterprise.enterpriseconfighandler,websharp" />
configsections>
 
然后,在websharp.enterprise節中,注冊不同的服務定位器。如果你自己擴展了這個框架,添加了新的服務定位器,也在這里注冊。其中,locator屬性的格式是:“類全名,assembly名”。 服務定位器都是singleton的。下面是目前wsl支持的服務定位器的注冊的信息:
<websharp.enterprise>
 <servicetypes>
 <servicetype name="localassembly" 
 locator="websharp.enterprise.localassemblylocator,websharp" /> 
 <servicetype name="webservice" 
 locator="websharp.enterprise.webservicelocator,websharp" /> 
 <servicetype name="dotnetremoting" 
 locator="websharp.enterprise.dotnetremotinglocator,websharp" /> 
 servicetypes>
websharp.enterprise>
 
最后,在websharp.enterprise下的services節中,注冊每個服務。每個service需要的屬性取決于不同的locator的實現,但是,name、service-type 和deploy-model是必須的。對于deploy-model,可以有兩種屬性值:singleton和multiinstance。
下面是一個例子:
<websharp.enterprise>
 <servicetypes>
 <servicetype name="localassembly" 
 locator="websharp.enterprise.localassemblylocator,websharp" /> 
 <servicetype name="webservice" 
 locator="websharp.enterprise.webservicelocator,websharp" /> 
 <servicetype name="dotnetremoting" 
 locator="websharp.enterprise.dotnetremotinglocator,websharp" /> 
 servicetypes>
 <services>
 <service name="helloworld" service-type="localassembly" deploy-model="singleton"
 type="enterpriseclient.helloworld,enterpriseclient" />
 <service name="helloworldwebservice" service-type="webservice" 
 deploy-model="singleton"
 url="http://localhost/webservicetest/hello.asmx" 
 namespace="http://www.websharp.org/webservices/" />
 services> 
 websharp.enterprise>
 
注:對于配置文件,在web項目中,可以是web.config文件,對于windows項目,可以自己為項目添加一個app.config配置文件。關于.net項目配置文件的更多內容,請參考msdn的相關文檔。
如何使用websharp service locator?
 使用wsl,一般的方法是這樣的:
1. 定義一個同你需要訪問的服務一致的接口(當然,如果你的服務是實現某個接口的,可以直接使用該接口)。接口的方法名和參數必須同服務類的方法名和參數一致。如果你的方法名和服務的方法名不一致,那么,可以使用servicemethodnameattribute來指明服務的方法名。
2. 在配置文件按中注冊你需要訪問的服務。
3. 調用servicelocator 的findservice方法.
4. 調用接口的方法。.
下面是一些例子,這些例子使用visual studio.net 2003開發,同樣可以從sourceforge下載。
 localassemblylocator 的hello world例子
 按照以下步驟進行:
1. 創建一個名為“enterpriseclient”的windows console 項目,加入websharp.dll的引用。
2. 添加一個類,名為“helloworld” ,然后添加一個名為“gethello”的方法,代碼如下:
 public class helloworld 
 {
 public string gethello(string hello)
 {
 return hello;
 }
 }
 
3. 添加一個名為 “ihelloworld” 的接口,代碼如下:
 public interface ihelloworld
 { 
 string gethello(string hello);
 
 [servicemethodname("gethello")]
 string gethello2(string hello);
 }
 
4. 填寫配置文件
xml version="1.0" encoding="utf-8" ?>
<configuration>
 <configsections> 
 <section name="websharp.enterprise" 
 type="websharp.enterprise.enterpriseconfighandler,websharp" />
 configsections>
 
 <websharp.enterprise>
 <servicetypes>
 <servicetype name="localassembly" 
 locator="websharp.enterprise.localassemblylocator,websharp" /> 
 <servicetype name="webservice" 
 locator="websharp.enterprise.webservicelocator,websharp" /> 
 servicetypes>
 
 <services>
 <service name="helloworld" service-type="localassembly" 
 deploy-model="singleton"
 type="enterpriseclient.helloworld,enterpriseclient" />
 services> 
 websharp.enterprise>
configuration>
 
5. 在main方法中添加如下代碼:
public static void main(string[] args)
{
 ihelloworld hello= servicelocator.findservice("helloworld",typeof(ihelloworld)) as ihelloworld;
 console.writeline(hello.gethello("hello world")); 
 console.writeline(hello.gethello2("hello again"));
 console.readline();
}
 
6. 運行程序,就能夠得到下面的結果:
hello world 的webservicelocator例子
按照以下步驟進行:
1. 新建一個webservice 項目,名為“webservicetest”。
2. 新建一個webservice 類,名為“hello” ,并添加一個“helloworld”方法,代碼如下: 
 [webservice(namespace="http://www.websharp.org/webservices/")]
 public class hello : system.web.services.webservice
 {
 [webmethod]
 public string helloworld()
 {
 return "hello world";
 }
 }
 
3. 使用上面我們創建的“enterpriseclient”項目,添加一個接口“ihello” ,代碼如下:
 public interface ihello
 {
 string helloworld();
 }
 
4. 填寫配置文件
<service name="helloworldwebservice" service-type="webservice" deploy-model="singleton"
 url="http://localhost/webservicetest/hello.asmx" 
 namespace="http://www.websharp.org/webservices/" />
 
5. 在main方法中添加下面的代碼:
public static void main(string[] args)
{
 ihello hello1= servicelocator.findservice
 ("helloworldwebservice",typeof(ihello)) as ihello; 
 console.writeline(hello1.helloworld());
 console.readline();
}
 
6. 運行程序,能夠得到下面的結果:
 
小結
 使用wsl,我們可以使用一致的編程模型訪問不同類型的服務,從而簡化軟件的開發和部署。例如,我們可以在開始的時候,使用本地assembly的方式開發軟件,然后,能夠很容易的改成使用webservice來發布服務,將軟件變成多層應用。我們也可以使用wsl來讓相同的服務能夠支持不同的客戶端,而所有的客戶端都使用相同的編程模型。
 websharp是一個還處于開發階段的框架,但是,因為他是開放源代碼的,我們可以直接使用他來進行進一步的開發。目前wsl支持的服務還不是很多,實現也還比較簡單,但是,他提供了一個很好的框架和構建分布式應用的方案,將來,他將提供越來越多的功能。