能够在运行时替换程序集

能够在运行时替换程序集

本文介绍了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.

我需要做什么才能实现这一目标?

What do I have to do to achieve this?

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:

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

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

07-31 12:26