Serenkiy
Serenkiy

Reputation: 309

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.

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:

enter image description here

What's wrong with my code?

Upvotes: 2

Views: 2077

Answers (2)

Fevzi Ozgul
Fevzi Ozgul

Reputation: 56

You should also add the following nuget package to your project in order to use the Populate method of the Configuration option.

The package name: StructureMap.Microsoft.DependencyInjection

You do not have to import this library to the startup class though. "using StructureMap" there handles everything.

Upvotes: 4

Serenkiy
Serenkiy

Reputation: 309

I decided to change IoC to Autofac. And the same problem appeared. I was following autofac documentation for asp.net core and skip a little detail. 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. 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.

!Read documentation extremely attentively!

Upvotes: 0

Related Questions