简体   繁体   English

结构图方法Populate()对ASP.NET Core不起作用

[英]Structure Map method Populate() doesn't work for ASP.NET Core

According to StructureMap documentation and examples from StructureMap.Microsoft.DependencyInjection repository it has to work but it doesn't. 根据StructureMap文档和来自StructureMap.Microsoft.DependencyInjection存储库的示例 ,它必须可以工作,但是不需要。

Here is my Startup class: 这是我的启动课程:

    public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddTransient<IMovieRepository, MovieRepository>();

        var container = new Container();
        container.Configure(config =>
        {
            config.AddRegistry(new MyRegistry());
            config.Populate(services);
        });

        return container.GetInstance<IServiceProvider>();
    }

And Registry: 和注册表:

public class MyRegistry : Registry
    {
        public MyRegistry()
        {
            For<IMovieRepository>().Transient().Use<MovieRepository>();
        }
    }

And here is error screenshot: 这是错误屏幕截图:

在此处输入图片说明

What's wrong with my code? 我的代码有什么问题?

You should also add the following nuget package to your project in order to use the Populate method of the Configuration option. 您还应该将以下nuget包添加到项目中,以便使用Configuration选项的Populate方法。

The package name: StructureMap.Microsoft.DependencyInjection 包名称:StructureMap.Microsoft.DependencyInjection

You do not have to import this library to the startup class though. 您不必将此库导入到启动类。 "using StructureMap" there handles everything. 那里的“使用StructureMap”可以处理所有事情。

I decided to change IoC to Autofac. 我决定将IoC更改为Autofac。 And the same problem appeared. 并出现了相同的问题。 I was following autofac documentation for asp.net core and skip a little detail. 我正在关注asp.net core的autofac文档,并略过了一些细节。 It took three days to figure out that I referenced to the wrong package. 花了三天的时间才弄清楚我引用了错误的软件包。 I referenced to the autofac package when what I was truly need was Autofac.Extensions.DependencyInjection package. 当我真正需要的是Autofac.Extensions.DependencyInjection程序包时,我引用了autofac程序包。 It's ridiculous mistake that kick me off for a three days. 这是一个荒谬的错误,使我踢了三天。 I am truly convinced that the same kind of mistake I did with structure map, so just look for StructureMap.AspNetCore package instead of StructureMap package and everything will work. 我确实确信与结构图一样的错误,因此只需查找StructureMap.AspNetCore包而不是StructureMap包,一切都会正常进行。

!Read documentation extremely attentively! !要非常仔细地阅读文档!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM