I am new to .net core and dependency injection concept. I want to inject service interface in Web API constructor, Service interface and implementation is in different project. Please find below layers of my application,
In startup.cs, I already add below line,
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
services.AddSingleton<IEntriesService, EntriesService>();
}
My controller,
public class EntriesController : Controller
{
IEntriesService entryService;
public EntriesController(IEntriesService _entryService)
{
entryService = _entryService;
}
// GET: api/values
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
}
Problem is, When I execute my API application, It is not hitting to my constructor and shows me blank page as below,
Without adding constructor, Application is working fine.
My IEntriesService,
public interface IEntriesService
{
RepeatEntries Get(int Id);
}
My EntriesService,
public class EntriesService : IEntriesService
{
IUnitOfWork _unitOfWork;
public EntriesService(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public bool Add(RepeatEntries entity)
{
_unitOfWork.EntryRepository.Add(entity);
return true;
}
}
My IUnitOfWork,
public interface IUnitOfWork : IDisposable
{
IEntriesRepository EntryRepository { get; }
void Complete();
}
My UnitOfWork,
public class UnitOfWork : IUnitOfWork
{
private readonly IEntriesRepository _entryRepository;
public UnitOfWork(IEntriesRepository entryRepository)
{
_entryRepository = entryRepository;
}
public IEntriesRepository EntryRepository
{
get
{
return _entryRepository;
}
}
void IUnitOfWork.Complete()
{
throw new NotImplementedException();
}
#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
// TODO: dispose managed state (managed objects).
}
// TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
// TODO: set large fields to null.
disposedValue = true;
}
}
// TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
// ~UnitOfWork() {
// // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
// Dispose(false);
// }
// This code added to correctly implement the disposable pattern.
void IDisposable.Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(true);
// TODO: uncomment the following line if the finalizer is overridden above.
// GC.SuppressFinalize(this);
}
#endregion
}
What else I need to add to make it work? Is it possible or I need to change my approach?


EntriesService? We have no idea if it has any dependencies on its own and if you have registered them or notStartupclass this is why it doesn't hit your controller. You can add a diagnostic middleware in the beginning of your pipeline so every time there is an unhandled exception you will see an exception page with details and stacktrace. To do this add nuget package to your projectMicrosoft.AspNetCore.Diagnosticsand then addapp.UseDeveloperExceptionPage()in the beginning ofStartup.Configure()method. Hopefully this will give you more details on what is going on.