繁体   English   中英

在不重新启动应用程序的情况下向asp.net mvc应用程序添加dll引用

[英]Adding dll reference to asp.net mvc application without restarting application

我正在尝试将新的dll添加到我的应用程序。 我试过使用Ninject:

var standardKernel = new StandardKernel();
ServiceLocator.SetLocatorProvider(() => new NinjectServiceLocator(standardKernel));
standardKernel.Load<MyPluginBootstrapper>();
standardKernel.Bind<IHelloWorldService>().To<HelloWorldService>();
DependencyResolver.SetResolver(new MyDependencyResolver(standardKernel));

我试过使用:

var _fullPluginPath = Path.Combine(path, "App2.Plugin.dll");
AppDomain.CurrentDomain.Load(Assembly.LoadFrom(_fullPluginPath).GetName());

当我尝试访问该新dll中的控制器时,总是出现相同的错误:

Compilation Error
Compiler Error Message: CS0246: The type or namespace name 'App2' could not be found (are     you missing a using directive or an assembly reference?)
Line 26:     using System.Web.Optimization;
Line 27:     using System.Web.Routing;
Line 28:     using App2.Plugin;
Line 29:     
Line 30:     

Source File: c:\Users\wilhem\AppData\Local\Temp\Temporary ASP.NET Files\root\0c41d57d\e08d7bc3\App_Web_index.cshtml.244a139d.hzhandta.0.cs    Line: 28 

我正在实现基于插件的体系结构,并且希望能够在不重新启动应用程序的情况下添加新的dll。 对上面的代码有任何想法吗?

您可以随时随地加载程序集。 但是不要将其放在应用程序的/bin目录中。 将其放在另一个位置,例如/plugins 不要让它公开可见。

使用IMyInterface.DoStuff()类的函数创建一个先前已知的公共接口,然后返回一个字符串。

然后,您可以使用反射来调用它:

Assembly assembly = Assembly.LoadFrom(Server.MapPath("~/plugins/myDll.dll"));
Type type = assembly.GetType("MyClass");
object instanceOfMyType = Activator.CreateInstance(type);

确保您的MyClass实现了常用的IMyInterface 您将无法看到您的课程,例如:

MyClass obj = new MyClass();

这将重置您的ASP.NET应用程序。 但是通过反射,您将可以执行以下操作:

string myReturn = ((IMyInterface)instanceOfMyType).DoStuff();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM