The current type is an interface and cannot be con

2019-06-17 02:48发布

I have a controller class that takes care for the double click command and in turn calls a method that popups a window to the user. Something like :

var popup = container.GetService<PopupCommand>();

In the above line it throws an error saying : The current type, PopupCommand.IPopupDataHandler, is an interface and cannot be constructed. Are you missing a type mapping?

I updated the DLL that contained a method for container.GetService(), before that it used to work fine.

I tried searching in google, but the issues similar are more related to Unity, and i doubt whether my issue is anywhere related to Unity.

2条回答
不美不萌又怎样
2楼-- · 2019-06-17 03:34

Basically the compiler tells you that you are trying to instantiate an interface.

container.GetService<PopupCommand>() probably brings you back an interface named PopupCommand.IPopupDataHandler, you probably need to cast it to the type you need or change the type to an object, you should also check the constrains of the method - it could be missing the new constraint.

查看更多
再贱就再见
3楼-- · 2019-06-17 03:47

Try Addin DefaultController Factory for Registering your controller. Three Steps :Step 1 1.Add a Class DefaultControllerFactory in your Project

public class ControllerFactory :DefaultControllerFactory
    {
        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            try
            {
                if (controllerType == null)
                    throw new ArgumentNullException("controllerType");

                if (!typeof(IController).IsAssignableFrom(controllerType))
                    throw new ArgumentException(string.Format(
                        "Type requested is not a controller: {0}",
                        controllerType.Name),
                        "controllerType");

                return MvcUnityContainer.Container.Resolve(controllerType) as IController;
            }
            catch
            {
                return null;
            }

        }
        public static class MvcUnityContainer
        {
            public static UnityContainer Container { get; set; }
        }
    }

Step 2:Register it in Bootstrap class in BuildUnityContainer Method

private static IUnityContainer BuildUnityContainer()
    {
      var container = new UnityContainer();

      // register all your components with the container here
      // it is NOT necessary to register your controllers

      // e.g. container.RegisterType<ITestService, TestService>();    
      //RegisterTypes(container);
      container = new UnityContainer();
      container.RegisterType<IProductRepository, ProductRepository>();


      UnityInterceptionExample.Models.ControllerFactory.MvcUnityContainer.Container = container;
      return container;
    }

Step 3:And Register it in The Global.asax File

protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();
            Bootstrapper.Initialise();
            ControllerBuilder.Current.SetControllerFactory(typeof(ControllerFactory));
        } 

And finisihed. May be this will work for you... Happy Coding.

查看更多
登录 后发表回答