代码改变世界

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

  Clingingboy  阅读(644)  评论(0编辑  收藏  举报

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

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();
 }    
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2009-08-26 Spring.NET学习笔记(6)-基础AOP
点击右上角即可分享
微信分享提示