2

I'm trying to add new dlls to my application. I've tried using Ninject:

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

I've tried using:

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

And I always got the same error when I try to access to a controller that is in that new 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 

I'm implementing like a plugins based architecture and I want the ability to add new dlls without restart the application. Any idea with the code above?

1 Answer 1

3

You can load assemblies on the go. But do not put it inside your application /bin directory. Put it on another location, like /plugins. Do not let it be public visible.

Create a public interface in which is previously know, with functions like IMyInterface.DoStuff() and return an string, whatever.

Then you can use reflection to call it:

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

Make sure your MyClass implements your commom IMyInterface. You won't be able to see your classes like:

MyClass obj = new MyClass();

This would reset your ASP.NET application. But with reflection, you will be able to do something like:

string myReturn = ((IMyInterface)instanceOfMyType).DoStuff();
Sign up to request clarification or add additional context in comments.

5 Comments

My dll is a asp.net mvc application that has controllers inside. I'm building the controller factory and registering all that stuff. I can achieve this using BuildManager.AddReferencedAssembly(MyDll) but this method can be used only in PreApplicationStartMethod, even y try to use Ninject and I got the same error. I don't want call any method directly, I just want to reference my dll to my mvc main application.
Is there a major reason for not restarting the MVC application? If you add new views to your application, I don't see why you cant reset it. Even plugin technologies sometimes requires you application to restart for wiring some events. Also there is no way to compile a class that is not know at compile time! How I said, you need to create interfaces and route information through your loaded dlls, because this will be known at compile time.
I understand what you say, I've created all that structure, routing, interfaces, etc...The problem restarting the app is that I will lost sessions in the application, maybe I should create like a maintenance task and restart the application early in the morning after add a new plugin. But I prefer add a plugin without restart the main application, I can load the new links to the news views into the index page within the main application dynamically without restart.
I would say that you should never let your application pool living for too long. You should restart it in a regular basis in order you don't run out of memory in your server. You can try using a webfarm for that, with sessions saved on SQL Server. Then your users would not be logged out.
Yes, definitely that can be a solution using a SQL server to store the sessions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.