2017-04-06 4 views
2

내가 asp.net의 신원과 Ninject에 사용하기 위해 노력하고있어 현재 내가 바인딩에 대해 다음을 수행 하였다Ninject에

public interface IUser : IUser<string> 

public interface IUserStore<TUser> : IUserStore<TUser, string>, IDisposable where TUser : class, IUser<string> 

public class IdentityUser : IdentityUser<string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>, IUser, IUser<string>, IGenerateKeys 

public class UserStore<TUser> : UserStore<TUser, IdentityRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>, IUserStore<TUser>, IUserStore<TUser, string> where TUser : IdentityUser, new() 

public class Context { 
    public Context(ILogger Logger, Dictionary<string, string> LocalConfig, IUserStore<IUser> UserManager 
     ){...} 
} 
:

여기
kernel.Bind(typeof(IUserStore<>)).To(typeof(UserStore<>)); 

당신은 클래스 정의가를

kernel.Get<Context>() 할 때 IUserStore를 UserStore로 캐스팅 할 수 없다는 오류가 발생합니다. 난 정말이 작동하지만, 사실 다음 IUserStore<IdentityUser> dummy = new Store<IdentityUser>()을 시도하지 않는 이유를 이해하지 않습니다

Unable to cast object of type 'UserStore`1[IdentityUser]' to type 'Microsoft.AspNet.Identity.IUserStore`1[Microsoft.AspNet.Identity.IUser]'. 

에는 컴파일 오류를 제공하지 않지만이 IUserStore<IUser> dummy = new Store<IdentityUser>()는 않지만, IdentityUser은 IUSER를 구현하기 때문에 내가 왜 understande 수 없습니다.

누구나 비슷한 문제가 있습니까? 당신은 당신이 UserStore에서 TUser이 IdentityUser하지 IUSER의 유형이어야합니다 볼 수 있습니다Microsoft.AspNet.Identity.EntityFramework를 보면 당신은 컴파일 오류가 발생했습니다 왜

+1

: 당신이 정체성해서 Ninject를 사용하려면

, 그것은 당신이 여기 단계 솔루션에 의해 단계를 찾을 수 있습니다, 라이트 비트 까다로운 일이 될 것입니다. 일반 객체의 유형 매개 변수를 변경하면 실제 유형이 변경됩니다. 그래서'IUserStore '는'IUserStore '와 같은 것이 아니며 다른 하나에 할당 할 수 없습니다. 'List '에'List '을 할당하려고한다고 상상해보십시오. 제네릭 형식이 서로의 구현인지 여부는 중요하지 않습니다. 이제 IdentityUser 객체를 IUserStore 객체에 추가 할 수 있지만 다른 유형의 실제 UserStore를 할당하는 것과 같은 것은 아닙니다. –

+1

좀 더 명확히해야합니다. 위에서 말한 것은 사실이지만 .NET 4 기반 언어 (C# 및 VB)에서이 작업을 수행하는 방법이 있지만 인터페이스를 지정해야합니다. 이를 공역 (또는 공역)이라고합니다. IUser 및 IUserStore 인터페이스는이를 구현하지 않으므로 여전히 그 지점이 있습니다. 분산에 대한 자세한 내용은 다음을 참조하십시오. https://msdn.microsoft.com/en-us/library/dd799517(v=vs.110).aspx –

답변

1

, 즉이다.

public namespace Microsoft.AspNet.Identity.EntityFramework 
{ 
// 
// Summary: 
//  EntityFramework based user store implementation that supports IUserStore, IUserLoginStore, 
//  IUserClaimStore and IUserRoleStore 
// 
// Type parameters: 
// TUser: 
public class UserStore<TUser> : UserStore<TUser, IdentityRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>, IUserStore<TUser>, IUserStore<TUser, string>, IDisposable where TUser : IdentityUser 
{ 
    // 
    // Summary: 
    //  Default constuctor which uses a new instance of a default EntityyDbContext 
    public UserStore(); 
    // 
    // Summary: 
    //  Constructor 
    // 
    // Parameters: 
    // context: 
    public UserStore(DbContext context); 
    } 
} 

IUser는 IdentityUser의 최소 인터페이스 일 뿐이므로 항상 IdentityUser를 사용해야합니다. 당신은 유형 자체 일반 매개 변수를 혼동 How to inject User Manager to Account Controller with default Identity Model in Identity 2 using Ninject