簡體   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