Unable to create instance using Activator.CreateInstance on a generic type

0

I have implemented a generic UnitOfWork and a generic Repository pattern in my application. I had it working so far, but some random change is not breaking a part of the code.

Below are the snippets from my code:

Generic UnitOfWork<TContext> class:

public sealed class UnitOfWork<TContext> : IDisposable, IUnitOfWork<TContext> where TContext : IDbContext//, new()
{
    private static readonly ILog Log = LogManager.GetLogger(typeof(UnitOfWork<TContext>));

    private readonly IDbContext _dbContext;
    private Dictionary<string, IRepository> _repositories;
    private IDbTransaction Transaction { get; set; }

    public UnitOfWork(IDbContext context)
    {
        _dbContext = context;
    }

    public IGenericRepository<TEntity> GetRepository<TEntity>() where TEntity : class
    {
        if (_repositories == null)
            _repositories = new Dictionary<string, IRepository>();

        var repositoryName = typeof(TEntity).Name;

        if (_repositories.ContainsKey(repositoryName))
            return (IGenericRepository<TEntity>)_repositories[repositoryName];

        var repositoryType = typeof(GenericRepository<,>);
        var constructedType = repositoryType.MakeGenericType(typeof(TContext), typeof(TEntity));
        var repositoryInstance = Activator.CreateInstance(constructedType, _dbContext);
        _repositories.Add(repositoryName, repositoryInstance as IRepository);

        return (IGenericRepository<TEntity>)_repositories[repositoryName];
    }
}

GenericRepository<TContext, TEntity> class:

public class GenericRepository<TContext, TEntity> : IGenericRepository<TEntity>
    where TEntity : class
    where TContext : IDbContext
{
    private readonly TContext _dbContext;

    public GenericRepository(TContext context)
    {
        _dbContext = context;
    }
}

I am invoking the GetRepository method as below:

_genevaPositionRepository = _reconciliationUoW.GetRepository<GenevaPosition>();

When I do so, I get an exception at the line where I try to create instance of the GenericRepository using Activator.CreateInstance().

Exception:

System.MissingMethodException occurred
  HResult=0x80131513
  Message=Constructor on type 'AQR.Framework.Core.UnitOfWork.GenericRepository`2[[BoxedPositionReconciliation.Services.DataContext.ReconciliationDbContext, BoxedPositionReconciliation.Services, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[BoxedPositionReconciliation.Models.GenevaPosition, BoxedPositionReconciliation.Models, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' not found.
  Source=mscorlib
  StackTrace:
   at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   at System.Activator.CreateInstance(Type type, Object[] args)
   at AQR.Framework.Core.UnitOfWork.UnitOfWork`1.GetRepository[TEntity]() in C:\AQR_GIT\BoxedPositionReconciliation\AQR.Framework.Core\UnitOfWork\UnitOfWork.cs:line 41
   at BoxedPositionReconciliation.Services.Service.ReconPositionService..ctor(IApplicationSettings applicationSettings, IWorkflowService workflowService, ISecurityMasterService secMasterService, ITaskStatusHistoryService taskStatusHistoryService, ICommentsService commentsService, IUnitOfWork`1 reconciliationUoW, IUnitOfWork`1 referenceDataUoW, IUnitOfWork`1 genevaDataUoW, IUnitOfWork`1 openStaarsUoW) in C:\AQR_GIT\BoxedPositionReconciliation\BoxedPositionReconciliation.Services\Service\ReconPositionService.cs:line 70
   at Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate()

I am finding it hard to resolve this problem, but I am sure it must be an easy fix, because the code worked earlier.

c#
generics
repository-pattern
unit-of-work
asked on Stack Overflow Oct 17, 2018 by Lucifer

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0