0

I have a problem with Microsoft Unity, very similar as in this post here, but none of answers was useful for me. I have solution, which consists from couple of projects. In first important project Bootstrap I have UnityContainer defined as bellow:

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;
    }
}

Problem is on line _unityContainer.RegisterType(typeof (IRepository<>), typeof (Ef6Repository<>));, where Unity says that there cannot resolve method 'RegisterType(System.Type, System.Type)'. But this line is correct as described in MSDN. It was compilable and working few days ago. I think that problem can be somewhere in my usage of Ef6UnitOfWork or Ef6Repository.

Can anyone help? I cant figure out what I am doing wrong. Version of Unity is 3.5

Bellow is my code from second project "Repository" in solution.

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
}
Community
  • 1
  • 1
  • Your Unity code looks OK. It sounds like you have a compile error. The other posted code snippet does not compile because you didn't include all dependent classes. Although, instead of including more code you should trim your example to the minimum required to recreate the issue (doing this would probably lead you to your issue). Also, if you're using Unity (or another other DI container) I'm not sure why the Ef6UnitOfWork is newing up the repositories; that's exactly the type of dependencies the container is used to inject. – Randy Levy Aug 06 '15 at 01:39
  • Yes, it is a compile error. After some googling I think that problem is in Ef6Repository constructors. Compiler has problem with constructors public Ef6Repository(DbContext context) and public Ef6Repository(DbContext context, ApplicationUser/RoleManager user/roleManager). But even if I remove this constructors, the problem did not dissapeard :-( – user3899265 Aug 06 '15 at 06:52
  • @user3899265, can you post the exact compiler error message? – Thomas Levesque Aug 08 '15 at 00:47
  • @user3899265, also, please post your `using` clauses. I suspect the problem is there. IUnityContainer only has one RegisterType method, and it's not the one you usually want. All the other overloads are extension methods. – Thomas Levesque Aug 08 '15 at 00:51
  • @Thomas Levesque - Now I uncomment damn line of code, and VS still tell me that there is an error (line is underscored), but project is buildable and no compile error is presented. So I dont know. But when I move mouse on that underscored line, I saw this: Cannot resolve method RegisterType(System.Type, System.Type), candidates are: – user3899265 Aug 10 '15 at 06:42
  • @Thomas Levesque RegisterType(this Microsoft.Practices.Unity.IUnityContainer, System.Type, Microsoft.Practices.Unity.LifeTimeManager, params Microsoft.Practices.Unity.InjectionMember[]) RegisterType(this Microsoft.Practices.Unity.IUnityContainer, System.Type, params Microsoft.Practices.Unity.InjectionMember[]) RegisterType(this Microsoft.Practices.Unity.IUnityContainer, System.Type, string, params Microsoft.Practices.Unity.InjectionMember[]) RegisterType(this Microsoft.Practices.Unity.IUnityContainer, System.Type, System.Type, params Microsoft.Practices.Unity.InjectionMember[]) – user3899265 Aug 10 '15 at 06:44
  • @Thomas Levesque - all RegisterType methods are in Microsoft.Practices.Unity.IUnityContainer assembly in UnityContainerExtensions class – user3899265 Aug 10 '15 at 06:45
  • @user3899265, I don't get it. The methods are in scope, so it should work. You say "project is buildable and no compile error", so where are you seeing this error message? Just in the editor? Do you have ReSharper installed? Sometimes it gets confused and shows error that aren't there – Thomas Levesque Aug 10 '15 at 08:23
  • @Thomas Levesque - you were right. It was Resharper message. When I disable Code analysis, then everythink is OK. Thank you very much for your help. If you will write this as an answer, I will mark it as good. – user3899265 Aug 24 '15 at 07:40

1 Answers1

0

If the project builds without error, then the problem is not in your code. It looks like ReSharper got confused, as it sometimes does, and started showing non-existing errors. Just clear the R# solution cache and everything should get back to normal.

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758