Castle DynamicProxy:保存代理程序集时拒绝访问

时间:2015-05-04 12:04:27

标签: castle-dynamicproxy

使用Castle DynamicProxy创建拦截器并将生成的运行时代理持久保存到汇编文件中。下次我将代理程序集加载到ModuleScope并创建几个新的代理,然后尝试将它们保存回同一个文件中,我得到以下异常:

System.UnauthorizedAccessException:"访问路径' D:\ BuildProjects \ VSProjects \ DynamicProxyTest \ DynamicProxyTest \ bin \ Debug \ DynamicProxyTest.Entity.Proxies.dll'被拒绝。"

Assembly proxies = Assembly.LoadFile(Environment.CurrentDirectory +  "\\DynamicProxyTest.Entity.Proxies.dll");

var scope = new ModuleScope(
    true,
    true,
    ModuleScope.DEFAULT_ASSEMBLY_NAME,
    ModuleScope.DEFAULT_FILE_NAME,
    "DynamicProxyTest.Entity.Proxies",
    "DynamicProxyTest.Entity.Proxies.dll");

scope.LoadAssemblyIntoCache(proxies);

_generator = new ProxyGenerator(
        new DefaultProxyBuilder(scope)
    );

var c2 = _generator.CreateClassProxy<Class2>(_logging);
var c3 = _generator.CreateClassProxy<Class3>(_logging);

// System.UnauthorizedAccessException thrown here!
scope.SaveAssembly(false);

我已经知道,当我第一次将它加载到缓存中时,Castle仍会引用 DynamicProxyTest.Entity.Proxies.dll

但是,如何通过将代理程序集加载到缓存中来正确实现此功能,如果需要,可以将任何新代理添加到作用域中,并将代理程序集成功保存/保存到桌面中?

0 个答案:

没有答案