Inyectar en un proveedor de membresía personalizado con StructureMap

I worked a little with StructureMap and I managed to inject in my controller (through constructor injection) a concrete type repository for an interface.

Now, I need to inject a repository type into my custom membership provider. But how? My custom membership provider is created through Membership.Provider.ValidateUser (por ejemplo).

For controller I created a class like this:

public class IocControllerFactory : DefaultControllerFactory
{
    protected override IController GetControllerInstance(
        System.Web.Routing.RequestContext requestContext, 
        Type controllerType)
    {
        return (Controller)
            ObjectFactory.GetInstance(controllerType);
    }
}

y en Global.asaxen Application_Start():

//...
ObjectFactory.Initialize(x =>
{
    x.AddRegistry(new ArticleRegistry());
}
                                    );


ControllerBuilder.Current.SetControllerFactory(
    new IocControllerFactory());
//...

But how inject an concrete type in my custom membership provider with StructureMap?

preguntado el 31 de julio de 12 a las 12:07

1 Respuestas

There'a no way to inject in this case I think.

Injection only works when the "factory" creating the instances required, know about injection, and the Membership provider area unfortunately is showing it's age. It was coded back when things were done differently :).

If you're using your custom membership provider with ASP.NET MVC (version 2+), you can get what you need by requesting it from the DependencyResolver like so:

DependencyResolver.Current.GetService<IRepository<User>>();

Or you could probably use ObjectFactory directly.

Respondido 31 Jul 12, 12:07

I not understand.I use this in my custom membership provider and not write anything in and in Global.asax, in Application_Start() ? - Obstruido

No, you still need to add your custom Dependency Resolver in the Application_Start() or in a bootstrap type class like DependencyResolver.SetResolver(new StructureMapDependencyResolver(container)); where StructureMapDependencyResolver is your custom class and container is your StructureMap IContainer. more info here: ericsowell.com/blog/2011/1/20/… - Stephenbayer

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.