日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Remoting系列(二)----建立第一个入门程序

發(fā)布時間:2024/8/5 编程问答 197 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Remoting系列(二)----建立第一个入门程序 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
http://www.cnblogs.com/Ring1981/archive/2006/07/23/455043.aspx

Remoting系列(二)----建立第一個入門程序

下面的Remoting程序,采用了Singleton模式的服務器端激活的方式,分為三部分。
General:一個遠程對象公用的程序集。
Server:服務器端
Client:客戶端


一 創(chuàng)建第一個Remoting控制臺程序

在創(chuàng)建程序之前,先把幾個要用到的類先介紹一下:

1) MarshalByRefObject類:

允許在支持遠程處理的應用程序中跨應用程序域邊界訪問對象。
應用程序域是一個操作系統(tǒng)進程中一個或多個應用程序所駐留的分區(qū)。同一應用程序域中的對象直接通信。不同應用程序域中的對象的通信方式有兩種:一種是跨應用程序域邊界傳輸對象副本,一種是使用代理交換消息。
MarshalByRefObject 是通過使用代理交換消息來跨應用程序域邊界進行通信的對象的基類。不是從 MarshalByRefObject 繼承的對象根據(jù)值隱式封送。當遠程應用程序引用根據(jù)值封送的對象時,將跨應用程序域邊界傳遞該對象的副本。
MarshalByRefObject 對象在本地應用程序域的邊界內(nèi)可直接訪問。遠程應用程序域中的應用程序首次訪問 MarshalByRefObject 時,會向該遠程應用程序傳遞代理。對該代理后面的調用將封送回駐留在本地應用程序域中的對象。
當跨應用程序域邊界使用類型時,類型必須是從 MarshalByRefObject 繼承的,而且由于對象的成員在創(chuàng)建它們的應用程序域之外無法使用,所以不得復制對象的狀態(tài)。

2) ChannelServices 類:

提供幫助進行遠程處理信道注冊、解析和 URL 發(fā)現(xiàn)的靜態(tài)方法。
信道跨遠程處理邊界(例如 AppDomains、進程和計算機)在應用程序之間傳輸消息。這些交叉可以是入站和出站的。信道可以在終結點上偵聽入站消息,向終結點發(fā)送出站消息,或同時進行這兩種操作。這在運行庫中提供一個可擴展性點,以便插入各種協(xié)議,即使運行庫可能并不在信道的另一端。運行庫對象可用于公開各種語義和實體。信道提供可擴展性點以將消息在特定協(xié)議間來回轉換。

3) TcpChannel 類:

提供使用 TCP 協(xié)議傳輸消息的信道實現(xiàn)。

一) Remoting程序的創(chuàng)建(分三步走)[附源代碼下載].

1 創(chuàng)建General遠程對象

此對象必須繼承MarshalByRefObject。
代碼如下:

?1/**////?<summary>
?2????///?遠程對象類
?3????///?</summary>

?4????public?class?RemoteObject?:?MarshalByRefObject
?5????{
?6????????public?RemoteObject()
?7????????{
?8????????????Console.WriteLine("I?am?from?Remote?Object!");
?9????????}

10????????public?string?GetClassName(string?name)
11????????{
12????????????return?name;
13????????}

14????}


2.創(chuàng)建Server作為"
宿主(host)",以接收客戶請求

1)注冊通道,下文注冊了一個端口號為8000的端口。
?? 注冊后服務器就會監(jiān)聽來自8000端口的請求。
2)注冊服務器激活的遠程對象

?1/**////?<summary>
?2????///?服務器端
?3????///?</summary>

?4????public?class?Server
?5????{
?6????????static?void?Main(string[]?args)
?7????????{
?8????????????//實例化并注冊一個TCP通道
?9????????????IChannel?tcpchannel?=?new?TcpChannel(8000);
10????????????ChannelServices.RegisterChannel(tcpchannel,?true);
11
12????????????//將服務端上的對象類型注冊為已知類型
13????????????RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject),?"GetObject",?WellKnownObjectMode.Singleton);
14
15????????????Console.WriteLine("Press?Enter?Key?to?Exit!");
16????????????Console.ReadLine();
17??????
18????????}

19????}


3.創(chuàng)建Client,調用遠程對象

1)注冊通道
2)根據(jù)URL得到對象代理
3)使用代理調用遠程對象

?1/**////?<summary>
?2????///?客戶端
?3????///?</summary>

?4????public?class?Client
?5????{
?6????????static?void?Main(string[]?args)
?7????????{
?8????????????//實例化并注冊一個TCP通道
?9????????????IChannel?tcpChannel?=?new?TcpChannel();
10????????????ChannelServices.RegisterChannel(tcpChannel,?true);
11
12????????????//激活遠程對象
13????????????RemoteObject?obj?=?(RemoteObject)Activator.GetObject(typeof(RemotingDemo.RemoteObject),"tcp://localhost:8000/GetObject");
14
15????????????if?(obj?==?null)
16????????????{
17????????????????Console.WriteLine("Could?not?get?Tcp?Server");
18????????????}

19
20????????????//調用遠程方法
21????????????Console.WriteLine("Client?TCP?GetClassName:?{0}",obj.GetClassName("ring1981"));
22
23????????????Console.ReadLine();
24????????}

25????}

到此為止,Remoting應用程序建立完畢!

運行結果:

上面的例子是全部通過編程實現(xiàn),在下篇文章里里我會介紹如何通過配置文件實現(xiàn)。

二)關于運行Remoting程序的一點說明

由于不少初學者說按"F5"看不到這樣的結果,其實是沒有學會如何運行這種多個項目,出現(xiàn)多個Dos窗口結果的工程。

我就附帶介紹一下如何運行上面這個例子吧:
第一步: 選擇"Debug"->"Start Debuging"或"Debug->Start without Debug"都行,點擊后會出現(xiàn)一個Dos窗口顯示結果。
第二步: 選擇"Soluting Explorer"下的"Client"項目,選中后點擊鼠標右鍵,點擊后出現(xiàn)一個菜單。
第三步: 選擇"Debug"->"Start new instance",單擊就OK了。
?

二 使用配置文件建立Remoting程序


一) 服務器端配置(app.config)

?1<configuration>
?2??<system.runtime.remoting>
?3????<application>
?4??????<channels>
?5????????<channel?ref="tcp"?port="8000"?/>
?6??????</channels>
?7??????<service>
?8????????<wellknown?mode="Singleton"
?9???????????????????type="RemotingDemo.RemoteObject,?General"
10???????????????????objectUri="GetObject"?/>
11??????</service>
12????</application>
13??</system.runtime.remoting>
14</configuration>
15
此時服務器端代碼為:

?1?/**////?<summary>
?2????///?服務器端
?3????///?</summary>

?4????public?class?Server
?5????{
?6????????static?void?Main(string[]?args)
?7????????{????????????
?8????????????RemotingConfiguration.Configure("server.exe.config",?true);
?9
10????????????Console.WriteLine("Press?Enter?Key?to?Exit!");
11????????????Console.ReadLine();??????
12????????}

13????}


二) 客戶端配置 (client.exe.config)

?1<configuration>
?2????<system.runtime.remoting>
?3????????<application>
?4????????????<client>
?5????????????????<wellknown?type="RemoteObject,?General"?url="tcp://localhost:8000/GetObject"?/>
?6????????????</client>
?7????????????<channels>
?8????????????????<channel?ref="tcp"?port="0"></channel>
?9????????????</channels>
10????????</application>
11????</system.runtime.remoting>
12</configuration>
13
此時客戶端的代碼為:

/**////?<summary>
????
///?客戶端
????
///?</summary>
????public?class?Client
????
{
????????
static?void?Main(string[]?args)
????????
{
????????????
????????????RemotingConfiguration.Configure(
@"client.exe.config",?true);
????????????RemoteObject?obj?
=?new?RemoteObject();
????????????
if?(obj?==?null)
????????????
{
????????????????Console.WriteLine(
"Could?not?get?Tcp?Server");
????????????}


????????????
//調用遠程方法
????????????Console.WriteLine("Client?TCP?GetClassName:?{0}",obj.GetClassName("ring1981"));

????????????Console.ReadLine();
????????}

????}


一個標準寫全的Remoting Configuration文件應該是如下結構:

?1<configuration>
?2???<system.runtime.remoting>
?3??????<application>
?4????????<lifetime?/>
?5????????<channels?/>
?6????????<service?/>
?7????????<client?/>
?8??????</application>
?9???</system.runtime.remoting>
10</configuration>
11
12
接下來結合配置文件介紹一下各結點的具體涉及內(nèi)容:

一) 生命周期(LifeTime):用于配置對象的生命周期.

ATTRIBUTE

DESCRIPTION

leaseTime

The initial time to live (TTL) for your objects (默認5 分鐘)

sponsorshipTimeout

The time to wait for a sponsor's reply (默認2 分鐘)

renewOnCallTime

The time to add to an object's TTL when a method is called (默認2 分鐘)

leaseManagerPollTime

The interval in which your object's TTL will be checked (默認10 秒)


例子:
1<lifetime
2???leaseTime="90MS"
3???renewOnCallTime="90MS"
4???leaseManagerPollTime="100MS"
5/>
這里MS是毫秒的意思.如果想配置成秒,那就用S;分鐘是M;小時是H;天是D。


二) 通道:

如果在服務器端注冊一個TCP通道,監(jiān)聽端口為1000,那么就該如下配置:

1<channels>
2???<channel?ref="tcp"?port="1000">
3</channels>
如果是在客戶端,那么port必須為0。

Channel結點包含如下內(nèi)容:

ATTRIBUTE

DESCRIPTION

ref

引用先前定義好的通道("tcp"或"http"),或者引用先前在配置文件里定義好的通道

displayName

此屬性只用于.NET Framework配置工具

type

該屬性在ref沒有定義時,是托管的。Attribute that is mandatory when ref has not been specified. Contains the exact type (namespace, classname, assembly) of the channel's implementation. When the assembly is in the GAC, you have to specify version, culture, and public key information as well.

port

端口號



三) Service:

Service屬性允許你注冊SAOs和CAOs.該結點下可以包含多和 <wellknown> 和<activated> 屬性。結構示例如下:

?1<configuration>
?2??<system.runtime.remoting>
?3????<application>
?4??????<service>
?5??????????<wellknown?/>
?6??????????<activated?/>
?7??????</service>
?8????</application>
?9??</system.runtime.remoting>
10</configuration>
11
12

?

1) WellKnow:

ATTRIBUTE

DESCRIPTION

type

寫法如"<namespace>.<classname>, <assembly>"。程序集中已發(fā)布類的類型。

mode

指出具體對象調用類型("Singleton" 或"SingleCall").

objectUri

用于呼叫對象的終端URI。當對象宿主在IIS中,URI需以.soap或.rem結尾。

displayName

用于.NET Framework Configuration Tool中。


結構示例如下:
?1<configuration>
?2??<system.runtime.remoting>
?3????<application>
?4??????<channels>
?5????????<channel?ref="http"?port="1234"?/>
?6??????</channels>
?7??????<service>
?8????????<wellknown?mode="Singleton"
?9???????????????????type="Server.CustomerManager,?Server"
10???????????????????objectUri="CustomerManager.soap"?/>
11??????</service>
12
13????</application>
14??</system.runtime.remoting>
15</configuration>
16

?2) Activated:

ATTRIBUTE

DESCRIPTION

type

寫法如"<namespace>.<classname>, <assembly>"。程序集中已發(fā)布類的類型。


結構示例如下:
?1<configuration>
?2???<system.runtime.remoting>
?3??????<application>
?4?????????<channels>
?5????????????<channel?ref="http"?port="1234"?/>
?6?????????</channels>
?7?????????<service>
?8????????????<activated?type="MyObject,?MyAssembly"/>
?9?????????</service>
10??????</application>
11???</system.runtime.remoting>
12</configuration>
13
14

?

四) Client:

與<service>屬性相對,結構與<service>非常類似。
結構示例如下:

?1<configuration>
?2???<system.runtime.remoting>
?3??????<application>
?4?????????<client>
?5????????????<wellknown?/>
?6????????????<activated?/>
?7????????</client>
?8??????</application>
?9???</system.runtime.remoting>
10</configuration>
11
12


三 Remoting的部署

一) 如何將Remoting部署為Windows Service?

1) 新建一個windows service項目
2) 在Service1的OnStart()下寫好方法,代碼如下:

1protected?override?void?OnStart(string[]?args)
2??{
3???RemotingConfiguration.Configure(@"C:\..\WindowsService1.exe.config");
4??}

3)?打開Visual Studio 2005 Command Prompt窗口.
4) 進入WindowsService1工程的bin\debug目錄.
???? cd D:\.......\bin\debug
5) 輸入installutil WindowsService1.exe,回車! 安裝完畢!此時在Windows服務中,就可以看到新注冊的服務.
6) 如果要卸載該服務,輸入的命令為 installutil /u WindowsService1.exe.


二) 如何將Remoting部署在IIS中?

1) 將Remoting遠程對象項目編譯成程序集
2) 創(chuàng)建虛擬目錄和bin文件夾
3) 將生成的程序集放在bin文件夾下.
4) 編寫一個web.config,內(nèi)容和Server下的配置文件相似,但不需要<channel>結點.
5) 將web.config放在虛擬目錄下。
完畢! 此時客戶端就可以訪問遠程對象了。

轉載于:https://www.cnblogs.com/chinhr/archive/2008/04/16/1155498.html

總結

以上是生活随笔為你收集整理的Remoting系列(二)----建立第一个入门程序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。