No member matching data has been found - Registering Dictionary in Unity Container

39 Views Asked by At

I'm attempting to be able to use multiple db connections in different repositories and am following this answer.

I am on .NET Framework 4.8 so things are a little different but everything has been mostly the same. The issue I'm running into is when I try to create a new dictionary in UnityConfig.cs.

The following code is throwing the error, specifically on the line where I'm attempting to register the dictionary.

var connectionDict = new Dictionary<DatabaseConnectionName, string>
{
    { DatabaseConnectionName.Connection1, ConfigurationManager.ConnectionStrings["Connection1"].ConnectionString },
    { DatabaseConnectionName.Connection2, Configurationmanager.ConnectionStrings["Connection2"].ConnectionString }
};

container.RegisterType<IDictionary<DatabaseConnectionName, string>>(new InjectionConstructor(connectionDict));
container.RegisterType<IDbConnectionFactory, DapperDbConnectionFactory>();

ERROR: System.ArgumentException: No member matching data has been found.

I'm assuming it's a silly mistake but I just can't figure it out. Any help would be greatly appreciated.

1

There are 1 best solutions below

0
Dev 404 On BEST ANSWER

Sometimes you just have to post to SO to solve your own problems lol.

I changed

container.RegisterType<IDictionary<DatabaseConnectionName, string>>(new InjectionConstructor(connectionDict));

to

container.RegisterType<IDictionary<DatabaseConnectionName, string>, Dictionary<DatabaseConnectionName, string>>(new InjectionConstructor(connectionDict));

and everything is working as expected now.