- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我试图将我的插件 dll 加载到单独的 AppDomain 中,但是 Load() 方法因 FileNotFoundException 而失败。此外,似乎设置 AppDomainSetup 的 PrivateBinPath 属性没有效果,因为在日志中我看到“Initial PrivatePath = NULL”。所有插件都有强名称。通常每个插件都存储在[Application startp path]\postplugins\[plugindir]。如果我将插件子目录放在 [Application startp path] 目录下,一切正常。我也尝试过手动更改 AppBase 属性,但它没有改变。
这是代码:
public void LoadPostPlugins(IPluginsHost host, string pluginsDir)
{
_Host = host;
var privatePath = "";
var paths = new List<string>();
//build PrivateBinPath
var dirs = new DirectoryInfo(pluginsDir).GetDirectories();
foreach (var d in dirs)
{
privatePath += d.FullName;
privatePath += ";";
}
if (privatePath.Length > 1) privatePath = privatePath.Substring(0, privatePath.Length - 1);
//create new domain
var appDomainSetup = new AppDomainSetup { PrivateBinPath = privatePath };
Evidence evidence = AppDomain.CurrentDomain.Evidence;
var sandbox = AppDomain.CreateDomain("sandbox_" + Guid.NewGuid(), evidence, appDomainSetup);
try
{
foreach (var d in dirs)
{
var files = d.GetFiles("*.dll");
foreach (var f in files)
{
try
{
//try to load dll - here I get FileNotFoundException
var ass = sandbox.Load(AssemblyName.GetAssemblyName(f.FullName));
var f1 = f;
paths.AddRange(from type in ass.GetTypes()
select type.GetInterface("PluginsCore.IPostPlugin")
into iface
where iface != null
select f1.FullName);
}
catch (FileNotFoundException ex)
{
Debug.WriteLine(ex);
}
}
}
}
finally
{
AppDomain.Unload(sandbox);
}
foreach (var plugin in from p in paths
select Assembly.LoadFrom(p)
into ass
select
ass.GetTypes().FirstOrDefault(t => t.GetInterface("PluginsCore.IPostPlugin") != null)
into type
where type != null
select (IPostPlugin)Activator.CreateInstance(type))
{
plugin.Init(host);
plugin.GotPostsPartial += plugin_GotPostsPartial;
plugin.GotPostsFull += plugin_GotPostsFull;
plugin.PostPerformed += plugin_PostPerformed;
_PostPlugins.Add(plugin);
}
}
这是日志:
'FBTest.vshost.exe' (Managed (v4.0.30319)): Loaded 'D:\VS2010Projects\PNotes - NET\pnfacebook\FBTest\bin\Debug\postplugins\pnfacebook\pnfacebook.dll', Symbols loaded.
A first chance exception of type 'System.IO.FileNotFoundException' occurred in FBTest.exe
System.IO.FileNotFoundException: Could not load file or assembly 'pnfacebook, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9e2a2192d22aadc7' or one of its dependencies. The system cannot find the file specified.
File name: 'pnfacebook, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9e2a2192d22aadc7'
at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection)
at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
at System.Reflection.Assembly.Load(String assemblyString)
at System.UnitySerializationHolder.GetRealObject(StreamingContext context)
at System.AppDomain.Load(AssemblyName assemblyRef)
at PNotes.NET.PNPlugins.LoadPostPlugins(IPluginsHost host, String pluginsDir) in D:\VS2010Projects\PNotes - NET\pnfacebook\FBTest\PNPlugins.cs:line 71
=== Pre-bind state information ===
LOG: User = ANDREYHP\Andrey
LOG: DisplayName = pnfacebook, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9e2a2192d22aadc7
(Fully-specified)
LOG: Appbase = file:///D:/VS2010Projects/PNotes - NET/pnfacebook/FBTest/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: No application configuration file found.
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: pnfacebook, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9e2a2192d22aadc7
LOG: Attempting download of new URL file:///D:/VS2010Projects/PNotes - NET/pnfacebook/FBTest/bin/Debug/pnfacebook.DLL.
LOG: Attempting download of new URL file:///D:/VS2010Projects/PNotes - NET/pnfacebook/FBTest/bin/Debug/pnfacebook/pnfacebook.DLL.
LOG: Attempting download of new URL file:///D:/VS2010Projects/PNotes - NET/pnfacebook/FBTest/bin/Debug/pnfacebook.EXE.
LOG: Attempting download of new URL file:///D:/VS2010Projects/PNotes - NET/pnfacebook/FBTest/bin/Debug/pnfacebook/pnfacebook.EXE.
最佳答案
当您以这种方式将程序集加载到 AppDomain 时,当前 AppDomain 的 PrivateBinPath 用于查找程序集。
对于您的示例,当我将以下内容添加到我的 App.config 时,它运行良好:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="[PATH_TO_PLUGIN]"/>
</assemblyBinding>
</runtime>
虽然这对你不是很有用。
我所做的是创建一个包含 IPostPlugin 和 IPluginsHost 接口(interface)的新程序集,以及一个名为 Loader 的类,如下所示:
public class Loader : MarshalByRefObject
{
public IPostPlugin[] LoadPlugins(string assemblyName)
{
var assemb = Assembly.Load(assemblyName);
var types = from type in assemb.GetTypes()
where typeof(IPostPlugin).IsAssignableFrom(type)
select type;
var instances = types.Select(
v => (IPostPlugin)Activator.CreateInstance(v)).ToArray();
return instances;
}
}
我将那个新程序集保存在应用程序根目录中,它不需要存在于插件目录中(它可以但不会被使用,因为将首先搜索应用程序根目录)。
然后在主 AppDomain 中我改为这样做:
sandbox.Load(typeof(Loader).Assembly.FullName);
Loader loader = (Loader)Activator.CreateInstance(
sandbox,
typeof(Loader).Assembly.FullName,
typeof(Loader).FullName,
false,
BindingFlags.Public | BindingFlags.Instance,
null,
null,
null,
null).Unwrap();
var plugins = loader.LoadPlugins(AssemblyName.GetAssemblyName(f.FullName).FullName);
foreach (var p in plugins)
{
p.Init(this);
}
_PostPlugins.AddRange(plugins);
所以我创建了一个已知 Loader 类型的实例,然后获取它以从 插件 AppDomain 中创建插件实例。这样 PrivateBinPaths 就可以按您希望的方式使用。
另外一件事,私有(private) bin 路径可以是相对的,因此您可以添加 pluginsDir + Path.DirectorySeparatorChar + d.Name
而不是添加 d.FullName
来保持最终路径列表简短。不过这只是我个人的喜好!希望这会有所帮助。
关于c# - AppDomain.Load() 因 FileNotFoundException 而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16367032/
我为 ReSharper 编写了 xunit.net 测试运行程序,但在 8.0 版本中遇到了一个问题 - 我看到程序集无法加载到影子复制的 AppDomain 中。相同的代码和测试项目在 7.1 版
我无法弄清楚有关我的 AppDomain.Unload(...) 调用的问题。我有来自 my earlier question 的代码的详细解释.事实证明,我执行了几个显然不需要的步骤。但是,我相当确
我想在应用程序域内运行第三方库(在本例中为 XSP),这样我就可以关闭它或以其他方式控制它的行为。 基本流程: var child = AppDomain.CreateDomain(...) ...
我正在尝试将一个 dll 加载到控制台应用程序中,然后将其卸载并完全删除该文件。我遇到的问题是,在其自己的 AppDomain 中加载 dll 的行为会在父 AppDomain 中创建一个引用,因此除
我的印象是 AppDomains 彼此隔离。似乎在 StackOverException 的情况下,情况并非如此。 为了演示这个问题,我创建了一个简单的控制台应用程序,其唯一目的是生成一个新的 App
我有一个带有服务器“AppDomain”的应用程序,它接受来自单独的 AppDomain 的调用(其中托管插件,由其他人开发且不值得信赖)。 从服务器 AppDomain 中,我需要知道哪个“插件”(
坐: 我正在从我的进程中创建子应用程序域以加载程序集。 我可以调用此 AppDomain。 我想将一个对象从我的默认进程 AppDomain 传递到这个新创建的 AppDomain,以接收从加载到新
可能是以下副本:Can I prevent an uncaught exception in another AppDomain from shutting down the application?
我已经在 Specflow 2.0、nUnit 3.X、TeamCity 和 Visual Studio 2013 中编写了自动化测试。我试图并行运行这些测试,但它们失败了,因为代码使用静态类/对象。
我有一个 Bootstrapper,它查看 ASP.NET MVC 应用程序中的所有程序集以查找实现 IBootstrapperTask 接口(interface)的类型,然后将它们注册到 IOC C
我有一个 .NET 应用程序,它创建多个单独的 AppDomain 来运行一些可插入代码,每个 AppDomain 都设置自己的 WCF 命名管道服务来与中央应用程序进行通信。 但是我想为每个应用程序
对于我的项目,我需要从其他 .Net 程序集访问资源,但我不想让它们加载,因为我可能会加载同一程序集的不同版本。 为此,我创建了另一个 AppDomain,我使用 CreateInstanceAndU
我正在使用 AppDomain 来加载程序集然后卸载它们。 但是,我遇到了一个非常奇怪的问题。卸载 AppDomain 后 - 我仍然可以在进程资源管理器中看到某些程序集被多次加载! 为什么有加载的程
假设我有 AppDomainA,它启动 AppDomainB。 AppDomainB 然后启动 AppDomainC。 如果在 AppDomainA 中卸载 AppDomainB,AppDomainC
是否可以通过它的“友好名称”或 ID 来引用现有的 AppDomain(不是由我的代码创建)?还是以其他方式? 最佳答案 List AppDomains in Process 关于.net - 如何通
我在同一个虚拟目录下有一个网站和一个 web 服务,一些网页调用了 web 服务。 调用 Web 服务时,IIS 是创建新的 AppDomain 还是在同一个 AppDomain 中运行 Web 服务
在 ASP.NET 3.5(带有 IIS6)中,是否为每个请求创建了 AppDomains?我知道所有应用程序在 w3wp.exe 下都有自己的 AppDomain,但是整个 AppDomain 究竟
我一直无法找到关于使用 AppDomains 时发生的事情的非常清楚的描述,所以希望有人能够启发我。我有一个简单的测试程序(基本上是扯掉了 MSDN example ): using System;
我仍在尝试了解持续存在的问题,但它几乎可以概括为无法卸载 AppDomain。 它发生在将 ASP.NET WebAPI 部署到 Azure 应用服务期间,我们观察到以下情况: 进程 ID 不会更改,
我已经编写了在所有专用服务器上运行的软件(C#.NET控制台应用程序),这些服务器将管理各个Java进程(启动/停止/重新启动它们)。我遇到的问题是,当我的应用程序崩溃时,它不会关闭它启动的Java子
我是一名优秀的程序员,十分优秀!