【发布时间】:2016-02-25 16:22:29
【问题描述】:
我从加载在子域中的类 (InnerModuleInfoLoader) 中引用了单例 (CacheLayer)。 问题是这个引用与主域中的其他代码不同。 不知道有没有办法绕过appDomain的执行隔离来使用单例的实例?
代码如下:
AppDomain subdomain = this.CreatedChildDomain(AppDomain.CurrentDomain);
从子域实例化类
var loader = (InnerModuleInfoLoader) subdomain.
CreateInstanceFrom(loaderType.Assembly.Location, loaderType.FullName).Unwrap();
InnerModuleInfoLoader 内部:我希望 CacheLayer.Instance 对于父域和子域是相同的。
var server = CacheLayer.Instance.Get<string>("Server");
单身
public sealed class CacheLayer
{
private static readonly CacheLayer instance = new CacheLayer();
private static readonly ObjectCache cache;
static CacheLayer()
{
cache = MemoryCache.Default;
}
private CacheLayer(){}
//More code omitted
}
创建子域
protected virtual AppDomain CreatedChildDomain(AppDomain parentDomain)
{
Evidence evidence = new Evidence(parentDomain.Evidence);
AppDomainSetup setup = parentDomain.SetupInformation;
return AppDomain.CreateDomain("ModuleFinder", evidence, setup);
}
【问题讨论】: