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