问题
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