Unity - No public constructor is available for type Mvcteste.Repository.Iempresaservice

Asked

Viewed 248 times

0

I’m using UNITY to inject dependency. net framework 4.5, configured everything else at the time of running is presents the error : No public constructor is available for type Mvcteste.Repository.Iempresaservice. Follows the code:

public static class UnityConfig
{
    public static void RegisterComponents()
    {
        var container = new UnityContainer();
        container.RegisterType<IEmpresaService, EmpresaService>();

        DependencyResolver.SetResolver(new UnityDependencyResolver(container));
    }
}

public interface IEmpresaService
{
    Task Add(Empresa empresa);
    Task Update(Empresa empresa);
    Task Delete(int? id);
    Task<Empresa[]> GetEmpresas();
    Task<Empresa> GetEmpresaById(int? id);
}

public class EmpresaService : IEmpresaService
{
    private readonly MVCTesteContext _context;

    public EmpresaService(MVCTesteContext context)
    {
        _context = context;
    }
   ...
}

Controller:

public class EmpresasController : Controller
{
    private readonly IEmpresaService _repo;

    public EmpresasController(IEmpresaService repo)
    {
        _repo = repo;
    }
....

Global.asax

protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        UnityConfig.RegisterComponents();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }

Could someone give me a hand?

  • Didn’t forget to inject MVCTesteContext?

  • @I don’t think it should be. From what I understand he is thinking that the interface is a class and that it does not have a public constructor (which is logical for being an interface)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.