Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
No public constructor is available for type System.Security.Principal.IIdentity
UnityConfig File:
C#
var container = new UnityContainer();    
container.RegisterType<IUnitOfWork, UnitOfWork>();
container.RegisterType<IUserBiz, UserBiz>();
container.RegisterType<User>(new InjectionConstructor());
container.RegisterType<ClaimsIdentity>(new InjectionConstructor());
container.RegisterType<UserDTO >();
container.RegisterType<IBiz<UserDTO>>();
container.RegisterType<UserAccess>(new InjectionConstructor());
container.RegisterType<UserGroup>(new InjectionConstructor());
container.RegisterType<ILookupBiz, LookupBiz>();
container.RegisterType<IRepository<User>, EFRepository<User>>();


container.RegisterType<DbContext, ToptecDB>();



container.RegisterType(typeof(ISecureDataFormat<>), typeof(SecureDataFormat<>));
container.RegisterType<ITextEncoder, Base64UrlTextEncoder>();
container.RegisterType<IDataSerializer<AuthenticationTicket>, TicketSerializer>();
container.RegisterType<IDataProtector>(new ContainerControlledLifetimeManager(),
new InjectionFactory(c => new DpapiDataProtectionProvider().Create("Toptec"))); 
container.RegisterType<AccountController>(new InjectionConstructor());






I wanted to initialize the interfaces :

C#
private IUserBiz _userBiz;
 private IUnitOfWork _unitOfWork;

 public ValuesController(){
 _userBiz = userBiz;
_unitOfWork = unitOfWork;

}


What I have tried:

C#
namespace Toptec.BLL.Modules.Membership
{
    public class UserBiz : BaseBiz<User, UserDTO>, IUserBiz
    {
        private readonly IRepository<User> _userRepository;
        private readonly IRepository<UserAccess> _userRoleRepository;
        private readonly IUserGroupBiz _userGroupBiz;
        private IEmployeeBiz _employeeBiz;
        private IVirtualOrganizationStructureBiz _virtualOrganizationStructureBiz;
        public static string connstr =
         AppSettings.TopTecConnectionString;
        public UserBiz(IUnitOfWork unitOfWork, IUserGroupBiz userGroupBiz, IEmployeeBiz employeeBiz, IVirtualOrganizationStructureBiz virtualOrganizationStructureBiz)
            : base(unitOfWork/*, userRepository*/)
        {
            _userRepository = unitOfWork.Repository<User>();
            _userRoleRepository = unitOfWork.Repository<UserAccess>();
            _userGroupBiz = userGroupBiz;
            _employeeBiz = employeeBiz;
            _virtualOrganizationStructureBiz = virtualOrganizationStructureBiz;
        }
}





C#
public class EFRepository<T> : IRepository<T> where T : BaseEntity
   {
       private readonly DbContext _context;
       protected DbSet<T> Entities { get; private set; }

       private readonly CurrentUser _currentUser;

       private bool _checkTenant;

       //public int CurrentUserID
       //{
       //    get
       //    {
       //        HttpContext.Current.User.Identity.IsAuthenticated
       //    }
       //}

       /// <summary>
       /// Ctor
       /// </summary>
       /// <param name="context">Object context</param>
       public EFRepository(DbContext context, CurrentUser currentUser)
       {
           this._context = context;
           this._currentUser = currentUser;
           this.Entities = _context.Set<T>();

           if (currentUser.IsAuthenticated)
           {
               _checkTenant = !typeof(T).IsSubclassOf(typeof(StrongEntityNoTenant));
           }


       }
Posted
Updated 4-Aug-23 1:06am
v2

1 solution

Read the error message: <prelang="error">No public constructor is available for type System.Security.Principal.IIdentity
It means what it says: you cannot construct an instance of that class (or Interface, which is what I suspect it is from the name).

We can't do anything about that: we have no idea which class implements the interface, or where in your code the error occurs.
 
Share this answer
 
Comments
maysam_p82 4-Aug-23 10:23am    
Actually, I provided all of the dependencies, dbcontext,... but there are lots of inheritances inside classes. But I still don't know why I face such an error. I have searched it for over 2 days. But I didn't find anything similar to my error on the Internet. I guess it looks for a dependency for Identity! Is there any other way to initialize my variables without DI?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900