hope ur doing well, I'm trying to minimize the repetitive code, so I created methods for each controller, all works fine, now I want to create a new controller for the method and register it in the DI container but unfortunately the Di container doesn't pick it up :This exception was originally thrown at this call stack:
Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(System.IServiceProvider, System.Type)
Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(object, Microsoft.AspNetCore.Builder.IApplicationBuilder)
in services.AddTransient();
this is my method as a controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
using QlikviewServer.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Threading.Tasks;
using System.DirectoryServices.AccountManagement;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace QlikviewServer.Services
{
public class GetAZCredidentials //: IUserManager/IHttpContextAccessor<GetAZCredidentials>/<GetAZCredidentials>//GetAZCredidentials//IActionContextAccessor
{
private readonly GetAZCredidentials _context;
DbSet<ApplicationUser> ApplicationUsers;
public HttpContext HttpContext { get => ((IHttpContextAccessor)_context).HttpContext; set => ((IHttpContextAccessor)_context).HttpContext = value; }
// public ActionContext ActionContext { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
private readonly IServiceProvider services;
public GetAZCredidentials(IServiceProvider services)
{
this.services = services;
}
public int GetAZCredidentials1()
{
// var person = GetPerson();
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
UserPrincipal user1 = UserPrincipal.Current;
string displayName2 = user1.DisplayName;
string displayName = user1.SamAccountName;
string DistinguishedName = user1.DistinguishedName;
string[] values = DistinguishedName.Split(',');
string domain = values[6];
string[] values2 = domain.Split('=');
string domain2 = values2[1].Trim();
string UserPrincipalName2 = user1.UserPrincipalName;
var comp = values[4];
var company_id = comp.Split('=');
var company_id2 = company_id[1].Trim();
List<ApplicationUser> groupsad = (from m in _context.ApplicationUsers
select new ApplicationUser { ApplicationUserId = m.ApplicationUserId, UserName = m.UserName, NameAdmin = m.NameAdmin, DisplayName = m.DisplayName, UserPrincipalName = m.UserPrincipalName }).ToList();
var groups2 = (from m in _context.ApplicationUsers
select m).ToList();
for (int i = 0; i < groupsad.Count; i++)
{
if (string.IsNullOrEmpty(groupsad[i].NameAdmin.Trim()))
{
groupsad[i].NameAdmin = groupsad[i].NameAdmin.Trim();
}
if (string.IsNullOrEmpty(groupsad[i].UserName.Trim()))
{
groupsad[i].UserName = groupsad[i].UserName.Trim();
}
_context.ApplicationUsers.Where(it => it.NameAdmin == groupsad[i].NameAdmin).ToList()
.ForEach(
it =>
{
var it2 = it.NameAdmin.TrimEnd();
if (it2 == displayName)
{
it.DisplayName = displayName2;
}
}
);
if (string.IsNullOrEmpty(groupsad[i].UserPrincipalName.Trim()))
{
_context.ApplicationUsers.Where(it => it.NameAdmin == groupsad[i].NameAdmin).ToList()
.ForEach(
it =>
{
var it2 = it.NameAdmin.TrimEnd();
if (it2 == displayName)
{
it.UserPrincipalName = UserPrincipalName2;
}
}
);
}
else
{
groupsad[i].UserPrincipalName = groupsad[i].UserPrincipalName.Trim();
}
var usersadmitted = (from m in _context.ApplicationUsers
select new ApplicationUser { NameAdmin = m.NameAdmin, UserName = m.UserName }).ToList();
// ViewBag.ad = (from m in _context.ApplicationUsers
// select new ApplicationUser { NameAdmin = m.NameAdmin }).ToList();
}
foreach (ApplicationUser f in groupsad)
{
if (f.NameAdmin.Trim() == displayName)
{
if (f.UserName.Trim() == "reader" || f.UserName.Trim() == "admin" && domain2 == "Vionet")
{
// if (f.UserName.Trim() == "reader") { ViewBag.reader_admin = "reader"; }
// if (f.UserName.Trim() == "admin") { ViewBag.reader_admin = "admin"; }
return 1;
}
}
return 2;
}
return 3;
}
}
public interface IUserManager
{
}
public interface IHttpContextAccessor<T>
{
}
public interface IActionContextAccessor<T>
{
}
}
and finally I call it if it would work as
var result1 = serviceProvider.GetRequiredService<GetAZCredidentials>();
if (result1 != 1)
{
thus depending on the return type 1,2,or 3 take appropriate action, any input will be highly appreciated as this my first refactoring, thank you in advance