none
Not able inject AutoMapper by Unity container to Controller RRS feed

  • Question

  • Not able inject  AutoMapper  by Unity container to Controller 

     public class AutoMapperProfile : Profile
        {
            public AutoMapperProfile()
            {            
                CreateMap<User, UserViewModel>();         
    
            }
        }

    Global.asax:

    namespace JSW.WebSite { public class MvcApplication : System.Web.HttpApplication { private static IUnityContainer UnityContainer { get; set; } protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RouteConfig.RegisterRoutes(RouteTable.Routes); //---maper configuration UnityContainer = UnityConfig.GetConfiguredContainer(); var mapperConfiguration = new MapperConfiguration(cfg => { cfg.AddProfiles(typeof(AutoMapperProfile)); // here addnig new profiles from projects }); UnityContainer.RegisterInstance(mapperConfiguration.CreateMapper(), new ContainerControlledLifetimeManager()); UnityContainer.RegisterInstance<IConfigurationProvider>(mapperConfiguration, new ContainerControlledLifetimeManager()); Bootstrapper.Initialise(); }

    } }


    public class Bootstrapper { public static IUnityContainer Initialise() { var container = BuildUnityContainer(); DependencyResolver.SetResolver(new UnityDependencyResolver(container)); return container; } private static IUnityContainer BuildUnityContainer() { var container = new UnityContainer(); container.RegisterTypeWithDefaultConstructorPerRequest<DbContext, JSWDbContext>(); container.RegisterType<ITestService, TestService>(); container.RegisterType<IMapper, Mapper>(); RegisterTypes(container); return container; } public static void RegisterTypes(IUnityContainer container) { } }

     public class SubscriptionController : Controller
        {
            DbContext dbContext;
            IMapper mapper;
            public SubscriptionController(DbContext dbContext, IMapper mapper)
            {
                this.dbContext = dbContext;
                this.mapper = mapper;
            }
        }
    }

    The current type, AutoMapper.IConfigurationProvider, is an interface and cannot be constructed. Are you missing a type mapping?

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: System.InvalidOperationException: The current type, AutoMapper.IConfigurationProvider, is an interface and cannot be constructed. Are you missing a type mapping?

    Source Error: 
    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Stack Trace: 
    [InvalidOperationException: The current type, AutoMapper.IConfigurationProvider, is an interface and cannot be constructed. Are you missing a type mapping?]
       Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.ThrowForAttemptingToConstructInterface(IBuilderContext context) +184
       lambda_method(Closure , IBuilderContext ) +24
       Microsoft.Practices.ObjectBuilder2.<>c__DisplayClass1.<GetBuildMethod>b__0(IBuilderContext context) +35
       Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context) +10
       Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) +196
       Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) +209
       Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp(NamedTypeBuildKey newBuildKey) +73
       Microsoft.Practices.Unity.ObjectBuilder.NamedTypeDependencyResolverPolicy.Resolve(IBuilderContext context) +48
       lambda_method(Closure , IBuilderContext ) +128
       Microsoft.Practices.ObjectBuilder2.<>c__DisplayClass1.<GetBuildMethod>b__0(IBuilderContext context) +35
       Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context) +10
       Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) +196
       Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) +209
       Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp(NamedTypeBuildKey newBuildKey) +73
       Microsoft.Practices.Unity.ObjectBuilder.NamedTypeDependencyResolverPolicy.Resolve(IBuilderContext context) +48
       lambda_method(Closure , IBuilderContext ) +255
       Microsoft.Practices.ObjectBuilder2.<>c__DisplayClass1.<GetBuildMethod>b__0(IBuilderContext context) +35
       Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context) +10
       Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) +196
       Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) +209
       Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides) +165
    
    [ResolutionFailedException: Resolution of the dependency failed, type = "JSW.WebSite.Areas.Subscription.Controllers.SubscriptionController", name = "(none)".
    Exception occurred while: while resolving.
    Exception is: InvalidOperationException - The current type, AutoMapper.IConfigurationProvider, is an interface and cannot be constructed. Are you missing a type mapping?
    -----------------------------------------------
    At the time of the exception, the container was:
    
      Resolving JSW.WebSite.Areas.Subscription.Controllers.SubscriptionController,(none)
      Resolving parameter "mapper" of constructor JSW.WebSite.Areas.Subscription.Controllers.SubscriptionController(System.Data.Entity.DbContext dbContext, AutoMapper.IMapper mapper)
        Resolving AutoMapper.Mapper,(none) (mapped from AutoMapper.IMapper, (none))
        Resolving parameter "configurationProvider" of constructor AutoMapper.Mapper(AutoMapper.IConfigurationProvider configurationProvider, System.Func`2[[System.Type, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] serviceCtor)
          Resolving AutoMapper.IConfigurationProvider,(none)
    ]
       Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides) +329
       Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name, ResolverOverride[] resolverOverrides) +15
       Microsoft.Practices.Unity.UnityContainerExtensions.Resolve(IUnityContainer container, Type t, ResolverOverride[] overrides) +18
       Microsoft.Practices.Unity.Mvc.UnityDependencyResolver.GetService(Type serviceType) +76
       MvcSiteMapProvider.DI.DependencyResolverDecorator.GetService(Type serviceType) +125
       System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +41
    
    [InvalidOperationException: An error occurred when trying to create a controller of type 'JSW.WebSite.Areas.Subscription.Controllers.SubscriptionController'. Make sure that the controller has a parameterless public constructor.]
       System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +178
       System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +80
       System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +102
       System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +188
       System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +50
       System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +48
       System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
       System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +443
       System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +132
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +163
    


    Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.7.3282.0


    Wednesday, March 27, 2019 5:39 PM

All replies

  • Hi Marek35,

    Thank you for posting here.

    Since your question is more related to MVC, you could post a new thread in ASP.NET forum for suitable support.

    https://forums.asp.net/

    The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, March 28, 2019 10:07 AM