Remoting 配置格式说明(转)
使用配置文件的好處是什么?很簡單,他可以簡化代碼,可以隨時更改,通道,端口,URL的設(shè)置不需要重新編譯就可以運(yùn)行。所以在實(shí)際項(xiàng)目中經(jīng)常采用這種方式。
怎么寫一個服務(wù)器端的配置文件?
下面舉個例子:
<configuration>
? <system runtime remoting>?? ///配置的都是與remoting有關(guān)的內(nèi)容
??? <application>? ///可以包含多個application
?????? <serive>//表示在我的一個程序中注冊了一個service
??????? <wellknown mode="Singleton" ObjectUri="HelloNewegg"? ///ObjectUri為訪問這個對象的地址
???????? type="Newegg.RAM,Accounting"/>?? ///type表示對象的類型;Newegg.RAM表示這個類的名字;
?????????????????????????????????????????????????????????????????? ///Accounting表示程序集,實(shí)際表現(xiàn)為一個DLL
??????? </serive>
??????? <channels>//可以在里面注冊多個通道
???????????? <channel port="8888" ref="http"/>? ///ref表示引用了machine.config本身http不是在這個配置文件里配置的
??????? </channels>
??? </application>
? </system runtime remoting>
</configuration>
怎么寫一個客戶器端的配置文件?
<configuration>
? <system runtime remoting> ///配置的都是與remoting有關(guān)的內(nèi)容
??? <application>? ///可以包含多個application
?????? <client>//表示在我的一個程序中注冊了一個service
??????? <wellknown? ObjectUri="http://localhost:8888/HelloNewegg"
???????? type="Newegg.RAM,Accounting"/>??
?????????????????????????????????????????
??????? </client>
??????? <channels>
???????????? <channel port="0" ref="http"/> //port="0"什么意思?表示客戶端不用探聽任何端口。
??????? </channels>
??? </application>
? </system runtime remoting>
</configuration>
使用配置文件的代碼怎么寫??
服務(wù)器端:RemotingConfiguration.Configure("NeweggServer.exe.config");///NeweggServer表示服務(wù)器端的工程名
????????? 有時候也這樣寫:
??????????? string cfg=AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
??????????? RemotingConfiguration.Configure(cfg);
客戶端:RemotingConfiguration.Configure("NeweggClient.exe.config");///NeweggClient表示客戶器端的工程名
?????????? RAM obj=new RAM();
1,如下是Server端典型的Remoting配置文件:
<?xmlversion="1.0"encoding="utf-8"?>
<configuration>
?<system.runtime.remoting>
??? <application>
????? <channels>
??????? <channelref="http"/>
????? </channels>
????? <service>
??????? <wellknownmode="Singleton"
?????????????????? type="ComponentHost.CustomerManager, ComponentHost"
?????????????????? objectUri="CustomerManager.soap"/>
????? </service>
?
???? </application>
?? </system.runtime.remoting>
</configuration>
?
(1)當(dāng)Remote Objects部署在Console/Windows Form、Windows Services下時(上面的配置文件channel需要設(shè)置port屬性),相應(yīng)Server端聲明Remote Objects的代碼可以簡化為:
string filename = "server.exe.config";
RemotingConfiguration.Configure(filename);
?
(2)如果Remote Objects部署在IIS時,根本就不需要任何代碼聲明。但是需要將上述配置文件命名為:web.config,并且將Remote Objects的DLL文件安置在web application的BIN文件夾。
?
一般在實(shí)際應(yīng)用中,基本上將Remote Objects部署在IIS環(huán)境中,好處是(I)不需要編寫額外的代碼;(II)只要啟動機(jī)器,遠(yuǎn)程對象就啟動了。不需要你半夜三更跑到公司去登錄,然后啟動發(fā)生故障的遠(yuǎn)程服務(wù);(III)容易與IIS認(rèn)證服務(wù)進(jìn)行集成;(IV)可能還有更多優(yōu)點(diǎn),我現(xiàn)在沒有想到。
?
(3)如果需要聲明多個遠(yuǎn)程對象,只需要在<service>與</service>之間添加相應(yīng)的Remote Objects配置信息即可。
?
(4)另外需要注意type屬性為:<namespace>.<class>, <assembly>
?
2,如下是Client端典型的配置文件:
<?xmlversion="1.0"encoding="utf-8"?>
<configuration>
?<system.runtime.remoting>
??? <application>
?
????? <client>
??????? <wellknowntype="ComponentHost.CustomerManager, RemotingTest"
?????????????????? url="http://localhost/ComponentHost/CustomerManager.soap"/>
????? </client>
?
??? </application>
?</system.runtime.remoting>
</configuration>
?
要注意type屬性的設(shè)定:<namespace>.<class>, <assembly>
如果Client通過SoapSuds產(chǎn)生Remote Objects的元數(shù)據(jù)assembly,或者是Shared Assembly(如Interface或Abstract Class),這里<assembly>則為上述assembly的名稱。
如果是通過SoapSuds產(chǎn)生Source code,則<assembly>為Client應(yīng)用程序名(無exe后綴)。
?
同時,Client端application調(diào)用Remote Objects時,可以省掉:注冊通道、Activator.GetObject()/RemotingConfiguration.RegisterActivatedServiceType()等代碼,取而代之的代碼為:
string filename = “clientApplication.exe.config”;
RemotingConfiguration.Configure(filename);
下面(疑應(yīng)為直接)通過new來創(chuàng)建Remote Object實(shí)例。
?
3,標(biāo)準(zhǔn)的.Net Remoting Configuration配置文件
MSDN中有.Net Remoting Configuration file中全部元素/屬性的完整的詳細(xì)說明,需要的時候再查閱了。一般情況下,知道下面這些屬性就夠用了。
<configuration>
?? <system.runtime.remoting>
????? <application>
??????? <lifetime /> ――配置Remote Objects生存期的信息
??????? <channels /> ――配置與遠(yuǎn)程對象進(jìn)行通信的信道
??????? <service />
??????? <client />
????? </application>
?? </system.runtime.remoting>
</configuration>
?
簡單說明:
(1)<service> ――僅在Server端配置
????? <service>
????????? <wellknown /> ――配置要發(fā)布的SAO(已知)對象的信息
????????? <activated /> ――配置要發(fā)布的CAO客戶端激活對象的信息
????? </service>
?
?
(2)<client> ――僅在Client端配置,與Server端<service>對應(yīng)
???????? <client>
??????????? <wellknown />
??????????? <activated />
??????? </client>
?
When using CAOs, the <client> property has to specify the URI to the server for all underlying <activated> entries.
Note:When using CAOs from more than one server, you have to create several <client> properties in your configuration file.
當(dāng)調(diào)用CAO遠(yuǎn)程對象時,必須設(shè)定<client>的url屬性。如果CAO來自不同的Server,則需要在配置文件中定義多個<client>。如下所示:
???? <client url="http://localhost/MyServer>
??????? <activated type="Server.MyRemote, Client" />
????? </client>
?
4,定制Client/Server Channel元素
(1)Client Side
<channelref="http">
??? <clientProviders>
????????? <formatterref="binary"/>
??? </clientProviders>
</channel>
其中,formatter ref=”binary” or “soap”。formatter ref指要在通道上發(fā)送的消息格式,在此示例中為二進(jìn)制,以增強(qiáng)性能。
?
(2)Server Side
<channelref="http">
??? <serverProviders>
????????? <providerref="wsdl"/>
????????? <formatterref="binary"typeFileterLevel="Full"/>
????????? <formatterref="soap"typeFileterLevel="Full"/>
??? </serverProviders>
</channels>??
typeFilterLevel表示當(dāng)前自動反序列化級別,支持的值包括 Low(默認(rèn)值)和 Full。
1. Ingo Rammer, Advanced .Net Remoting.
2. MSDN
本文來自CSDN博客,轉(zhuǎn)載請標(biāo)明出處:http://blog.csdn.net/zhanghefu/archive/2007/07/18/1696818.aspx
轉(zhuǎn)載于:https://www.cnblogs.com/MayGarden/archive/2010/01/06/1640647.html
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的Remoting 配置格式说明(转)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 百度输入框的秘密
- 下一篇: 去黑头的7个必胜秘方