0

I have some problem with my application. Recently I've downloaded Ninject packages and I cannot bind my entities.

My controller:

public class ServerController : ApiController
{
    private readonly IServerService _service;

    public ServerController(IServerService service)
    {
        this._service = service;
    }

RegisterServices:

private static void RegisterServices(IKernel kernel)
    {
        kernel.Bind<IServerService>().To<ServerService>().InSingletonScope();


        kernel.Bind<IRepository<Server>>()
            .To(typeof(ServerRepository))
            .InRequestScope();
    }  

ServerService:

private readonly IRepository<Server> _repository;

    public ServerService(IRepository<Server> repository)
    {
        this._repository = repository;
    }

    public async Task<IEnumerable<Server>> GetAllAsync()
    {
        return await this._repository.GetAllAsync();
    }

If someone needs more info I'll send it to comments.

Kiryl
  • 51
  • 1
  • 5
  • I got exception: Unable to cast object of type 'HealthCheck.DAL.Framework.Repositories.ServerRepository' to type 'HealthCheck.DAL.Framework.Interfaces.IRepository`1[HealthCheck.DAL.Framework.Models.EnvironmentDbModels.Server]'. – Kiryl Jun 06 '19 at 14:17
  • And another one: An error occurred when trying to create a controller of type 'ServerController'. Make sure that the controller has a parameterless public constructor. – Kiryl Jun 06 '19 at 14:17
  • The first error message seems to say that `ServerRepository` is not implementing `IRepository`. – Andreas Appelros Jun 07 '19 at 08:53

1 Answers1

0

The problem has been solved. I just added new interfaces to DAL model and implemented IRepository. After that my Ninject register looks like:

        kernel.Bind<IServerService>().To<ServerService>().InSingletonScope();
        kernel.Bind<IServerRepository>().To<ServerRepository>().InSingletonScope();
Kiryl
  • 51
  • 1
  • 5