gpt4 book ai didi

c# - 如何在 Structuremap 中使用自动注册和类型扫描

转载 作者:行者123 更新时间:2023-11-30 22:01:36 25 4
gpt4 key购买 nike

在我的解决方案中,我有三个这样的项目。 enter image description here

我将 Common.dll 和 Service.dll 复制到 d:\libs 之类的文件夹中,并使用下面的代码和类型扫描

 ObjectFactory.Initialize(x =>
{
x.Scan(xx =>
{
xx.AssembliesFromPath(@"d:\libs");
xx.LookForRegistries();
});
});
//I have PersonService that implement IPersonService
namespace Common
{
public interface IPersonService
{
string SayHello(string name);
}
}
namespace Services
{
public class PersonService : IPersonService
{
public string SayHello(string name)
{
return string.Format("Hello {0}", name);
}
}
}

当我想从 IPerson 获取实例时初始化我的依赖项后出现此错误

  var personService = ObjectFactory.GetInstance<IPersonService>();

{"No default Instance is registered and cannot be automatically determined for type 'Common.IPersonService'\r\n\r\nThere is no configuration specified for Common.IPersonService\r\n\r\n1.) Container.GetInstance(Common.IPersonService)\r\n"}

最佳答案

  • 同时添加 xx.WithDefaultConventions();
  • 当您使用 StructureMap 设计插件系统时,宿主项目不应引用任何插件。仅应引用接口(interface)/契约(Contract)插件,不应将此程序集复制到 d:\libs 文件夹。换句话说,当前应用程序域不应该有任何程序集的 2 个实例。所以如果你想直接在宿主程序中使用IPersonService接口(interface),添加对Common.dll的引用,不要将它复制到d:\libs 文件夹以避免重复。现在宿主项目也不应该引用 Services.dll

关于c# - 如何在 Structuremap 中使用自动注册和类型扫描,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27571262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com