向Controller-Constructor注入服务时,可访问性参数类型错误不一致

时间:2015-08-04 08:32:04

标签: asp.net-mvc asp.net-mvc-4 inversion-of-control

向控制器注入服务时显示错误"不一致的可访问性:参数类型' IProductRepository'比方法产品更容易访问' ProductController.ProductController(IProductRepository)'"

使用的工具/框架:visualstudio_2015,mvc 4.6和unity_Container_4

我对此非常陌生" IoC" ,我想我错过了什么

错误代码

private IProductRepository _IobjProductRepository;
//error shown in this code part
 Injecting Service to Controller shows error -->
public ProductController(IProductRepository Repository)
 { _IobjProductRepository = Repository;} 
 <--error shown in this code par

t

BootStrapcode

private static IUnityContainer BuildUnityContainer()
    {
      var container = new UnityContainer();
      container.RegisterType<IProductRepository,ProductRepository>();
      container.RegisterType<IController,ProductController>();
            // register all your components with the container here
            // it is NOT necessary to register your controllers

            // e.g. container.RegisterType<ITestService, TestService>();  
            // MvcUnityContainer.Container = container;

            RegisterTypes(container);  
            return container;
    }
    public static void RegisterTypes(IUnityContainer container){}

1 个答案:

答案 0 :(得分:6)

使您的IProductRepository成为公共界面。

public interface IProductRepository 
{
  // your properties & methods
}