.Net AOP(二)远程代理Remoting/RealProxy

本文介绍了一种使用 .NetRemoting 和 RealProxy 实现远程代理的方法。通过 TransparentProxy 和 RealProxy 的结合,可以在客户端和服务端之间建立代理关系。文章提供了具体的 C# 代码实现,并展示了如何创建用户注册接口及其实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

远程代理 

1、使用.Net Remoting/RealProxy

采用TransparentProxy和RealProxy实现对象的代理,实现思路如下:Client -TransparentProxy - RealProxy - Target Object

下面实现自定义的TransparentProxy和RealProxy

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. using System.Runtime.Remoting.Proxies;  
  2. using System.Runtime.Remoting.Messaging;  
  3.     //RealProxy  
  4.     public class MyRealProxy<T>:RealProxy  
  5.     {  
  6.         private T _target;  
  7.         public MyRealProxy(T target) : base(typeof(T))  
  8.         {  
  9.             this._target = target;  
  10.         }  
  11.        public override IMessage Invoke(IMessage msg)  
  12.        {  
  13.             PreProceede(msg);  
  14.             IMethodCallMessage callMessage = (IMethodCallMessage)msg;  
  15.             object returnValue = callMessage.MethodBase.Invoke(this._target, callMessage.Args);  
  16.             PostProceede(msg);  
  17.             return new ReturnMessage(returnValue, new object[0], 0, null, callMessage);  
  18.         }  
  19.        public void PreProceede(IMessage msg)  
  20.        {  
  21.            Console.WriteLine("方法执行前");  
  22.        }  
  23.        public void PostProceede(IMessage msg)  
  24.        {  
  25.            Console.WriteLine("方法执行后");  
  26.        }  
  27.     }  
  28.    //TransparentProxy  
  29.    public static class TransparentProxy  
  30.    {  
  31.         public static T Create<T>()  
  32.         {  
  33.            T instance = Activator.CreateInstance<T>();  
  34.            MyRealProxy<T> realProxy = new MyRealProxy<T>(instance);  
  35.            T transparentProxy = (T)realProxy.GetTransparentProxy();  
  36.            return transparentProxy;  
  37.         }  
  38.    }  

用户注册接口和实现

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public interface IUserProcessor  
  2.     {  
  3.         void RegUser(User user);  
  4.     }  
  5.   
  6.     public class UserProcessor : MarshalByRefObject, IUserProcessor  
  7.     {  
  8.         public void RegUser(User user)  
  9.         {  
  10.             Console.WriteLine("用户已注册。");  
  11.         }  
  12.     }  

客户端调用

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class Client  
  2.     {  
  3.         public static void Run()  
  4.         {  
  5.             try  
  6.             {  
  7.                 User user = new User() { Name = "lee"PassWord = "123123123123" };  
  8.                 UserProcessor userprocessor = TransparentProxy.Create<UserProcessor>();  
  9.                 userprocessor.RegUser(user);  
  10.             }  
  11.             catch (Exception ex)  
  12.             {  
  13.                 throw ex;  
  14.             }  
  15.         }  
  16.     }  


优点:简单实现

缺点:性能差

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值