gpt4 book ai didi

asp.net-mvc - 向 Controller 构造函数注入(inject)服务时出现不一致的可访问性参数类型错误

转载 作者:行者123 更新时间:2023-12-01 07:54:29 24 4
gpt4 key购买 nike

而向 Controller 注入(inject)服务显示错误“不一致的可访问性:参数类型'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



引导码
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){}

最佳答案

让您的IProductRepository公共(public)接口(interface)。

public interface IProductRepository 
{
// your properties & methods
}

关于asp.net-mvc - 向 Controller 构造函数注入(inject)服务时出现不一致的可访问性参数类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31804374/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com