开发者

StructureMap - Ability to replace an assembly at runtime

开发者 https://www.devze.com 2023-04-06 07:39 出处:网络
Example: Console application: class Program { static void Main(string[] args) { var calculator = ObjectFactory.GetInstance<ICalculator>();

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

Assemb开发者_运维问答ly "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.


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.


I think this has what you're looking for:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号