StructureMap错误 - 未注册默认实例

时间:2018-03-11 07:40:36

标签: c# dependency-injection integration-testing structuremap structuremap4

我有一个使用StructureMap IoC容器的控制台演示应用程序。该演示将所有接口和实现都集中在一个项目中的一个文件中,扫描注册表如下所示:

public class ConsoleRegistry : Registry
{
    public ConsoleRegistry()
    {
        Scan(scan =>
        {
            scan.TheCallingAssembly();
            scan.WithDefaultConventions();
        });
    }
}

该演示使用约定ISomethingSomething,因此StructureMap可以自动查找接口的实现。

现在,当我将其转移到一个有 UI项目和业务项目的真实项目时。我保留ISomethingSomething的约定,但是当我尝试在单元测试项目中运行集成测试时,我收到以下错误消息。

  

消息:测试方法   AbcCompany.Tests.IntegrationTestsForTasks.Get_something_test扔了   exception:StructureMap.StructureMapConfigurationException:No   默认实例已注册,无法自动确定   对于类型' AbcCompany.DomainLayer.ISomething'

     

没有指定配置   AbcCompany.DomainLayer.ISomething   1.)Container.GetInstance()

如果我将注册表更改为以下内容,则可以正常工作:

class ScanningRegistry : Registry
{
    public ScanningRegistry()
    {
        this.For<ISomething>().Use<Something>();

        this.Policies.SetAllProperties(y => y.WithAnyTypeFromNamespaceContainingType<Something>());
    }
}

但是,我喜欢这样,如果我保持标准命名约定,StructureMap将为我找到我的所有接口和实现,而不必指定它们。

1 个答案:

答案 0 :(得分:0)

您只是在扫描TheCallingAssembly。运行应用程序时,调用程序集就是您的应用程序。当测试运行器运行时,调用程序集是测试运行器。

为了使其可靠,您应手动指定每个程序集:

Scan(scan =>
{
    scan.Assembly(typeof(SomeTypeFromAssembly1).Assembly);
    scan.Assembly(typeof(SomeTypeFromAssembly2).Assembly);
    scan.WithDefaultConventions();
 });

或者您应该使用the scanning documentation中的其他方法之一来按目录名指定程序集。

相关问题