Search code examples
c#asp.net-coreasp.net-core-mvcblazor-webassembly

Getting "Inconsistent accessibility parameter type" error in blazor webassembly


I am working on a project for Blazor web assembly and my API controller is giving the following error message:

Error CS0051 Inconsistent accessibility: parameter type 'IAuthRepository' is less accessible than method 'AuthController.AuthController(IAuthRepository)' BattlesBlazor.Server

This is my service response class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BattlesBlazor.Shared
{
    public class ServiceResponse<T>
    {
        public T Data { get; set; }
        public bool Success { get; set; }
        public string Message { get; set; }
    }
}

This is my IAuthRepository:

using BattlesBlazor.Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BattlesBlazor.Server.Data
{
    interface IAuthRepository
    {
        Task<ServiceResponse<int>> Register(User user, string password);
        Task<ServiceResponse<string>> Login(string email, string password);
        Task<bool> UserExists(string email);
    }
}

This is the AuthController:

using BattlesBlazor.Server.Data;
using BattlesBlazor.Shared;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BattlesBlazor.Server.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
  
    public class AuthController : ControllerBase
    {
        private readonly IAuthRepository _authRepo;

        public AuthController(IAuthRepository authRepo)
        {
            _authRepo = authRepo;
        }
    
        [HttpPost("register")]
        public async Task<IAuthRepository> Register(UserRegister request)
        {
            var response = await _authRepo.Register(
                new User {
                    Username = request.Username,
                    Email = request.Email,
                    Bananas = request.Bananas,
                    DateOfBirth =request.DateOfBirth,
                    IsConfirmed = request.IsConfirmed

                }, request.Password );

            if (!response.Success)
            {
                return BadRequest(response);
            }
            return Ok(response);
        }
    
    }

I have tried removing "public" from public class AuthController : ControllerBase which does take the error away, however on PostMan there is no response.


Solution

  • The return type in the code here is the issue:

    [HttpPost("register")]
    public async Task<IAuthRepository> Register(UserRegister request)
    

    AuthController is public, and so is Register, so IAuthRepository must also be public.

    From your definition, we can see that IAuthRepository is the default (internal):

    interface IAuthRepository
    {
        Task<ServiceResponse<int>> Register(User user, string password);
        Task<ServiceResponse<string>> Login(string email, string password);
        Task<bool> UserExists(string email);
    }
    

    Generally, the solution is to make these agree. That is: you should make IAuthRepository public (i.e. public interface IAuthRepository). On the other hand, it seems strange that Register returns IAuthRepository. Should it not return ServiceResponse<int>?:

    [HttpPost("register")]
    public async Task<ServiceResponse<int>> Register(UserRegister request)