2

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?

3
  • Did you add using StructureMap; to the top of the code file so you can call the Populate exension method? Commented Jan 31, 2018 at 0:53
  • 1
    jeremydmiller.com/2018/01/29/sunsetting-structuremap With the sunsetting statement, probably you should consider moving to an alternative. Commented Jan 31, 2018 at 0:59
  • NightOwl888, of course I did add structure map name space, otherwise I wouldn't be able to create Container instance in the first place. Commented Jan 31, 2018 at 12:23

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.