C
C#3y ago
PontiacGTX

Cannot resolve constructor for String type. Using Unity container

I am sending a string to StringCon Class but it doesnt resolve when injecting into HomeController... here is the code
var con = ConfigurationManager.AppSettings["MyConnectionString"];
var paramStringCon = new ParameterOverrides();
container.RegisterType<StringCon>();
container.Resolve<StringCon>(new ResolverOverride[]
{
new ParameterOverride("str", con)
});

var logProd = container.Resolve(typeof(Microsoft.Extensions.Logging.ILogger<ProductoRepository>));

container.RegisterType<ProductoRepository>(nameof(ProductoRepository),
new InjectionConstructor(container.Resolve<StringCon>(new ResolverOverride[]
{
new ParameterOverride("str", con)
}), logProd));
new UnityDependencyResolver(container);
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
var con = ConfigurationManager.AppSettings["MyConnectionString"];
var paramStringCon = new ParameterOverrides();
container.RegisterType<StringCon>();
container.Resolve<StringCon>(new ResolverOverride[]
{
new ParameterOverride("str", con)
});

var logProd = container.Resolve(typeof(Microsoft.Extensions.Logging.ILogger<ProductoRepository>));

container.RegisterType<ProductoRepository>(nameof(ProductoRepository),
new InjectionConstructor(container.Resolve<StringCon>(new ResolverOverride[]
{
new ParameterOverride("str", con)
}), logProd));
new UnityDependencyResolver(container);
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
12 Replies
Yawnder
Yawnder3y ago
Are you just like... writing anything you can think of? Your registrations should happen very early in the process normally, and you should seldom use overrides. Use named registrations instead.
undisputed world champions
i'd wrap the string in a options-class, register and configure it in the ServiceCollection and let DI inject it into constructors as needed https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-6.0
Options pattern in ASP.NET Core
Discover how to use the options pattern to represent groups of related settings in ASP.NET Core apps.
PontiacGTX
PontiacGTXOP3y ago
it is what I have done if you see container.RegisterType<StringCon>();
undisputed world champions
sry somehow completely missed it's about unity ^^ but i'm with yawnder ... why are you trying to override the parameter when resolving, instead of registering it with the right value? you seem to use the same value in all places, right?
phaseshift
phaseshift3y ago
that isn't 'IOptions' type/pattern that was being referenced by icebear
PontiacGTX
PontiacGTXOP3y ago
technically yes but no I think that is more of an automatic thing in ASP.NET Core I fixed it had to instance from a factory container.RegisterFactory(typeof(StringCon),instance=>new StringCon(con), FactoryLifetime.Scoped);
phaseshift
phaseshift3y ago
your string comes from config
PontiacGTX
PontiacGTXOP3y ago
yeah i read it from the Config
phaseshift
phaseshift3y ago
so you can just use IOptions instead of hard wiring all these overridees
PontiacGTX
PontiacGTXOP3y ago
ASP.NET has IOptions? it isnt asp.net core
phaseshift
phaseshift3y ago
i dont think ioptions is new
PontiacGTX
PontiacGTXOP3y ago
so yeah I have to manually bind the properties
Want results from more Discord servers?
Add your server