C# 我必须做什么才能使我的接口至少与引用它的方法一样可访问?
我正在尝试将我的控制器转换为使用DI 基于,我现在得到了以下代码:C# 我必须做什么才能使我的接口至少与引用它的方法一样可访问?,c#,interface,dependency-injection,asp.net-web-api,inversion-of-control,C#,Interface,Dependency Injection,Asp.net Web Api,Inversion Of Control,我正在尝试将我的控制器转换为使用DI 基于,我现在得到了以下代码: namespace HandheldServer.Controllers { public class DuckbillsController : ApiController { static IDuckbillRepository _platypiRepository; public DuckbillsController(IDuckbillRepository platypi
namespace HandheldServer.Controllers
{
public class DuckbillsController : ApiController
{
static IDuckbillRepository _platypiRepository;
public DuckbillsController(IDuckbillRepository platypiRepository)
{
if (platypiRepository == null)
{
throw new ArgumentNullException("platypiRepository is null");
}
_platypiRepository = platypiRepository;
}
public int GetCountOfDuckbillRecords()
{
return _platypiRepository.Get();
}
public IEnumerable<Duckbill> GetBatchOfDuckbillsByStartingID(int ID, int CountToFetch)
{
return _platypiRepository.Get(ID, CountToFetch);
}
public void PostDuckbill(int accountid, string name)
{
_platypiRepository.PostDuckbill(accountid, name);
}
public HttpResponseMessage Post(Duckbill Duckbill)
{
Duckbill = _platypiRepository.Add(Duckbill);
var response = Request.CreateResponse<Duckbill>(HttpStatusCode.Created, Duckbill);
string uri = Url.Route(null, new { id = Duckbill.Id });
response.Headers.Location = new Uri(Request.RequestUri, uri);
return response;
}
}
}
名称空间HandheldServer.Controller
{
公共类DuckBills控制器:ApiController
{
静态IDuckbillRepository\u平台存储库;
公共DuckbillsController(IDuckbillRepository平台存储库)
{
if(platypiRepository==null)
{
抛出新ArgumentNullException(“PlatypRepository为null”);
}
_platypiRepository=platypiRepository;
}
public int GetCountOfDuckbillRecords()
{
返回_platypiRepository.Get();
}
public IEnumerable GetBatchOfDukbillsBystartingId(int-ID,int-CountToFetch)
{
return\u platypiRepository.Get(ID,CountToFetch);
}
public void PostDuckbill(int accountid,字符串名称)
{
_platypiRepository.PostDuckbill(accountid,name);
}
公共HttpResponseMessage Post(鸭嘴鸭嘴)
{
鸭嘴兽=_platypiRepository.Add(鸭嘴兽);
var response=Request.CreateResponse(HttpStatusCode.Created,Duckbill);
字符串uri=Url.Route(null,新的{id=Duckbill.id});
response.Headers.Location=新Uri(Request.RequestUri,Uri);
返回响应;
}
}
}
…但它不编译;我得到,“不一致的可访问性:参数类型‘HandheldServer.Models.IDuckbillRepository’比方法‘HandheldServer.Controllers.DuckbillsController.DuckbillsController(HandheldServer.Models.IDuckbillRepository)’更难访问”
err msg中提到的接口参数类型为:
using System.Collections.Generic;
namespace HandheldServer.Models
{
interface IDuckbillRepository
{
int Get();
IEnumerable<Duckbill> Get(int ID, int CountToFetch);
Duckbill Add(Duckbill item);
void Post(Duckbill dept);
void PostDuckbill(int accountid, string name);
void Put(Duckbill dept);
void Delete(int Id);
}
}
使用System.Collections.Generic;
名称空间HandheldServer.Models
{
接口IDuckbillRepository
{
int Get();
IEnumerable Get(int ID,int CountToFetch);
鸭嘴兽添加(鸭嘴兽项目);
空岗(鸭嘴部);
作废邮政汇票(int accountid,字符串名称);
无效认沽权(鸭嘴部);
无效删除(int-Id);
}
}
我需要做些什么来避免这个错误消息?您需要明确地将
界面
标记为公共
:
public interface IDuckbillRepository
{
// ....
}
另外,不要在控制器中将其标记为静态。您需要将
接口明确标记为公共:
public interface IDuckbillRepository
{
// ....
}
另外,不要在控制器中标记它是静态的。文章中它是静态的,不应该是静态的。假设存储库设置正确。。应在每次请求时重新创建。。就像你的控制器一样。静态属性将在appdomain的生命周期内有效。。如果存储库是在“每个请求”的生命周期中设置的(应该是这样的)。。然后,您向控制器抛出的请求越多,就可能会出现随机错误。在本文中,它是静态的,不应该是静态的。假设存储库设置正确。。应在每次请求时重新创建。。就像你的控制器一样。静态属性将在appdomain的生命周期内有效。。如果存储库是在“每个请求”的生命周期中设置的(应该是这样的)。。然后,向控制器抛出的请求越多,就可能出现随机错误。