StructureMap-能够在运行时替换装配件 [英] StructureMap - Ability to replace an assembly at runtime

查看:111
本文介绍了StructureMap-能够在运行时替换装配件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例:

控制台应用程序:

class Program
{
    static void Main(string[] args)
    {
        var calculator = ObjectFactory.GetInstance<ICalculator>();
        for (var i = 0; i < 10; i++)
        {
            Console.WriteLine(calculator.Calculate(10, 5));
            Console.ReadLine();
        }
        Console.ReadLine();
    }
}

装配接口":

public interface ICalculator
{
    int Calculate(int a, int b);
}

程序集提示":

internal class Calculator : ICalculator
{
    public int Calculate(int a, int b)
    {
        return a + b;
    }
}

程序集实现",该程序集应在运行时替换上面的程序集:

Assembly "Implemenation", this assembly shall replace the assembly above at runtime:

internal class Calculator : ICalculator
{
    public int Calculate(int a, int b)
    {
        return a * b;
    }
}

装配旋转变压器"

For<ICalculator>().Use<Calculator>();

我想在运行时替换具体的实现.这可以通过UpdateService来完成,该服务只需替换旧的程序集"Implementation"即可.

I want to replace the concrete implementation at runtime. This could be done by an UpdateService which just replace the old assembly "Implementation".

我遇到的问题是程序集实现"被锁定.我无法替换.

The problem I have is that the assembly "Implementation" is locked. I can't replace it.

我该怎么做?

IoC容器是否负责我的需求,还是我必须构建自己的基础架构?

Is the IoC container responsible for my requirement or do I have to build my own infrastructure?

在Web环境中,您可以轻松地替换部件.我已经成功地做到了这一点.

In a web environment you can easily replace an assembly. I did this already with success.

推荐答案

恐怕您只能加载其他程序集.

I'm afraid you can only load an additional assembly.

来自 MSDN :

如果不卸载所有部件,则无法卸载单个部件 包含它的应用程序域.即使组装进行了 超出范围,实际的程序集文件将保持加载状态,直到所有 包含它的应用程序域将被卸载.

There is no way to unload an individual assembly without unloading all of the application domains that contain it. Even if the assembly goes out of scope, the actual assembly file will remain loaded until all application domains that contain it are unloaded.

这篇关于StructureMap-能够在运行时替换装配件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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