• .NET Remoting Basic(6)配置文件


          除了以硬编码的形式来注册对象,也可以以配置文件的方式来注册,以便增加灵活性

    1.服务器端配置文件

    其中以system.runtime.remoting 为配置节点

    配置了信道和注册对象,看起来非常的容易理解

    <configuration>
       <system.runtime.remoting>
         <application>
           <channels>
             <channel ref="http" port="1234" />
           </channels>
           <service>
             <wellknown mode="Singleton" type="Server.CustomerManager, Server" objectUri="CustomerManager.soap" />
           </service>
         </application>
       </system.runtime.remoting>
     </configuration>

    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.定义服务器端对象代理

    需加上SoapTypeSoapMethod来表示这个对象

    [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 in
                 RemotingConfiguration.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();
     }    
  • 相关阅读:
    Newbit 启用淘宝店域名
    Ninja构建系统入门
    异想家Golang学习笔记
    Webpack学习
    JavaFx图形界面开发
    异想家Win10常用的软件推荐
    Java Swing图形界面开发
    优雅写Java之四(类与对象)
    优雅写Java之三(IO与文本解析)
    优雅写Java之二(数组集合流)
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/1809575.html
Copyright © 2020-2023  润新知