using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Text; namespace SGSMWebTest
{
public partial class Default : System.Web.UI.Page
{
private static FirstEliteValidateLib.Validator MyValidator = null; protected void Page_Load(object sender, EventArgs e)
{
var sb = new StringBuilder();
_AppDomain defaultAppDomain;
var appdomainList = CLRUtil.EnumAppDomains(out defaultAppDomain).ToList();
sb.AppendLine("***********Start**************"); foreach (var appdomain in appdomainList)
sb.AppendLine("AppDomainName:" + appdomain.FriendlyName + (appdomain.FriendlyName == defaultAppDomain.FriendlyName ? " [DefaultAppDomain]" : string.Empty)); sb.AppendLine("***********End**************");
sb.AppendLine("Current Appdomain:" + AppDomain.CurrentDomain.FriendlyName); if (defaultAppDomain.GetData("Validator") == null)
{
MyValidator = new FirstEliteValidateLib.Validator();
defaultAppDomain.SetData("Validator", MyValidator);
sb.AppendLine("Set Validator");
}
else
{
MyValidator = defaultAppDomain.GetData("Validator") as FirstEliteValidateLib.Validator;
if(MyValidator != null)
sb.AppendLine("Validator Get Success");
else
sb.AppendLine("Validator Get null");
} Label1.Text = "<pre>" + sb.ToString() + "</pre>";
} public static class CLRUtil
{
public static List<_AppDomain> EnumAppDomains(out _AppDomain defaultAppDomain)
{
// Obtain ICLRMetaHost interface
object objHost;
int hr = CLRCreateInstance(ref CLSID_CLRMetaHost, ref IID_CLRMetaHost, out objHost);
if (hr < ) throw new COMException("Cannot create meta host", hr);
var host = (ICLRMetaHost)objHost; // Obtain ICLRRuntimeInfo interface
var vers = Environment.Version;
var versString = string.Format("v{0}.{1}.{2}", vers.Major, vers.Minor, vers.Build);
var objRuntime = host.GetRuntime(versString, ref IID_CLRRuntimeInfo);
var runtime = (ICLRRuntimeInfo)objRuntime;
bool started;
uint flags;
runtime.IsStarted(out started, out flags);
if (!started) throw new COMException("CLR not started??"); // Obtain legacy ICorRuntimeHost interface and iterate appdomains
var result = new List<_AppDomain>();
var V2Host = (ICorRuntimeHost)runtime.GetInterface(ref CLSID_CorRuntimeHost, ref IID_CorRuntimeHost);
V2Host.GetDefaultDomain(out defaultAppDomain);
IntPtr hDomainEnum;
V2Host.EnumDomains(out hDomainEnum);
for (; ; )
{
_AppDomain domain = null;
V2Host.NextDomain(hDomainEnum, out domain);
if (domain == null) break;
result.Add(domain);
}
V2Host.CloseEnum(hDomainEnum);
return result;
} private static Guid CLSID_CLRMetaHost = new Guid(0x9280188d, 0xe8e, 0x4867, 0xb3, 0xc, 0x7f, 0xa8, 0x38, 0x84, 0xe8, 0xde);
private static Guid IID_CLRMetaHost = new Guid(0xD332DB9E, 0xB9B3, 0x4125, 0x82, 0x07, 0xA1, 0x48, 0x84, 0xF5, 0x32, 0x16);
private static Guid IID_CLRRuntimeInfo = new Guid(0xBD39D1D2, 0xBA2F, 0x486a, 0x89, 0xB0, 0xB4, 0xB0, 0xCB, 0x46, 0x68, 0x91);
private static Guid CLSID_CorRuntimeHost = new Guid(0xcb2f6723, 0xab3a, 0x11d2, 0x9c, 0x40, 0x00, 0xc0, 0x4f, 0xa3, 0x0a, 0x3e);
private static Guid IID_CorRuntimeHost = new Guid(0xcb2f6722, 0xab3a, 0x11d2, 0x9c, 0x40, 0x00, 0xc0, 0x4f, 0xa3, 0x0a, 0x3e); [DllImport("mscoree.dll")]
private static extern int CLRCreateInstance(ref Guid clsid, ref Guid iid,
[MarshalAs(UnmanagedType.Interface)] out object ptr); [ComImport, Guid("D332DB9E-B9B3-4125-8207-A14884F53216"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface ICLRMetaHost
{
[return: MarshalAs(UnmanagedType.Interface)]
object GetRuntime(string version, ref Guid iid);
// Rest omitted
} [ComImport, Guid("BD39D1D2-BA2F-486a-89B0-B4B0CB466891"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface ICLRRuntimeInfo
{
void GetVersionString(char[] buffer, int bufferLength);
void GetRuntimeDirectory(char[] buffer, int bufferLength);
bool IsLoaded(IntPtr hProcess);
void LoadErrorString(uint id, char[] buffer, int bufferLength, int lcid);
void LoadLibrary(string path, out IntPtr hMdodule);
void GetProcAddress(string name, out IntPtr addr);
[return: MarshalAs(UnmanagedType.Interface)]
object GetInterface(ref Guid clsid, ref Guid iid);
bool IsLoadable();
void SetDefaultStartupFlags(uint flags, string configFile);
void GetDefaultStartupFlags(out uint flags, char[] configFile, int configFileLength);
void BindAsLegacyV2Runtime();
void IsStarted(out bool started, out uint flags);
} [ComImport, Guid("CB2F6722-AB3A-11d2-9C40-00C04FA30A3E"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface ICorRuntimeHost
{
void CreateLogicalThreadState();
void DeleteLogicalThreadState();
void SwitchinLogicalThreadState(IntPtr cookie);
void SwitchoutLogicalThreadState(out IntPtr cookie);
void LocksHeldByLogicalThread(out int count);
void MapFile(IntPtr hFile, out IntPtr address);
void GetConfiguration(out IntPtr config);
void Start();
void Stop();
void CreateDomain(string name, object identity, out _AppDomain domain);
void GetDefaultDomain(out _AppDomain domain);
void EnumDomains(out IntPtr hEnum);
void NextDomain(IntPtr hEnum, out _AppDomain domain);
void CloseEnum(IntPtr hEnum);
// rest omitted
}
}
}
}

跨 Appdomain 对象共享的更多相关文章

  1. .NET跨AppDomain访问对象

    什么是AppDomain? 我们都知道windows进程,它起到应用程序隔离的作用,带来的好处是,当某个进程发生错误的时候,不会影响其他的进程,系统也不会受到影响.但是,创建windows进程的代价是 ...

  2. 跨JavaScript对象作用域调用setInterval方法

    跨JavaScript对象作用域调用setInterval方法: var id = window.setInterval(function() {foofunc.call(this);}, 200);

  3. redis 系列15 数据对象的(类型检查,内存回收,对象共享)和数据库切换

    一.  概述 对于前面的五章中,已清楚了数据对象的类型以及命令实现,其实还有一种数据对象为HyperLogLog,以后需要用到再了解.下面再了解类型检查,内存回收,对象共享,对象的空转时长. 1.1 ...

  4. Apex的对象共享

    Apex的对象共享 在Apex中,每个对象都有一个"共享"对象,其中存储了该对象的共享设定. 这种共享对象以"share"结尾.比如Account的共享对象是A ...

  5. 利用localStorage事件来跨标签页共享sessionStorage

    //干货 利用localStorage事件来跨标签页共享sessionStorage //因为cookie保存字节数量有限,很多童鞋考虑用html5 storage来保存临时数据,Sessionsto ...

  6. PHP实现跨服务器session共享的方法教程

    今天带来PHP实现跨服务器session共享的方法教程. 本文实例讲述了PHP实现cookie跨域session共享的方法.分享给大家供大家参考,具体如下: 做过web开发的小伙伴们都了解cookie ...

  7. [C++] 所有该类的对象共享静态类成员变量

    问:智能指针可以对指针的引用数量进行计数,一个智能指针释放时,别的智能指针怎么知道的? 同一类的对象共享同一变量最简单的方法是静态变量: 不像普通的变量,静态成员变量是被所有类对象共享的,不同的对象可 ...

  8. 跨域cookies 共享

    这是由于,本地调试.涉及到cookies的问题 想要跨域使用的问题 vue 中的mian.js中放入下面代码 import axios from 'axios' axios.defaults.with ...

  9. 【Flutter】功能型组件之跨组件状态共享

    前言   在Flutter开发中,状态管理是一个永恒的话题.   一般的原则是:如果状态是组件私有的,则应该由组件自己管理:如果状态要跨组件共享,则该状态应该由各个组件共同的父元素来管理.   对于组 ...

随机推荐

  1. Web Api 接口返回值不困惑:返回值类型详解

    前言:已经有一个月没写点什么了,感觉心里空落落的.今天再来篇干货,想要学习Webapi的园友们速速动起来,跟着博主一起来学习吧.之前分享过一篇 WebApi 接口参数:传参详解,这篇博文内容本身很基础 ...

  2. Java 11必掌握的8大特性,完美代码信手拈来

    [MyEclipse CI 2019.4.0安装包下载] 美国时间 09 月 25 日,Oralce正式发布了Java 11,这是据Java 8以后支持的首个长期版本.从官方发布的支持路线图表看出,J ...

  3. 【NOIP2017提高组模拟12.17】环

    题目 小A有一个环,环上有n个正整数.他有特殊的能力,能将环切成k段,每段包含一个或者多个数字.对于一个切分方案,小A将以如下方式计算优美程度: 首先对于每一段,求出他们的数字和.然后对于每段的和,求 ...

  4. 【leetcode】1105. Filling Bookcase Shelves

    题目如下: We have a sequence of books: the i-th book has thickness books[i][0] and height books[i][1]. W ...

  5. shell练习--PAT试题1009:说反话 (20 分)

    给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出. 输入格式: 测试输入包含一个测试用例,在一行内给出总长度不超过 80 的字符串.字符串由若干单词和若干空格组成,其中单词是由英文字母(大小 ...

  6. Dw 表单制作 与 dedecms 结合实现提交效果

    Dw 表单制作 与 dedecms 结合实现提交效果 自定义表单的用处 1.教育类网站的学员报名 2.企业网站的在线订单 3.普通网页上的一些和用户交互的小功能 实现原理:首先添加表单:核心-频道模型 ...

  7. 限制 button 在 3 秒内不可重复点击

    在下载或者上传文件过程中避免重复点击带来的多次同样的请求造成资源浪费,限制 button 的点击次数是很有必要的. 1. 增强用户体验,2. 减轻服务器压力. HTML 代码 <button i ...

  8. android之实现上下左右翻页效果

    如果实现上下或者左右翻页效果,我们借助下这个开源项目:https://github.com/openaphid/android-flip Aphid FlipView是一个能够实现Flipboard翻 ...

  9. Spring Cloud架构教程 (一)Hystrix监控面板

    下面我们基于之前的示例来结合Hystrix Dashboard实现Hystrix指标数据的可视化面板,这里我们将用到下之前实现的几个应用,包括: eureka-server:服务注册中心 eureka ...

  10. 【转】BYV--有向图强连通分量的Tarjan算法

    转自beyond the void 的博客: https://www.byvoid.com/zhs/blog/scc-tarjan 注:红色为标注部分 [有向图强连通分量] 在有向图G中,如果两个顶点 ...