Custom AppDomain and PrivateBinPath

↘锁芯ラ 提交于 2019-12-04 10:39:47

问题


I'm using c# 4.0 and a console application just for testing, the following code does gives an exception.

AppDomainSetup appSetup = new AppDomainSetup()
{
    ApplicationName = "PluginsDomain",
    ApplicationBase = AppDomain.CurrentDomain.BaseDirectory,
    PrivateBinPath = @"Plugins",
    ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
};

AppDomain appDomain = AppDomain.CreateDomain("PluginsDomain", null, appSetup);

AssemblyName assemblyName = AssemblyName.GetAssemblyName(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins", "sample.dll"));

Assembly assembly = appDomain.Load(assemblyName); //This gives an exception of File not found

AppDomain.Unload(appDomain);

I keep getting File not found exception when using Load on my created AppDomain.

Thanks.


回答1:


First make sure Plugins is a subdirectory of your AppDomain base path. PrivateBinPath will only work on subdirectories as described here

If that isn't the problem then take a look at your fusion binding logs. Use the fusion log viewer There is also a good Blog Post on that. The fusion logs will tell you where it searched for the assembly. That should tell you if your path is included in the search.

One of the other possibilities is that it is finding your assembly but not one of its dependencies. Again the fusion log viewer will tell you.




回答2:


I came across this thread when attempting to dynamically load a dll file from a directory outside of the bin directory. Long story short, I was able to accomplish this by using the AppDomain.CurrentDomain.AssemblyResolve event. Here is the code:

//--begin example:

public MyClass(){
    AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
}

private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    foreach (var moduleDir in _moduleDirectories)
    {
        var di = new DirectoryInfo(moduleDir);
        var module = di.GetFiles().FirstOrDefault(i => i.Name == args.Name+".dll");
        if (module != null)
        {
            return Assembly.LoadFrom(module.FullName);
        }
    }
    return null;
}

//---end example

The method CurrentDomain_AssemblyResolve is called each time the AppDomain.CurrentDomain.Load("...") method is called. This custom event handler does the job of locating the assembly using your own custom logic (which means you can tell it to look anywhere, even outside of the bin path, etc). I hope this saves someone else a few hours...




回答3:


I think i'v figured out why this happens, thats because the current domain needs to load the assembly too even if your loading the assembly in a diffrent app domain, the current domain needs to know about it and load it, thats because how the .NET was designed.

check here for details.

http://msdn.microsoft.com/en-us/library/36az8x58.aspx

and when i checked the fusion logs, i found that the newly created app domain was successfully able to load the assembly from the private bin path, and the reason why you still get the exception of "File not found", because this exception belongs originally to the current app domain.

that means if you copied the assembly in current application path or to the path where the current domain is probing, you will find that you can load the assembly into your custom domain.

Hope that helps.



来源:https://stackoverflow.com/questions/6626647/custom-appdomain-and-privatebinpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!