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
12 Replies
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.
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.
it is what I have done if you see container.RegisterType<StringCon>();
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?
that isn't 'IOptions' type/pattern that was being referenced by icebear
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);
your string comes from config
yeah i read it from the Config
so you can just use IOptions instead of hard wiring all these overridees
ASP.NET has IOptions?
it isnt asp.net core
i dont think ioptions is new
so yeah I have to manually bind the properties