C# – Accessibility: Parameter type is less accessible than method

asp.netc

this question has been answered a few times but I still can not get my code to work. I have a very simple class looking like this:

namespace SportsStore.Controllers {
    public class ProductController : Controller {
        private IProductRepository repository;
        public ProductController(IProductRepository repo) {  
            repository = repo;
        }
        public ViewResult List() => View(repository.Products);
    }
}

With IproductRepository.cs

namespace SportsStore.Models
{
    interface IProductRepository {
        IQueryable<Product> Products { get; }
    }
}

The error I get is of course is this one:

Severity    Code    Description Project File    Line    Suppression State
Error   CS0051  Inconsistent accessibility: parameter type 
'IProductRepository' is less accessible than method 
'ProductController.ProductController(IProductRepository)'   SportsStore 

I can see that the problem is that I shouldn't set the IproductRepository variable to private and then have the ProductController public.

I read this answer:
Inconsistent Accessibility: Parameter type is less accessible than method
and it basically tells me that the class needs to be public (it already is) and the parameter needs to be 'as accessible as'. Meaning public, right?

But if I set the parameter to public aswell, like this:

public IProductRepository repository; 

Then I still get this error:

Severity    Code    Description Project File    Line    Suppression State
Error   CS0052  Inconsistent accessibility: field type 'IProductRepository' 
is less accessible than field 'ProductController.repository'    SportsStore 

How do I fix this?

Note: This is an example from the book Asp Net Core by Adam Freeman and the github for the project is available at this link, in which the code is witten as I have above, so they have made no corrections to it. It seems like it should be working. Am running Visual Studio 2017 if that makes any difference.

https://github.com/Apress/pro-asp.net-core-mvc-2/blob/master/08%20-%20SportsStore/SportsStore/SportsStore/Controllers/ProductController.cs

Best Answer

Looks like the interface should be public

namespace SportsStore.Models
{
    public interface IProductRepository
    {
        IQueryable<Product> Products { get; }
    }
}

Same applies for classes that you pass to the constructor.

Related Topic