0

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?

FirasR
  • 31
  • 2
  • The assembly is being loaded into your current app domain and therefore locked (in-use). To do what you want (replace an assembly that you want to dynamically load at runtime), you'll need to load the assembly into a different app domain. When you're ready to replace the assembly, unload the app domain. This will release the lock on the DLL file. You can then overwrite the DLL and then reload the app domain with the updated DLL. – PatrickSteele May 26 '15 at 12:49
  • 1
    Unrelated to your original question, but how did you persist the generated runtime proxies into assembly file? I've been trying to do this without success. – Keith Aug 03 '15 at 17:09

0 Answers0