Reputation:
I am creating ASP.NET CORE project using DI and Repository Pattern. When I run project I get this type of error
InvalidOperationException: Unable to resolve service for type 'AD.BLL.Servisi.IKorisnikServis' while attempting to activate 'AD.Web.Controllers.KorisnikController'.
Here is my interface class
using System;
using System.Collections.Generic;
using System.Text;
namespace AD.BLL.Interfejsi
{
public interface IKorisnik
{
public string VratiKorisnike();
}
}
And here is my Service class which call this interface
using AD.BLL.Interfejsi;
using AD.Web.Data;
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Text;
namespace AD.BLL.Servisi
{
public class IKorisnikServis : IKorisnik
{
private ApplicationDbContext _db;
public IKorisnikServis(ApplicationDbContext db)
{
_db = db;
}
public string VratiKorisnike()
{
System.DirectoryServices.DirectoryEntry rootDSE = new System.DirectoryServices.DirectoryEntry("LDAP://RootDSE");
var defaultNamingContext = rootDSE.Properties["defaultNamingContext"].Value;
DirectorySearcher dssearch = new DirectorySearcher("LDAP://" + defaultNamingContext);
dssearch.Filter = "(sAMAccountName=ABCDEFGHI)";
SearchResult sresult = dssearch.FindOne();
System.DirectoryServices.DirectoryEntry dsresult = sresult.GetDirectoryEntry();
var Ime = dsresult.Properties["Ime"][0].ToString();
var Prezime = dsresult.Properties["Prezime"][0].ToString();
var LoginName = dsresult.Properties["LoginName"][0].ToString();
var Status = dsresult.Properties["Status"][0].ToString();
var AccountExpired = dsresult.Properties["AccountExpired"][0].ToString();
var PassNevExp = dsresult.Properties["PassNevExp"][0].ToString();
var DomenskaGrupa = dsresult.Properties["DomenskaGrupa"][0].ToString();
var Email = dsresult.Properties["Email"][0].ToString();
return Ime;
}
}
}
Here is my ApplicationDbContext
class
using AD.Models.DbModels;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace AD.Web.Data
{
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<Korisnik> Korisnici { get; set; }
}
}
And here is my Controller action
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AD.BLL.Servisi;
using Microsoft.AspNetCore.Mvc;
namespace AD.Web.Controllers
{
public class KorisnikController : Controller
{
private IKorisnikServis _korisnikServis;
public KorisnikController(IKorisnikServis korisnikServis)
{
_korisnikServis = korisnikServis;
}
public IActionResult VratiKorisnike()
{
_korisnikServis.VratiKorisnike();
return View();
}
public IActionResult Index()
{
return View();
}
}
}
And in Startup.cs I register IKorisnik
and IKorisnikServic
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddTransient<IKorisnik, IKorisnikServis>();
}
I checked everything but I cannot see where I made mistake. Any help ?
Upvotes: 1
Views: 524
Reputation: 39946
In your Controller
you need to create the filed of type IKorisnik
which is your interface
instead of IKorisnikServis
, So your constructor should be like this:
private IKorisnik _korisnikServis;
public KorisnikController(IKorisnik korisnikServis)
{
_korisnikServis = korisnikServis;
}
However, I would strongly recommend to consider another name for your IKorisnikServis
class (KorisnikServis
for example) as the prefix I
is mostly using to indicate an interface, in this case it is also misleading and I believe that was the reason you have used it in your controller by mistake.
Upvotes: 1