我必须做什么才能让我的界面至少像引用它的方法一样可访问



我正在尝试将我的控制器转换为使用 DI。

根据这里的文章,我现在得到了以下代码:

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.Models.IDuckbillRepository'比方法'HandheldServer.Controllers.DuckbillsController.DuckbillsController (HandheldServer.Models.IDuckbillRepository)'更难访问"

错误消息中提到的接口参数类型为:

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);
    }
}

我需要做什么才能解决这个错误的消息?

您需要

interface显式标记为public

public interface IDuckbillRepository
{
    // ....
}

此外,请勿将其标记为static控制器中。

最新更新