StructureMap - Ability to replace an assembly at runtime

与世无争的帅哥 提交于 2020-02-08 02:42:07

问题


Example:

Console application:

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

Assembly "Interface":

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

Assembly "Implemenation":

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

Assembly "Resolver"

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

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?

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

EDIT:

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


回答1:


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

From 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.




回答2:


I think this has what you're looking for:

http://structuremap.net/structuremap/ChangingConfigurationAtRuntime.htm



来源:https://stackoverflow.com/questions/7505812/structuremap-ability-to-replace-an-assembly-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!