Skip to content

Instantly share code, notes, and snippets.

@gavilanch
Created November 19, 2017 14:09
Show Gist options
  • Save gavilanch/c159f3fe7554544ab16e4d5ffe11b891 to your computer and use it in GitHub Desktop.
Save gavilanch/c159f3fe7554544ab16e4d5ffe11b891 to your computer and use it in GitHub Desktop.
SimpleInjectorInitializer con el error de ASP.NET MVC 5 accounController corregido
[assembly: WebActivator.PostApplicationStartMethod(typeof(web2.App_Start.SimpleInjectorInitializer), "Initialize")]
namespace web2.App_Start
{
using System.Reflection;
using System.Web.Mvc;
using SimpleInjector;
using SimpleInjector.Integration.Web;
using SimpleInjector.Integration.Web.Mvc;
using Web2;
using SimpleInjector.Advanced;
using System;
using System.Linq;
public static class SimpleInjectorInitializer
{
/// <summary>Initialize the container and register it as MVC3 Dependency Resolver.</summary>
public static void Initialize()
{
var container = new Container();
container.Options.ConstructorResolutionBehavior = new LeastParametersConstructorBehavior();
container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();
InitializeContainer(container);
container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
container.Verify();
DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
}
private static void InitializeContainer(Container container)
{
// For instance:
// container.Register<IUserRepository, SqlUserRepository>(Lifestyle.Scoped);
}
}
public class LeastParametersConstructorBehavior : IConstructorResolutionBehavior
{
public ConstructorInfo GetConstructor(Type implementationType) => (
from ctor in implementationType.GetConstructors()
orderby ctor.GetParameters().Length ascending
select ctor)
.First();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment