The current type is an interface and cannot be constructed. Do you miss type mapping?

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

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

An error message is displayed in the above line: The current type, PopupCommand.IPopupDataHandler, is an interface and cannot be constructed. Do you miss type mapping?

I updated the DLL containing the container.GetService () method, before that it worked fine.

I tried to search on google, but similar problems are more related to Unity, and I doubt that my problem is related to Unity.

+8
c # types mapping wpf
source share
2 answers

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

container.GetService<PopupCommand>() probably returns you an interface named PopupCommand.IPopupDataHandler , you probably need to assign it to the type you need, or change the type of the object, you should also check the limitations of the method - it may not have a restriction new .

+1
source share

Try Addin DefaultController Factory to register your controller. Three steps: Step 1 1. Add the DefaultControllerFactory class to 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 the Bootstrap class in the 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 // eg container.RegisterType<ITestService, TestService>(); //RegisterTypes(container); container = new UnityContainer(); container.RegisterType<IProductRepository, ProductRepository>(); UnityInterceptionExample.Models.ControllerFactory.MvcUnityContainer.Container = container; return container; } 

Step 3: 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 finished. Maybe this will work for you ... Happy coding.

0
source share

All Articles