.NET Remoting Basic(9)-上下文(CallContext)
CallContext 是类似于方法调用的线程本地存储区的专用集合对象,并提供对每个逻辑执行线程都唯一的数据槽。
使用该功能,存储对象必须实现ILogicalThreadAffinative接口
1.定义接口
[Serializable]public class LogSettings: ILogicalThreadAffinative{public bool EnableLog;}
2.客户端设置一个对象
static void Main(string[] args){String filename = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;RemotingConfiguration.Configure(filename);if (args.Length > 0 && args[0].ToLower() == "/enablelog"){LogSettings ls = new LogSettings();ls.EnableLog = true;CallContext.SetData("log_settings", ls);}IRemoteCustomerManager mgr = (IRemoteCustomerManager) RemotingHelper.CreateProxy(typeof(IRemoteCustomerManager));Customer cust = mgr.GetCustomer(42);Console.WriteLine("Done");Console.ReadLine();}
3.服务器端接收对象
根据key获取对象,然后判断属性
class CustomerManager: MarshalByRefObject, IRemoteCustomerManager{public Customer GetCustomer(int id){LogSettings ls = CallContext.GetData("log_settings") as LogSettings;if (ls!= null && ls.EnableLog){// simulate write to a logfile Console.WriteLine("LOG: Loading Customer " + id);}Customer cust = new Customer();return cust;}}class ServerStartup{static void Main(string[] args){String filename = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;RemotingConfiguration.Configure(filename);Console.WriteLine ("ServerStartup.Main(): Server started");Console.ReadLine();}}
4.将客户端和服务器端上下文数据操作进行封装
因为上下文数据可以在客户端和服务器端同时交互,所以可以进行封装,而无需让客户端和服务器端知道细节
4.1 定义LogSettingContext类
public class LogSettingContext{public static bool EnableLog{get{LogSettings ls = CallContext.GetData("log_settings") as LogSettings;if (ls!= null){return ls.EnableLog;}else{return false;}}set{LogSettings ls = new LogSettings();ls.EnableLog = value;CallContext.SetData("log_settings", ls);}}}
4.2 前端调用
LogSettingContext.EnableLog=true;
4.3后端调用
if (LogSettingContext.EnableLog){// simulate write to a logfile Console.WriteLine("LOG: Loading Customer " + id);}
如此便简化了操作
转载于:https://www.cnblogs.com/Clingingboy/archive/2010/08/26/1809584.html
.NET Remoting Basic(9)-上下文(CallContext)相关推荐
- .NET | 多线程下的调用上下文 : CallContext
[.NET]| 总结/Edison Zhou 最近在分析现在团队的项目代码(基于.NET Framework 4.5),经常发现一个CallContext的调用,记得多年前的时候用到了它,但是印象已经 ...
- .NET Remoting Basic(6)-配置文件
除了以硬编码的形式来注册对象,也可以以配置文件的方式来注册,以便增加灵活性 1.服务器端配置文件 其中以system.runtime.remoting 为配置节点 配置了信道和注册对象,看起来非常的容 ...
- EntLib 3.1学习笔记(6) : Security Application Block
http://www.microsoft.com/china/MSDN/library/enterprisedevelopment/softwaredev/dnpag2entlib.mspx?mfr= ...
- java httpinvoker漏洞_Spring HttpInvoker 服务端安全验证的和客户端请求配置
1.服务端 服务Java接口 package service; public interface TestService { int add(int i,int j); } 服务的Java实现 pac ...
- C# 用Attribute实现AOP事务
前言 使用Attribute来实现方法级别事务一直是我的梦想,浅谈Attribute [C# | Attribute | DefaultValueAttribute]有体现我的无奈,Attribute ...
- [转][.NET 基于角色安全性验证] 之一:基础知识
.NET 基于角色安全性验证的核心是主体(Principal)和标识(Identity)对象,其中主体负责角色或者组的验证,标识对象封装有关正在验证的用户或实体的信息.角色安全性验证通过生成可供当前线 ...
- Visual Biasc 函数速查
Calendar 常数 可在代码中的任何地方用下列常数代替实际值: 常数 值 描述 vbCalGreg 0 指出使用的是阳历. vbCalHijri 1 指出使用的是伊斯兰历法. Color 常数 可 ...
- 저장소system.runtime.remoting.messaging.callcontext
https://msdn.microsoft.com/ko-kr/library/system.runtime.remoting.messaging.callcontext(v=vs.110).asp ...
- EF创建上下文对象HttpContext和CallContext
public class BaseController : Controller { /// <summary> /// 方法1 / ...
- CallContext和多线程
前一段时间正好要在某个网页程序上开一个多线程调用多个组件的尝试,这些组件是有其他团队开发的(如:印度/俄罗斯),所以修改它们的代码看起来是不太现实的,但是,令人恼火的是他们的代码中大量的用到了AppC ...
最新文章
- 写一个能自动生成四则运算题目的软件,要求除了整数,还要支持正分数的四则运算。和同学们比较各自的程序功能、实现方法的异同。...
- windows下在计算机上远程登陆云服务器操作
- 一串事物中每个事物的前后位置与顺序编号的转换问题
- TypeError: Cannot set property 'msg' of undefined
- 二叉树遍历(代码,分析,汇编)
- LSGO软件技术团队2015~2016学年第十六周(1214~1220)总结
- lamp mysql5.0_CentOS 5/6 LAMP(Apache MySQL PHP)一键安装脚本
- Struts2框架学习笔记1
- DFS(深度优先遍历)走迷宫算法
- 此版本的 SQL Server 不支持用户实例登录标志。该连接将关闭“的解决
- gcc -o sqlite3 shell.c sqlite3.c -ldl -lpthread
- bilstmcrf词性标注_深度学习--biLSTM_CRF 命名实体识别
- linux下编译复数类型引发的错误:expected unqualified-id before '(' token
- .gliffy文件怎么打开和gliffy的免费试用两周
- Oracle Instead of Trigger的用法
- ORA-29339: tablespace block size 32768 does not match configured block sizes
- linux中 kill USR1和USR2 的区别
- supermap javascript 点聚合
- 倾斜摄影 镜头畸变校准_什么是风景摄影的最佳镜头?
- 二十:让行内元素在div中垂直居中