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. KVM虚拟化网卡管理

    brctl常用命令 查看当前虚拟网桥状态 brctl show 添加一个网桥 addbr 删除一个网桥 delbr 添加网口 addif 删除网口 delif VALN LAN 表示 Local Ar ...

  2. JAVA并发编程的艺术 Java并发容器和框架

    ConcurrentHashMap ConcurrentHashMap是由Segment数组结构和HashEntry数组结构组成. 一个ConcurrentHashMap里包含一个Segment数组, ...

  3. 图片转base64的几种场景(网络图片,本地图片,用户上传图片)

    转载于博客园 https://www.cnblogs.com/zhangdiIT/p/7895903.html 写的很棒  推荐给大家 场景一:将用户本地上传的资源转化,即用户通过浏览器点击文件上传时 ...

  4. MongoDB 副本集+分片 认证方式搭建

    MongoDB 副本集+分片 认证方式搭建 参考资料: https://www.cnblogs.com/ityouknow/p/7344005.htmlhttps://jorwen-fang.itey ...

  5. 误删除mysql的root账号

    误删除mysql的root账号 mysql 测试通过 # mysql 误删除root最高权限用户 操作步骤: .停止mysql服务 (如果你删除了root用户,但没有退出操作对话框,恭喜节省了些麻烦, ...

  6. Python Number(数字) Ⅰ

    Python Number(数字) Python Number 数据类型http://www.xuanhe.net/用于存储数值. 数据类型是不允许改变的,这就意味着如果改变 Number 数据类型的 ...

  7. Redis 数据安全与性能保障

    数据安全与性能保障 ·将数据持久化至硬盘·将数据复制至其他机器·处理系统故障·reids事务·非实物型流水线·诊断性能问题 持久化选项: 共享选项,这个选项决定了快照文件和AOF文件的保存位置dir ...

  8. linux的字符集转换

    命令查看编码类型 查看命令参数 查看支持的字符集 将文件转换成utf-8 的文件(经测试比较鸡肋,不好用) [root@ag-1 hh]# iconv oldboy -f us-ascii -t ut ...

  9. POJ 2492 A Bug's Life (带权并查集 && 向量偏移)

    题意 : 给你 n 只虫且性别只有公母, 接下来给出 m 个关系, 这 m 个关系中都是代表这两只虫能够交配, 就是默认异性, 问你在给出的关系中有没有与异性交配这一事实相反的, 即同性之间给出了交配 ...

  10. golang rabbitmq实践 (一 rabbitmq配置)

    1:环境选择 系统为ubuntu 15.04 ,我装在虚拟机里面的 2:rabbitmq tabbitmq 3.5.4  download url : http://www.rabbitmq.com/ ...