是否有对热插拔的.N​​ET参考实现? [英] Are there reference implementations of hot-swapping in .NET?

查看:303
本文介绍了是否有对热插拔的.N​​ET参考实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一个好的实现热插拔.NET中完成的。我需要的东西是:

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

  • 如果能够在一个特定的文件夹将部署DLL,有一个正在运行的系统接他们回家。
  • 在具有相应的容器引用正在运行的系统更新。

我一直在寻找到MEF和其目录加载机制,但似乎非常不可靠。也许有人在那里有一个可选的实现?

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

推荐答案

您可以通过调用newAppDomain提供()以下的自定义事件处理程序AssemblyResolve。提供您的目录中,这样的AppDomain看起来那里。当加载一个类型,使用功能loadFromAppDomain()返回它。这应该允许您复制新的DLL到C:\ dll文件在运行时,并从那里重新加载。 (请原谅我,我根据自己的标记从我的VB源到C#翻译这一点。)

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;
}

这篇关于是否有对热插拔的.N​​ET参考实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆