Reputation: 835
I am trying to register following type as follows:
IDbManager db= new DbManager(ConnectionDbType.SqlServer);
InjectionConstructor injectionConstructor = new InjectionConstructor(db);
.RegisterType<IDbManager, DbManager>(injectionConstructor)
normally it's like:
IDbManager dbmanager = new DbManager(ConnectionDbType.SqlServer)
Full error:
System.InvalidOperationException: 'Injected constructor .ctor(DataAccessHandler.DbManager) could not be matched with any public constructors on type IDbManager.
Error in: RegisterType(Invoke.Constructor(DataAccessHandler.DbManager)) Inner exception: ArgumentException: Injected constructor .ctor(DataAccessHandler.DbManager) could not be matched with any public constructors on type IDbManager.
What i am doing wrong?
Upvotes: 3
Views: 2120
Reputation: 195
In case anyone else encounters this problem, I recently ran into it myself where I was trying to register a type with a 2-parameter constructor but Unity kept claiming it couldn't find the constructor. The constructor in question takes a Type
and a String
and initially I was trying to inject it in the registration like so:
...
new InjectionConstructor(this.GetType(), "some string")
As it turned out, for whatever reason Unity was having trouble resolving the types of the constructor parameters based on the values I supplied to be injected into the constructor. For the first argument anyway, I had to be explicit about what type of parameter the constructor should expect, meaning my registration had to be changed thus:
new InjectionConstructor(new InjectionParameter<Type>(this.GetType()), "some string")
The generic argument to InjectionParameter
must match the declared constructor parameter type so Unity can correctly resolve the constructor, but the argument you pass in for that parameter only has to be compatible with that type. InjectionParameter
could also be any other Unity parameter type like ResolvedParameter
, etc.
Upvotes: 1