Am using Castle DynamicProxy to create interceptors and persist the generated runtime proxies into assembly file. The next time I load the proxies assembly into ModuleScope and create few new proxies then try to save them back into the same file, I got the following exception:
System.UnauthorizedAccessException: "Access to the path 'D:\BuildProjects\VSProjects\DynamicProxyTest\DynamicProxyTest\bin\Debug\DynamicProxyTest.Entity.Proxies.dll' is denied."
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);
I already know that DynamicProxyTest.Entity.Proxies.dll is still referenced by Castle when I first loaded it into cache.
However, how can I implement this correctly by loading proxies assembly into cache, add any new proxies into the scope, if needed, and save/persist the proxies assembly successfully back into desk again?