惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
博客园_首页
酷 壳 – CoolShell
酷 壳 – CoolShell
G
Google Developers Blog
Jina AI
Jina AI
C
Check Point Blog
Apple Machine Learning Research
Apple Machine Learning Research
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
GbyAI
GbyAI
Last Week in AI
Last Week in AI
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
阮一峰的网络日志
阮一峰的网络日志
The Cloudflare Blog
M
MIT News - Artificial intelligence
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
J
Java Code Geeks
WordPress大学
WordPress大学
博客园 - 聂微东
月光博客
月光博客
博客园 - 司徒正美
T
The Blog of Author Tim Ferriss
大猫的无限游戏
大猫的无限游戏
Microsoft Security Blog
Microsoft Security Blog

博客园 - 蓝色游骑兵

利用JavaScript实现简单的下拉菜单 大话权限设计 安全系统的设计 ASP.NET 安全问题的思索 自定义验证器的开发 WinForm 开发 MSCRM 的感受 测试驱动开发 异常使用的思考 读UML精髓第三版之感受 使用静态构造函数与使用单件 数据完整性的检查 Acegi安全框架 安全服务的设计思考 ContextAttribute与ContextBoundObject应用开发3 ContextAttribute与ContextBoundObject应用开发2 邮件发送组件的开发完成 验证组件的设计和使用 如何从新开发的程序中提炼服务
ContextAttribute与ContextBoundObject应用开发
蓝色游骑兵 · 2008-05-19 · via 博客园 - 蓝色游骑兵

这两天因开发的需要,需要分析和构建针对ContextAttribute极其ContextBoundContext相关的拦截器的内容,所以今天一上班就开发分析ContextAttribute与ContextBoundContext之间的应用关系,在查看了相关网友的资源后开始了我的分析之路。
  首先:我建立了一个ContextAttribute的子类和一个普通的Attribute子类,分别附加在ContextBoundContext子类和普通子类上。代码如下:

using System;
using System.Collections.Generic;
using System.Text;

using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Activation;

namespace ContextDemo
{
    
public class DemoContextAttribute : ContextAttribute
    
{
        
public DemoContextAttribute()
            : 
base("DemoContextAttribute")
        
{
            Console.WriteLine(
"Call 'DemoContextAttribute' - 'Constructor' ");
        }

    }


    
public class NonContextAttribute : Attribute
    
{
        
public NonContextAttribute()
        
{
            Console.WriteLine(
"Call 'NonContextAttribute' - 'Constructor' ");
        }

    }


    [DemoContext]
    [NonContext]
    
public class DemoContextBoundObject : ContextBoundObject
    
{
        
public DemoContextBoundObject()
        
{
            Console.WriteLine(
"Call 'DemoContextBoundObject' - 'Constructor' ");
        }

    }


    [DemoContext]
    [NonContext]
    
public class NonContextBoundObject
    
{
        
public NonContextBoundObject()
        
{
            Console.WriteLine(
"Call 'NonContextBoundObject' - 'Constructor'  ");
        }

    }


    
class Program
    
{
        
static void Main(string[] args)
        
{
            Console.WriteLine(
"Begin Main ");

            DemoContextBoundObject dcbo 
= new DemoContextBoundObject();
            NonContextBoundObject ncbo 
= new NonContextBoundObject();

            Console.WriteLine(
"End Main ");

            Console.Read();
        }

    }

}

执行的结果如下:

这里可以看出,只有继承于ContextAttribute的子类在ContextBoundObject上使用才会进行构造。其实都没有进行调用。
 然后:我将ContextAttribute之下的虚方法都进行了一次继承并且添加相应的查看信息来分析各个方法的调用顺序,代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Activation;

namespace ContextDemo {
    
public class DemoContextAttribute : ContextAttribute {
        
public DemoContextAttribute()
            : 
base("DemoContextAttribute"{
            Console.WriteLine(
"Call 'DemoContextAttribute' - 'Constructor' ");
        }


        
public override void Freeze(Context newContext) {
            Console.WriteLine(
"Call 'DemoContextAttribute' - 'Freeze' ");
        }


        
public override void GetPropertiesForNewContext(IConstructionCallMessage ctorMsg) {
            Console.WriteLine(
"Call 'DemoContextAttribute' - 'GetPropertiesForNewContext' ");
            
base.GetPropertiesForNewContext(ctorMsg);
        }


        
public override bool IsContextOK(Context ctx, IConstructionCallMessage ctorMsg) {
            Console.WriteLine(
"Call 'DemoContextAttribute' - 'IsContextOK' ");
            
return false;
        }


        
public override bool IsNewContextOK(Context newCtx) {
            Console.WriteLine(
"Call 'DemoContextAttribute' - 'IsNewContextOK' ");
            
return base.IsNewContextOK(newCtx);
        }

    }


    [DemoContext]
    
public class DemoContextBoundObject : ContextBoundObject {
        
public DemoContextBoundObject() {
            Console.WriteLine(
"Call 'DemoContextBoundObject' - 'Constructor' ");
        }

    }


    
class Program {
        
static void Main(string[] args) {
            Console.WriteLine(
"Begin Main ");
            
new DemoContextBoundObject();
            Console.WriteLine(
"End Main ");
            Console.Read();
        }

    }

}

执行的结果显示如下:

这里可能以看出在执行的过程中,先建立与ContextBoundObject相关的ContextAttribute对象。并且向ContextAttribute对象询间IsContextOK,环境怎么样还可以吗。这里我先从这个IsContextOK开始进行分析。
这里通过修改代码查看运行的结果发现IsContextOk返回的结果为False,表示对当前环境有点不满意啊,将这里的IsContextOK直接返回True会是什么情况呢。修改代码执行显示结果如下:

 public override bool IsContextOK(Context ctx, IConstructionCallMessage ctorMsg) {
            Console.WriteLine(
"Call 'DemoContextAttribute' - 'IsContextOK' ");
            
return true;
        }


看来这个方式就是用来确认当前环境是否满足要求的,如果满足则不执行。这里可以查看一下这一部分的过程:
   CRL先询问当前环境是否满足要求,如果满足则不继续执行,如果不满足则先初始化新环境的GetPropertiesForNewContext,再将其Freeze,再询问IsNewContextOK如果没有问题则成功,如果在这里返回false,将引发一个Remoting的异常。