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

C# 在不重新启动应用程序的情况下向asp.net mvc应用程序添加dll引用,c#,asp.net,.net,asp.net-mvc,dll,C#,Asp.net,.net,Asp.net Mvc,Dll,我正在尝试向我的应用程序添加新的DLL。我试过使用Ninject: var standardKernel = new StandardKernel(); ServiceLocator.SetLocatorProvider(() => new NinjectServiceLocator(standardKernel)); standardKernel.Load<MyPluginBootstrapper>(); standardKernel.Bind<IHelloWorldS

我正在尝试向我的应用程序添加新的DLL。我试过使用Ninject:

var standardKernel = new StandardKernel();
ServiceLocator.SetLocatorProvider(() => new NinjectServiceLocator(standardKernel));
standardKernel.Load<MyPluginBootstrapper>();
standardKernel.Bind<IHelloWorldService>().To<HelloWorldService>();
DependencyResolver.SetResolver(new MyDependencyResolver(standardKernel));
当我尝试访问新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实现了commom
IMyInterface
。 您将无法看到您的类,如:

MyClass obj = new MyClass();
这将重置ASP.NET应用程序。 但是,通过反思,您将能够执行以下操作:

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

My dll是一个asp.net mvc应用程序,其中包含控制器。我正在建造控制器工厂并注册所有这些东西。我可以使用BuildManager.AddReferenceAssembly(MyDll)实现这一点,但此方法只能在PreApplicationStartMethod中使用,即使尝试使用Ninject,我也会遇到同样的错误。我不想直接调用任何方法,我只想将我的dll引用到我的mvc主应用程序。不重新启动mvc应用程序有什么主要原因吗?如果向应用程序添加新视图,我不明白为什么不能重置它。甚至插件技术有时也需要重新启动应用程序来连接某些事件。此外,也没有办法编译一个在编译时不知道的类!我是这么说的,你需要通过加载的DLL创建接口和路由信息,因为这在编译时就会知道。我理解你说的,我已经创建了所有的结构、路由、接口等。重新启动应用程序的问题是我会丢失应用程序中的会话,也许我应该创建一个类似维护任务的应用程序,并在添加新插件后在清晨重新启动应用程序。但我更喜欢在不重启主应用程序的情况下添加插件,我可以在不重启的情况下将新闻视图的新链接动态加载到主应用程序的索引页面中。我想说的是,你永远不应该让你的应用程序池活得太久。您应该定期重新启动它,以免耗尽服务器中的内存。您可以尝试使用webfarm,并将会话保存在SQL Server上。这样您的用户就不会注销。是的,这肯定是一个使用SQL server存储会话的解决方案。
string myReturn = ((IMyInterface)instanceOfMyType).DoStuff();