.NET Remoting Basic(6)-配置文件
生活随笔
收集整理的這篇文章主要介紹了
.NET Remoting Basic(6)-配置文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
????? 除了以硬編碼的形式來注冊對象,也可以以配置文件的方式來注冊,以便增加靈活性
1.服務器端配置文件
其中以system.runtime.remoting 為配置節點
配置了信道和注冊對象,看起來非常的容易理解
2.Server端代碼
static void Main(string[] args){Console.WriteLine ("ServerStartup.Main(): Server started");String filename = "server.exe.config";RemotingConfiguration.Configure(filename);// the server will keep running until keypress.Console.ReadLine();}
3.客戶端同樣是采用配置文件
不過節點為client
<configuration><system.runtime.remoting><application><client><wellknown type="Server.CustomerManager, Client" url="http://localhost:1234/CustomerManager.soap" /></client></application></system.runtime.remoting></configuration>4.定義服務器端對象代理
需加上SoapType和SoapMethod來表示這個對象
[Serializable, SoapType(XmlNamespace=@"http://schemas.microsoft.com/clr/nsassem/Server/Server%2C%20Version%3D1.0.1584.30404%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull", XmlTypeNamespace=@"http://schemas.microsoft.com/clr/nsassem/Server/Server%2C%20Version%3D1.0.1584.30404%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull")]public class CustomerManager : System.MarshalByRefObject{[SoapMethod(SoapAction=@"http://schemas.microsoft.com/clr/nsassem/Server.CustomerManager/Server#GetCustomer")]public General.Customer GetCustomer(Int32 id){return((General.Customer) (Object) null);}}
5.客戶端訪問
static void Main(string[] args){String filename = "client.exe.config";RemotingConfiguration.Configure(filename);CustomerManager mgr = new CustomerManager();Console.WriteLine("Client.Main(): Reference to CustomerManager acquired");Customer cust = mgr.GetCustomer(4711);int age = cust.GetAge();Console.WriteLine("Client.Main(): Customer {0} {1} is {2} years old.",cust.FirstName,cust.LastName,age);Console.ReadLine();}
6.以接口形式訪問
6.1先讀取客戶端配置文件接口對象
class RemotingHelper{private static IDictionary _wellKnownTypes;public static Object CreateProxy(Type type){if (_wellKnownTypes == null) InitTypeCache();WellKnownClientTypeEntry entr = (WellKnownClientTypeEntry)_wellKnownTypes[type];if (entr == null){throw new RemotingException("Type not found!");}return Activator.GetObject(entr.ObjectType, entr.ObjectUrl);}public static void InitTypeCache(){Hashtable types = new Hashtable();foreach (WellKnownClientTypeEntry entr inRemotingConfiguration.GetRegisteredWellKnownClientTypes()){if (entr.ObjectType == null){throw new RemotingException("A configured type could not " +"be found. Please check spelling in your configuration file.");}types.Add(entr.ObjectType, entr);}_wellKnownTypes = types;}}
6.2客戶端獲取
static void Main(string[] args){String filename = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;RemotingConfiguration.Configure(filename);IRemoteCustomerManager mgr = (IRemoteCustomerManager) RemotingHelper.CreateProxy(typeof(IRemoteCustomerManager));Console.WriteLine("Client.Main(): Reference to rem.obj. acquired");Customer cust = mgr.GetCustomer(42);Console.WriteLine("Retrieved customer {0} {1}", cust.FirstName,cust.LastName);Console.ReadLine();}
轉載于:https://www.cnblogs.com/Clingingboy/archive/2010/08/26/1809575.html
總結
以上是生活随笔為你收集整理的.NET Remoting Basic(6)-配置文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 甲骨文全球大会——看SOA
- 下一篇: ASP.NET 完成基于表单的身份验证