Marian Ene
Marian Ene

Reputation: 665

StructureMapConfigurationException

I'm building and ASP.NET MVC 4.5. I've added StructureMap to my project. I've created in my website and Ioc class:

public static IContainer Initialize()
    {
        var connectionString = ConfigurationManager.ConnectionStrings["Ebancomat"].ConnectionString;

        ObjectFactory.Initialize(x =>
        {
            x.For<Ebancomat.DataAdapters.IncomeExpenses.IIncomeExpensesDataAdapter>().Use<Ebancomat.DataAdapters.IncomeExpenses.IncomeExpensesDataAdapter>().Ctor<string>("connectionString").Is(connectionString);
            x.For<Ebancomat.Repositories.IncomeExpenses.IIncomeExpensesRepository>().Use<Ebancomat.Repositories.IncomeExpenses.IncomeExpensesRepository>();
        });

        return ObjectFactory.Container;
    }

and a StructureMapControllerFactory class:

public class StructureMapControllerFactory : DefaultControllerFactory
{
    private readonly IContainer container;

    public StructureMapControllerFactory(IContainer container)
    {
        this.container = container;
    }

    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        var instance = container.GetInstance(controllerType) as IController;

        if (instance == null)
        {
            return base.GetControllerInstance(requestContext, controllerType);
        }

        return instance;
    }
}

I've added this isn my Global.asax.cs:

var container = Ioc.Initialize();
        ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory(container));

When I've first run the application, a window had opened and it wanted Container.cs . I didn't find that class, I pressed Cancel and since then I get this error and I don't understand why. (I tried the same code at work and there it was working perfectly)

An exception of type 'StructureMap.StructureMapConfigurationException' occurred in StructureMap.dll but was not handled in user code
Additional information: No default Instance is registered and cannot be automatically determined for type 'IUserStore<ApplicationUser>'

Upvotes: 4

Views: 4181

Answers (3)

gadildafissh
gadildafissh

Reputation: 2402

Using the other answers posted here I was able to find where my issue was. I have to edit my StructureMapInitializer.cs file to include a couple more lines so the Repository and Service classes I created would be found. Also, the Repository is pulling from a view instead of a table.

    public static IContainer Initialize()
    {
        var container = new Container();

        container.Configure(x =>
        {
            ...
            x.For<IDetailedEventViewRepository>().Use<DetailedEventViewRepository>();
            x.For<IDetailedEventViewService>().Use<DetailedEventViewService>();
            ...
            MapBuilder.Wireup(x);
            ...
        });
        ...
        return container;
    }

Upvotes: 0

LetMeCodeThis
LetMeCodeThis

Reputation: 591

I assume that IUserStore<ApplicationUser> is being resolved via constructor injection and SM cant resolve it because it is not registered within container. So all you need is to register IUserStore<ApplicationUser> in the container.

I would also get rid of ObjectFactory in favour of new Container

public static IContainer Initialize()
{
    var connectionString = ConfigurationManager.ConnectionStrings["Ebancomat"].ConnectionString;

    return new Container(
        c =>
            {
                c.For<Ebancomat.DataAdapters.IncomeExpenses.IIncomeExpensesDataAdapter>()
                    .Use<Ebancomat.DataAdapters.IncomeExpenses.IncomeExpensesDataAdapter>()
                    .Ctor<string>("connectionString")
                    .Is(connectionString);
                c.For<Ebancomat.Repositories.IncomeExpenses.IIncomeExpensesRepository>()
                    .Use<Ebancomat.Repositories.IncomeExpenses.IncomeExpensesRepository>();
                c.For<IUserStore<ApplicationUser>>().Use<DatabaseUserStore>();
        });
}

Upvotes: 1

sh1rts
sh1rts

Reputation: 1874

It's asking for Container.cs because that is the class within StructureMap that's throwing the exception; since you're (I assume) referencing the StructureMap.DLL binary, you won't have the source. I wouldn't worry about that.

It looks like you're calling ObjectFactory.GetInstance on a type that you haven't registered.

Your ObjectFactory.Initialize registers concrete types for IIncomeExpensesDataAdapter and IIncomeExpensesRepository, but I can't see anything for IUserStore<ApplicationUser> or IUserStore<T>

Can you search your code for ObjectFactory.GetInstance and see if there's anywhere you're using this without registering the object graph in StructureMap ?

Upvotes: 1

Related Questions