Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there reference implementations of hot-swapping in .NET?

Tags:

c#

.net

mef

hotswap

I'm looking for a good implementation of hot-swapping done in .NET. The things I need are:

  • Being able to deploy DLLs in a particular folder and have a running system pick them up.
  • Having the running system update corresponding references in the container.

I've been looking into MEF and its directory loading mechanism, but it seems very unreliable. Maybe someone out there has an alternative implementation?

like image 925
Dmitri Nesteruk Avatar asked Mar 05 '13 19:03

Dmitri Nesteruk


People also ask

Which devices are hot swappable?

Hot swappable components include network adapters, Universal Serial Bus (USB) devices (like a keyboard) and external hard drives, among others. Hot swap is a required feature in fault-tolerant systems with redundant components such as circuit boards, hard drives, etc.

What does a hot swap controller do?

Hot swap controllers provides circuit protection from many potential hazards caused by abrupt changes in the power supply. This type of IC can monitor overvoltage and undervoltage conditions and open and close switches very quickly to protect sensitive circuits when an unsafe power condition is detected.

What is hot swappable power supply?

Hot pluggable power supply units (PSUs) can be removed and installed while the server is running without affecting the rest of the server's capabilities. Hot plugging or hot swapping gets its name because devices are swapped while a computer is running - or hot.

What is how swap?

Hotswap (also hot-swap or hot swap) is an acclaimed feature that Kono Store and Input Club adopted early. It describes keyboards that allow switch replacement without soldering. Kaihua (Kailh), a premier switch manufacturer in China, created the market-dominating hotswap socket design pictured below.


1 Answers

You can provide a custom event handler for AssemblyResolve by calling newAppDomain() below. Supply your directory so AppDomain looks there. When loading a Type, use function loadFromAppDomain() to return it. This should allow you to copy new dlls to C:\dlls at runtime and reload from there. (Forgive me, I translated this from my VB source to C# according to your tag.)

String dllFolder = "C:\\dlls";

public void newAppDomain()
{
    AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(assemblyResolve);
}

private static Assembly assemblyResolve(Object sender, ResolveEventArgs args){
    String assemblyPath = Path.Combine(dllFolder, new AssemblyName(args.Name).Name + ".dll");
    if(!File.Exists(assemblyPath))
    {
        return null;
    }
    else
    {
        return Assembly.LoadFrom(assemblyPath);
    }
}

private Type loadFromAppDomain(String className)
{
    Assembly[] asses = AppDomain.CurrentDomain.GetAssemblies();
    List<Type> types = new List<Type>();
    foreach(Assembly ass in asses)
    {
        Type t = ass.GetType(className);
        if(t != null) types.Add(t);
    }
    if(types.Count == 1)
        return types.First();
    else
        return null;
}
like image 89
djv Avatar answered Sep 20 '22 13:09

djv