5
Ben http://localhost:58472/Account/Register için sörf yapıyorsanız

Birkaç gün önce istisnaakım tipleri, IUserStore <ApplicationUser> ve DbConnection sırasıyla bir arayüz ve soyut sınıf ve inşa edilemez

System.InvalidOperationException: akım tipi, IUserStore<ApplicationUser>, bir arayüzdür ve yapılamaz. Bir tür eşlemesi eksik misiniz? Ben hiçbir şey değişti ettik, sen benim kod okursanız

[Authorize] 
public class AccountController : Controller 
{ 
    private ApplicationSignInManager _signInManager; 
    private ApplicationUserManager _userManager; 

    public AccountController() 
    { 
    } 

    public AccountController(
     ApplicationUserManager userManager, 
     ApplicationSignInManager signInManager) 
    { 
     UserManager = userManager; 
     SignInManager = signInManager; 
    } 

    public ApplicationSignInManager SignInManager 
    { 
     get 
     { 
      return _signInManager ?? 
       HttpContext.GetOwinContext().Get<ApplicationSignInManager>(); 
     } 
     private set 
     { 
      _signInManager = value; 
     } 
    } 

    public ApplicationUserManager UserManager 
    { 
     get 
     { 
      return _userManager ?? 
       HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); 
     } 
     private set 
     { 
      _userManager = value; 
     } 
    } 

    [HttpGet] 
    [AllowAnonymous] 
    public ActionResult Login(string returnUrl) 
    { 
     ViewBag.ReturnUrl = returnUrl; 
     return View(); 
    } 
} 

Yani görebilirsiniz: Burada

benim kodudur:

public class ApplicationUserManager : UserManager<ApplicationUser> 
{ 
    public ApplicationUserManager(IUserStore<ApplicationUser> store) 
     : base(store) 
    { } 

    public static ApplicationUserManager Create(
     IdentityFactoryOptions<ApplicationUserManager> options, 
     IOwinContext context) 
    { 
     var manager = new ApplicationUserManager(
      new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()) 
     ); 

     // other code 
     return manager; 
    } 
} 

ve burada denetleyici içine kodudur Yeni bir MVC projesi oluşturduğunuzda orijinal kod, ancak yukarıdaki hatayı aldım. Neyi yanlış yaptım?

ProfileController adında yeni bir denetleyici hazırladım, ikinci kod bloğumdakiyle aynı kodla ancak ApplicationSignInManager olmadan buna ihtiyacım yok.

Ayrıca, hizmetleri kaydetmek ve yönetmek için bir Unity konteyner kullanıyorum. Yukarıdaki kodda satır yorumsuz Siteyi çalıştırdığınızda herhangi bir fark bulunmamaktadır,

public static class UnityConfig 
{ 
    public static void RegisterComponents() 
    { 
     var container = new UnityContainer(); 

     container.RegisterType<IGenericRepo<Category>, GenericRepo<Category>>(); 
     container.RegisterType<ICategoryService, CategoryService>(); 
     //container.RegisterType<IUser, ApplicationUser>(); 

     DependencyResolver.SetResolver(new UnityDependencyResolver(container)); 
    } 
} 

: İşte kodudur.


Güncelleme: Ben Unity yöneticisinde bu kodu ekledikten @Jay tarafından bir açıklama ile

.System.Data.Common.DbConnection akım tipi, soyut sınıftır ve inşa edilemez:

System.InvalidOperationException:

container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(); 

Şimdi bu istisna var. Bir tür eşlemesi eksik misiniz? Burada

yığın izleme ile ilgili bazı bilgiler: tip AccountController bir denetleyicisi oluşturmak için çalışırken

  1. InvalidOperationException

    bir hata meydana geldi. Denetleyicinin parametresiz bir kamu kurucusuna sahip olduğundan emin olun.

    Evet, paramutiz bir kamu kurucum var.

  2. bağımlılık ResolutionFailedException

    Çözünürlük = "(none)" type = "AccountController", isim, başarısız oldu.

    container.RegisterType<IDbConnection, DbConnection>(); 
    

    Ama hala aynı durum var ettik:

Yani Birlik yöneticisi bu satırı ekledik.

akım tipi, System.Data.Common.DbConnection ...


Güncelleme: Bu soruya Configure Unity DI for ASP.NET Identity tarafından yardım Bu denedim @RandyLevy tarafından bir açıklama ile

:

  1. AccountController bu ekleme:

    InjectionConstructor accountInjectionConstructor = 
        new InjectionConstructor(new ApplicationDbContext()); 
    
    container.RegisterType<AccountController>(); 
    container.RegisterType<IController, Controller>(); 
    
    container.RegisterType 
        <IRoleStore<IdentityRole>, RoleStore<IdentityRole>> 
        (accountInjectionConstructor); // on this line 
    
    container.RegisterType 
        <IUserStore<ApplicationUser>, UserStore<ApplicationUser>> 
        (accountInjectionConstructor); 
    

Ama yorum ile on line bu hatayı (hiçbir çalışma zamanında istisna) var:

// two fields added 
private readonly UserManager<ApplicationUser> _userManager; 
private readonly RoleManager<IdentityRole> _roleManager; 

// constructor added 
public AccountController(
    IUserStore<ApplicationUser> userStore, 
    IRoleStore<IdentityRole> roleStore, 
    ApplicationSignInManager signInManager) 
{ 
    _userManager = new UserManager<ApplicationUser>(userStore); 
    _roleManager = new RoleManager<IdentityRole>(roleStore); 
    SignInManager = signInManager; // I need this 
} 

public ApplicationSignInManager SignInManager // I need this 
{ 
    get 
    { 
     return _signInManager ?? 
      HttpContext.GetOwinContext().Get<ApplicationSignInManager>(); 
    } 
    private set 
    { 
     _signInManager = value; 
    } 
} 

public UserManager<ApplicationUser> UserManager // replace by other property 
               // type of ApplicationUserManager 
{ 
    get 
    { 
     return _userManager ?? 
      HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); 
    } 
    //private set // comment this because _usermanager is readonly. 
    //{ 
    // _userManager = value; 
    //} 
} 

//public ApplicationUserManager UserManager // replaced by the property type of 
//           // UserManager<ApplicationUser> 
//{ 
// get 
// { 
//  return _userManager ?? 
//   HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); 
// } 
// private set 
// { 
//  _userManager = value; 
// } 
//} 
  • UnityManager içine bu ekleme:

    tip RoleStore<IdentityRole> genel türü veya meth tip parametresi TTo olarak kullanılamaz od aşağıdaki:

    UnityContainerExtensions.RegisterType<TFrom, TTo> 
        (IUnityContainer, params InjectionMember[]) 
    

    RoleStore<IdentityRole> den IRoleStore<IdentityRole> için bir üstü kapalı bir referans dönüştürme bulunmaktadır.

  • +2

    IUserStore için bir kayıt türü kaçırıyor musunuz? – Jay

    +1

    https://stackoverflow.com/questions/21927785/configure-unity-di-for-asp-net-identity yardımcı olabilir. –

    +0

    @Jay: Teşekkürler, ipucu için ama şimdi başka bir istisna yapıyorum. Benim soruma daha fazla bilgi. –

    cevap

    13

    Sonunda benim sorunum için bir çözüm buldum. Bu kodu Unity yöneticisine ekledim.

    container.RegisterType<DbContext, ApplicationDbContext>(new HierarchicalLifetimeManager()); 
    container.RegisterType<UserManager<ApplicationUser>>(new HierarchicalLifetimeManager()); 
    container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new HierarchicalLifetimeManager()); 
    container.RegisterType<AccountController>(new InjectionConstructor()); 
    
    +1

    Yalnızca, Unity'yi kurduktan sonra OP'nin aldığına dair hata almaya başladım. Unity'nin bağımlılık çözümleyicisi ile yerel Kimlik işleviyle çalışmadığı bir şey var gibi görünüyor. Bu türlerin kaydedilmesi, düzeltildi. – JARRRRG

    2

    H. Pauwelyn yanıtı için güncelleştirme. Bunun asıl sorunu, Unity'nin yapıcıyı iki parametre ile çağırmaya çalışmasıdır.

    public AccountController(
        ApplicationUserManager userManager, 
        ApplicationSignInManager signInManager) 
    { 
        UserManager = userManager; 
        SignInManager = signInManager; 
    } 
    

    aşağıdaki satırı yerine parametresiz kurucusunu çağırmak için Unity söyleyecektir ve başka bir şey gerekmez. Bu benim için çalıştı.

    container.RegisterType<AccountController>(new InjectionConstructor()); 
    
    İlgili konular