在 ac# 控制台应用程序中设置结构图
我收到此错误:
StructureMap Exception Code: 202
No Default Instance defined for PluginFamily
我的设置如下所示:
Console.WriteLine("Structure Map");
SetupSM sm = new SetupSM();
sm.Setup();
ISomeThing someThing = ObjectFactory.GetInstance<ISomeThing>();
Console.WriteLine("something.HowManyTHings: " + someThing.HowManyThings("asdf"));
public class SetupSM
{
public void Setup()
{
var c1 = new Container(config =>
{
config.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
});
});
var c2 = new Container(x =>
{
x.For<ISomeThing>().Use<SomeThingOne>();
});
}
}
这是我第一次尝试使用结构图,我缺少什么? 他们主网站上的指南似乎很旧,使用旧语法等。
I'm getting this error:
StructureMap Exception Code: 202
No Default Instance defined for PluginFamily
My setup looks like:
Console.WriteLine("Structure Map");
SetupSM sm = new SetupSM();
sm.Setup();
ISomeThing someThing = ObjectFactory.GetInstance<ISomeThing>();
Console.WriteLine("something.HowManyTHings: " + someThing.HowManyThings("asdf"));
public class SetupSM
{
public void Setup()
{
var c1 = new Container(config =>
{
config.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
});
});
var c2 = new Container(x =>
{
x.For<ISomeThing>().Use<SomeThingOne>();
});
}
}
This is my first try at using structure map, what am I missing?
It seems the guide on their main website is very old using the old syntax etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试将您的配置应用于静态 ObjectFactory,而不是您似乎立即扔掉的单独容器。
Try applying your configuration to the static ObjectFactory instead of seperate containers which you appear to throw away immediately..
前面的答案适用于 StructureMap 2 或 3,但在 StructureMap 4 中语法已更改。您可以在此处查看完整的解决方案:
http://ardalis.com/using -structmap-4-in-a-console-app
本质上,ObjectFactory 已被 Container 实例取代。您还可以选择在单独的注册表实例中执行配置(建议这样做)。因此,Main() 中的初始化代码变为:
这使 Main 非常干净,并允许应用程序通过 请求所需的任何依赖项依赖注入并遵循显式依赖关系原则。结合起来,这些使您的应用程序变得非常可组合和可测试。
The previous answers are for StructureMap 2 or 3, but in StructureMap 4 the syntax has changed. You can see a complete solution here:
http://ardalis.com/using-structuremap-4-in-a-console-app
Essentially, ObjectFactory has been replaced with a Container instance. You can also optionally perform the configuration in a separate Registry instance, which is recommended. Thus your initialization code in Main() becomes:
This keeps Main very clean, and allows Application to request any dependencies it needs through dependency injection and to follow the Explicit Dependencies Principle. Combined, these allow your application to be very composable and testable.
您的控制台应用程序:
Your console app: