This project is read-only.

On extending / inherits from abstract classes and other interfaces : TEntity is not a concrete type

Apr 5, 2014 at 8:12 PM
Edited Apr 5, 2014 at 8:20 PM
Hi, The injector class called by: [assembly: PreApplicationStartMethod(typeof(SimpleInjectorWebCommon), "Start")]
is throwing the following exception at :
            container.RegisterOpenGeneric(
                typeof(IService<>),
                typeof(Service<>),
                new WebRequestLifestyle());
_An exception of type 'System.ArgumentException' occurred in SimpleInjector.dll but was not handled in user code
Additional information: The given type Service<TEntity> is not a concrete type. Please use one of the other overloads to register this type.
_
I have the following statements:
public interface IService<TEntity> where TEntity : class {..}
public abstract class Service<TEntity> : IService<TEntity> where TEntity : class {..}

public interface IRepository<TEntity> where TEntity : class {..}
public class Repository<TEntity> : IRepository<TEntity> where TEntity : class {..}

public class OfficeService : Service<tOffice>, IOfficeService {..}
public interface IOfficeService : IService<tOffice> {..}
As you can see the exception is thrown before App_Start method of Global.asax.
Any clue I'd really appreciate.
Thanks. -- JR
Apr 5, 2014 at 8:48 PM
Edited Apr 5, 2014 at 8:49 PM
Firstly you can't register an abstract class as an open generic implementation.

Secondly, by the looks of things you have one (or more) closed implementations of a generic type. You can register all found closed implementations of a generic type with the extension method RegisterManyForOpenGeneric:

e.g. (see all of the overloads of this extension method for full details)
container.RegisterManyForOpenGeneric(typeof(IService<>), typeof(IService<>).Assembly);
Apr 5, 2014 at 9:29 PM
Edited Apr 5, 2014 at 9:39 PM
Thanks qujck, I changed my abstract class by class type, what I have now is:
            container.RegisterManyForOpenGeneric(
                typeof(IRepository<>),
                typeof(IRepository<>).Assembly);

            container.RegisterManyForOpenGeneric(
                typeof(IService<>),
                typeof(IService<>).Assembly);

            container.Register<IOfficeService, OfficeService>();
But I have the following exception.. I guess everything is well setup.
The constructor of the type OfficeService contains the parameter of type IRepository<tOffice> with name 'repository' that is not registered. Please ensure IRepository<tOffice> is registered in the container, or change the constructor of OfficeService.
Any clue ?
Apr 5, 2014 at 10:05 PM
From the code sample above I would guess you need this:
container.RegisterOpenGeneric(
    typeof(IRepository<>),
    typeof(Repository<>));

container.RegisterManyForOpenGeneric(
    typeof(IService<>),
    typeof(IService<>).Assembly);

container.Register<IOfficeService, OfficeService>();
Apr 5, 2014 at 11:00 PM
Thanks qujck, I did what you suggested, but now I'm getting this:
The constructor of the type Repository<tOffice> contains the parameter of type IDbContext with name 'context' that is not registered. Please ensure IDbContext is registered in the container, or change the constructor of Repository<tOffice>.

DbContext was injected as follow:
container.RegisterPerWebRequest<DbContext, AmsDbContext>();

I modified to:
container.Register<DbContext, AmsDbContext>();  //but didn't work neither.
Apr 5, 2014 at 11:11 PM
You don't seem to have a specific registration for IDbContext. If a constructor expects an IDbContext then that needs to be registered - the container won't try to guess whether, for example, AmsDbContext implements IDbContext - you need to tell the container.

Purely a guess but does this get you past the error (I'm assuming AmsDbContext implements IDbContext)?
container.RegisterPerWebRequest<IDbContext, AmsDbContext>();
Apr 6, 2014 at 12:58 AM
As you're coding on a Saturday night I'm guessing you love coding so here's 3 (hopefully useful) tips ... in order
  1. User Verify()
  2. Buy, read and digest The Art Of Unit Testing
  3. Read these posts on Commands and Querys
Enjoy :-)
Marked as answer by dot_NET_Junkie on 4/15/2014 at 1:50 AM