.NET Remoting 进阶之三——上下文(Context)例程

简介:
除了应用程序域外,.NET 提供了另一种类型的应用程序边界——上下文。应用程序可以包含许多上下文,而且至少包含一个默认的上下文。
   上下文提供了:
1、一个由一组属性组成的环境,驻留在同一上下文中的所有对象可以共享该环境。
2、一个拦截边界,由运行库对所有来自上下文外的方法调用进行预处理和事后处理。
3、可以保存具有类似运行库要求的对象,如同步、线程亲缘性或即时激活。
---------------------------------------------------------------------------------------------------
Context Agile对象:当运行库创建对象时,会调查对象的上下文请求,以将它放到合适的上下文中。如果没有合适
                   的上下文,运行时就会创建一个上下文,即默认上下文。大多数对象都会建立在默认上下文中,
                   这些对象称为Context Agile对象,因为可以直接从应用程序域内的任何地方直接访问它们。 
Context Bound对象:确实具有上下文要求的对象称之,它们必须要从 ContextBoundObject 类中派生而来。对该类
                   对象的跨上下文访问,由模拟实际对象的由运行库生成的代理来提供。代理允许运行库截取跨上
                   下文调用并可以应用任何预处理和事后处理。(会引起系统开销,慎重使用)
例子:
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Contexts;
namespace ContextTest
{
 /// <summary>
 /// Class1 的摘要说明。
 /// </summary>
 [Synchronization]
 public class MyContextBoundClass: ContextBoundObject
 {
 }
 public class MyAgilClass
 {
 }
 class ContextTest
 {
  static void Main(string[] args)
  {
   MyContextBoundClass myBound = new MyContextBoundClass();
   MyAgilClass myAgile = new MyAgilClass();
   Console.WriteLine("Is myBound out of context?
{0}",RemotingServices.IsObjectOutOfContext(myBound));
   Console.WriteLine("Is myAgile out of context?
{0}",RemotingServices.IsObjectOutOfContext(myAgile));
   Console.WriteLine("Is myBound a proxy ?
{0}",RemotingServices.IsTransparentProxy(myBound));
   Console.WriteLine("Is myAgile a proxy ?
{0}",RemotingServices.IsTransparentProxy(myAgile));
   Console.ReadLine();
  }
 }
}
运行结果:
Is myBound out of context? true
Is myAgile out of context? false
Is myBound a proxy ?  true 
Is myAgile a proxy ?  false
    利用 Thread.CurrentContext 静态方法,您可以获取对当前正在执行的上下文的引用。所返回的 Context 对象可用来查找与上下文相关的各种信息。
将上个例子稍做修改:
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Contexts;
using System.Threading;
namespace ContextTest
{
 /// <summary>
 /// Class1 的摘要说明。
 /// </summary>
 [Synchronization]
 public class MyContextBoundClass: ContextBoundObject
 {
  public MyContextBoundClass()
  {
   Console.WriteLine("in MyContextBoundClass Constructor");
   Diagnostics.DisplayContextInfo();
  }
 }
 public class MyAgilClass
 {
 }
 public class Diagnostics
 {
  public static void DisplayContextInfo()
  {
   Context ctx = Thread.CurrentContext;
   Console.WriteLine("Properties for context id:{0}", ctx.ContextID);
   foreach(IContextProperty ctxProp in ctx.ContextProperties)
   {
    Console.WriteLine("{0}",ctxProp.Name);
   }
  }
 }
 class ContextTest
 {
  static void Main(string[] args)
  {
//   MyContextBoundClass myBound = new MyContextBoundClass();
//   MyAgilClass myAgile = new MyAgilClass();
//
//   Console.WriteLine("Is myBound out of context? {0}",RemotingServices.IsObjectOutOfContext(myBound));
//   Console.WriteLine("Is myAgile out of context? {0}",RemotingServices.IsObjectOutOfContext(myAgile));
//
//   Console.WriteLine("Is myBound a proxy ? {0}",RemotingServices.IsTransparentProxy(myBound));
//   Console.WriteLine("Is myAgile a proxy ? {0}",RemotingServices.IsTransparentProxy(myAgile));
//
//   Console.ReadLine();
   Console.WriteLine("In Main");
   Diagnostics.DisplayContextInfo();
   MyContextBoundClass myBound = new MyContextBoundClass();
   Console.ReadLine();
  } 
 }
}


本文转自 august 51CTO博客,原文链接:http://blog.51cto.com/august/6925,如需转载请自行联系原作者
相关文章
|
开发框架 .NET 数据库
.NETCore 获取数据库上下文[实例的方法和配置连接字符串
.NETCore 获取数据库上下文[实例的方法和配置连接字符串
763 1
|
5月前
|
存储 移动开发 前端开发
对象存储oss使用问题之OSS SDK .net 使用下载例程报错如何解决
《对象存储OSS操作报错合集》精选了用户在使用阿里云对象存储服务(OSS)过程中出现的各种常见及疑难报错情况,包括但不限于权限问题、上传下载异常、Bucket配置错误、网络连接问题、跨域资源共享(CORS)设定错误、数据一致性问题以及API调用失败等场景。为用户降低故障排查时间,确保OSS服务的稳定运行与高效利用。
|
.NET 数据库 开发框架
ASP.NET Core 数据库上下文 - ASP.NET Core 基础教程 - 简单教程,简单编程
原文:ASP.NET Core 数据库上下文 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 数据库上下文 上一章节中我们了解了 Entity Framework 并讲述了如何配置它。
1817 0
|
网络协议 网络架构
.Net中Remoting通信机制
原文:.Net中Remoting通信机制 Remoting通信机制 Remoting介绍 主要元素 通道类型 激活方式 对象定义 Remoting介绍 什么是Remoting,简而言之,我们可以将其看作是一种分布式处理方式。
859 0
|
C# 网络协议
.Net中Remoting通信机制简单实例
原文:.Net中Remoting通信机制简单实例 .Net中Remoting通信机制 前言: 本程序例子实现一个简单的Remoting通信案例     本程序采用语言:c#   编译工具:vs2013工程文件   编译环境:.
835 0
.NET Remoting学习笔记(二)激活方式
原文:.NET Remoting学习笔记(二)激活方式 目录 .NET Remoting学习笔记(一)概念 .NET Remoting学习笔记(二)激活方式 .NET Remoting学习笔记(三)信道   参考:百度百科  风车车.Net   激活方式概念 在访问远程类型的一个对象实例之前,必须通过一个名为Activation的进程创建它并进行初始化。
1009 0
|
网络协议
.NET Remoting学习笔记(一)概念
原文:.NET Remoting学习笔记(一)概念 目录 .NET Remoting学习笔记(一)概念 .NET Remoting学习笔记(二)激活方式 .NET Remoting学习笔记(三)信道  背景 自接触编程以来,一直听过这个名词Remoting,但是对他了解少之又少,近日有点时间,参考研究研究。
1155 0
|
网络协议 数据安全/隐私保护 网络架构
.NET Remoting学习笔记(三)信道
原文:.NET Remoting学习笔记(三)信道 目录 .NET Remoting学习笔记(一)概念 .NET Remoting学习笔记(二)激活方式 .NET Remoting学习笔记(三)信道   参考:风车车.Net   .NET Framework 远程处理基础结构提供下列信道实现: IpcChannel TcpChannel HttpChannel IpcChannel IPCChannel是.NET Framework 2.0 里面新增的,它使用 Windows 进程间通信 (IPC) 系统在同一计算机上的应用程序域之间传输消息。
960 0
|
27天前
|
开发框架 前端开发 JavaScript
ASP.NET MVC 教程
ASP.NET 是一个使用 HTML、CSS、JavaScript 和服务器脚本创建网页和网站的开发框架。
27 7
|
25天前
|
存储 开发框架 前端开发
ASP.NET MVC 迅速集成 SignalR
ASP.NET MVC 迅速集成 SignalR
38 0