Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace assembly at runtime with .NET

Is there a way with a plugin system (I would use with an IoC container) to load one version of an assembly at runtime and then replace that DLL while the AppDomain is running? I don't want to restart the application.

Does MEF do something like this?

like image 539
Daniel A. White Avatar asked Feb 22 '11 16:02

Daniel A. White


People also ask

Is a DLL an assembly?

An assembly is a collection of types and resources that are built to work together and form a logical unit of functionality. Assemblies take the form of executable (.exe) or dynamic link library (. dll) files, and are the building blocks of . NET applications.

What is same assembly in C#?

It is basically compiled code that can be executed by the CLR. An Assembly is a basic building block of . Net Framework applications. It is basically a compiled code that can be executed by the CLR.

What is runtime version of DLL?

Runtime is the version of the CLR (or . NET framework) the DLL needs (usually as a minimum), version is the DLL's version. So long as you have the minimum runtime installed, it should be usable. However as a general rule it is usually best to select the latest version of the library for the latest runtime support etc.

When assembly will load on AppDomain?

If an assembly is loaded into the same AppDomain, then the class can be instantiated in the usual way. But if an assembly is loaded into a different AppDomain then it can be instantiated using reflection. Another way is an interface.


2 Answers

This is essentially what NUnit does (or at least did, I haven't been in the code in a while). But it does it by loading the test assembly in another AppDomain, calling code in that domain using the DoCallback method of AppDomain and then reloads the test assembly if it is recompiled.

So while you can't unload or reload a dll, but you can unload and reload an appdomain and execute code in it.

like image 164
Mike Two Avatar answered Oct 02 '22 17:10

Mike Two


It is impossible using pure .net, because there is no way to unload assembly from domain. Since MEF is written in managed code I doubt that it is possible. I solved this issue by loading assembly to separate domain and when I wanted to reload it I stoped it and started again.

like image 26
Andrey Avatar answered Oct 02 '22 16:10

Andrey