结构映射新手 - 获取 StructureMap 异常代码:202 没有为 PluginFamily 定义默认实例

发布于 2024-10-09 23:18:49 字数 906 浏览 0 评论 0原文

我第一次尝试 StructureMap 并收到以下编译器错误,

StructureMap Exception Code:  202
No Default Instance defined for PluginFamily Super.SuperCore.Core.DataAccess.IPersonRepository, Super.SuperCore, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

调用接口的地方:

        private IPersonRepository _iPersonRepository;

        public void Init() {
            _iPersonRepository = ObjectFactory.GetInstance<IPersonRepository>();
        }

我的接口声明:

[PluginFamily("Default")]
    public interface IPersonRepository
    {
        List<string> getAllNames();
    }

我的 StructureMap.config:

<?xml version="1.0" encoding="utf-8" ?>
<StructureMap>
<Assembly Name="Super.SuperWeb" />
<Assembly Name="Super.SuperCore" />
</StructureMap>

任何人都可以指出我到底哪里出错了。

Am trying out structuremap for the first time and am getting the following compiler error,

StructureMap Exception Code:  202
No Default Instance defined for PluginFamily Super.SuperCore.Core.DataAccess.IPersonRepository, Super.SuperCore, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

The place where am calling the interface:

        private IPersonRepository _iPersonRepository;

        public void Init() {
            _iPersonRepository = ObjectFactory.GetInstance<IPersonRepository>();
        }

My Interface Declaration:

[PluginFamily("Default")]
    public interface IPersonRepository
    {
        List<string> getAllNames();
    }

My StructureMap.config:

<?xml version="1.0" encoding="utf-8" ?>
<StructureMap>
<Assembly Name="Super.SuperWeb" />
<Assembly Name="Super.SuperCore" />
</StructureMap>

Can anyone point out where exactly am I going wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

热血少△年 2024-10-16 23:18:49

首先,这不是编译器错误,这是一个例外,有很大的差异,但我不会详细讨论。

在您的配置中,您似乎正在混合和匹配配置方法。我通常会选择一个并坚持下去。这是文档

如果你想走属性路线,你必须告诉 SM 扫描具有属性的类

如果您想走 xml 路线,请确保您的 Structuremap.config 正在获取复制到exe文件夹中。我不认为你应该这样做,但你可以像这样从 xml 配置中显式加载

ObjectFactory.Initialize(x =>
            {
                x.AddConfigurationFromXmlFile("StructureMap.config");
            });

如果你想要强类型和编译器检查的东西,请尝试注册表 dsl,它是更现代的配置机制。

First, that's not a compiler error, that's an exception, there is a very large difference, I won't go into that though.

In your configuration, you appear to be mixing and matching configuration methods. I typically pick one and stick to it. Here's the documentation

If you want to go the attribute route, you have to tell SM to scan for classes with attributes

If you want to go the xml route, make sure your structuremap.config is getting copied to the folder with the exe. I don't think you should have to, but you can explicitly load from the xml config like this

ObjectFactory.Initialize(x =>
            {
                x.AddConfigurationFromXmlFile("StructureMap.config");
            });

If you want something that is strongly typed and compiler checked, try the registry dsl, it's the more modern configuration mechanism.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文