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();
}
}
程序集“Interface”:
public interface ICalculator
{
int Calculate(int a, int b);
}
程序集“Implementation”:
internal class Calculator : ICalculator
{
public int Calculate(int a, int b)
{
return a + b;
}
}
程序集“Implementation”,该程序集应在运行时替换上面的程序集:
internal class Calculator : ICalculator
{
public int Calculate(int a, int b)
{
return a * b;
}
}
程序集“Resolver”
For<ICalculator>().Use<Calculator>();
我想在运行时替换具体实现。这可以通过 UpdateService 来完成,它只是替换旧的程序集“Implementation”。
我遇到的问题是程序集“实现”被锁定。我无法更换它。
我需要做什么才能实现这一目标?
IoC 容器是否满足我的要求,还是我必须构建自己的基础设施?
编辑:
在网络环境中,您可以轻松替换程序集。我已经成功地做到了这一点。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
恐怕你只能加载一个额外的程序集。
来自 MSDN:
I'm afraid you can only load an additional assembly.
From MSDN:
我认为这就是您正在寻找的内容:
http://structuralmap.net/structuralmap/ChangingConfigurationAtRuntime.htm< /a>
I think this has what you're looking for:
http://structuremap.net/structuremap/ChangingConfigurationAtRuntime.htm