我遇到了Microsoft Unity的问题,与此帖here非常相似,但没有一个答案对我有用。 我有解决方案,其中包括几个项目。在第一个重要的项目Bootstrap中,我将UnityContainer定义为如下:
public static class Bootstrap
{
private static readonly IUnityContainer _unityContainer;
static Bootstrap()
{
_unityContainer = new UnityContainer();
_unityContainer.RegisterType(typeof (IRepository<>), typeof (Ef6Repository<>));
_unityContainer.RegisterType<IUnitOfWork, Ef6UnitOfWork>();
}
public static IUnityContainer GetConfiguredContainer()
{
return _unityContainer;
}
}
问题在线 _unityContainer.RegisterType(typeof(IRepository&lt;&gt;),typeof(Ef6Repository&lt;&gt;)); ,其中Unity表示无法解析方法&#39; RegisterType(System.Type,System.Type )&#39 ;.但是这条线是正确的,如MSDN中所述。它是几天前可编辑和工作的。我认为这个问题可以在我使用Ef6UnitOfWork或Ef6Repository的某个地方。
有人可以帮忙吗?我无法弄清楚我做错了什么。 Unity的版本是3.5
Bellow是我的第二个项目代码&#34; Repository&#34;在解决方案中。
public interface IRepository<T>
{
T GetById(Guid id);
IQueryable<T> GetAll();
IEnumerable<T> Find(Expression<Func<T, bool>> predicate);
ApplicationUserManager GetUserManager();
ApplicationRoleManager GetRoleManager();
}
public class Ef6Repository<T> : IRepository<T> where T : class
{
public DbContext context;
public DbSet<T> dbset;
private readonly ApplicationUserManager _userManager;
private readonly ApplicationRoleManager _roleManager;
public Ef6Repository(DbContext context)
{
this.context = context;
dbset = context.Set<T>();
}
public Ef6Repository(DbContext context, ApplicationUserManager userManager)
{
this.context = context;
dbset = context.Set<T>();
_userManager = userManager;
}
public Ef6Repository(DbContext context, ApplicationRoleManager roleManager)
{
this.context = context;
dbset = context.Set<T>();
_roleManager = roleManager;
}
public T GetById(Guid id)
{
return dbset.Find(id);
}
public IQueryable<T> GetAll()
{
return dbset;
}
public IEnumerable<T> Find(Expression<Func<T, bool>> predicate)
{
return dbset.Where(predicate);
}
public ApplicationUserManager GetUserManager()
{
return _userManager;
}
public ApplicationRoleManager GetRoleManager()
{
return _roleManager;
}
}
public interface IUnitOfWork : IDisposable
{
IRepository<Person> PersonRepository { get; }
IRepository<ApplicationUser> ApplicationUserRepository { get; }
IRepository<IdentityRole> ApplicationRoleRepository { get; }
void Save();
}
public partial class Ef6UnitOfWork : IUnitOfWork
{
private IRepository<Person> _personRepository;
private IRepository<ApplicationUser> _applicationUserRepository;
private IRepository<IdentityRole> _applicationRoleRepository;
private Context _context;
public Ef6UnitOfWork()
{
_context = new Context();
}
public IRepository<Person> PersonRepository
{
get
{
if (_personRepository == null)
_personRepository = new Ef6Repository<Person>(_context);
return _personRepository;
}
}
public IRepository<ApplicationUser> ApplicationUserRepository
{
get
{
if (_applicationUserRepository == null)
_applicationUserRepository = new Ef6Repository<ApplicationUser>(_context,
new ApplicationUserManager(new UserStore<ApplicationUser>(_context))
);
return _applicationUserRepository;
}
}
public IRepository<IdentityRole> ApplicationRoleRepository
{
get
{
if (_applicationRoleRepository == null)
_applicationRoleRepository = new Ef6Repository<IdentityRole>(_context,
new ApplicationRoleManager(new RoleStore<IdentityRole>(_context))
);
return _applicationRoleRepository;
}
}
public void Save()
{
_context.SaveChanges();
}
#region IDisposable
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
_context.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}