cpa*_*lus 4 asp.net plugins webforms mef .net-assembly
我正在尝试使用MEF为asp.net webforms制作某种插件系统.
到目前为止,我已经找到了一个解决方案,主机网站将在其插件文件夹中搜索加载名为Module的其他网站.模块(插件)只是另一个网站项目,因此它的bin子文件夹中有自己的程序集.
您当然已经知道,ASP.NET应用程序不会在其bin文件夹之外加载dll .因此,如果我尝试访问模块页面(例如:plugins/MyModule/Page.aspx).我会收到一个错误,说服务器无法加载程序集MyModule.我可以将模块的程序集放在主bin文件夹中,一切都会正常工作,但我想将所有模块文件保存在同一个文件夹中.
所以我正在寻找一种在bin文件夹之外加载程序集的方法.我已经尝试搞乱web.config文件,但无法提供有效的解决方案.
然后我来到BuildManager类并编写了这段代码:
string pluginPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "plugins");
foreach (string f in Directory.GetDirectories(pluginPath))
{
string binPath = Path.Combine(f, "bin");
if (Directory.Exists(binPath))
{
foreach (String file in Directory.GetFiles(binPath, "*.dll"))
{
Assembly a = Assembly.LoadFrom(file);
BuildManager.AddReferencedAssembly(a);
}
}
}
Run Code Online (Sandbox Code Playgroud)
它在plugin文件夹中搜索包含bin文件夹的任何其他子文件夹,并使用BuildManager加载相应的程序集.要使此代码有效,我必须在global.aspx程序集的PreApplicationStartMethod中调用它.
现在当我导航到插件页面(插件/ MyModule/Page.aspx)时,它不会给我以前的错误,但页面是空白的.我检查了源代码,没有html,没有.我试图调试页面,但从未调用Page_Load方法.所以我想某种方式从来没有调用Page.aspx背后的代码.
编辑:
所以在搜索了一下之后我找到了一个解决方案:
当我加载模块程序集时,我将其引用存储在一个dictionnay中.然后在AppDomain.CurrentDomain.AssemblyResolve的事件处理程序中,我返回相应的模块程序集.
小智 7
我遇到了同样的问题.我解决了如下的解决方案
protected virtual void Application_Start(object sender, EventArgs e)
{
//...
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
}
System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
var currentAssemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (var assembly in currentAssemblies)
{
if (assembly.FullName == args.Name || assembly.GetName().Name == args.Name)
{
return assembly;
}
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1110 次 |
最近记录: |