I was asking for some help…
I need to use the IConnectionFactory (Microsoft.Extensions.Http package) in the WiseJ application.
I would like to inject it where needed, but the corresponding “Application.Services.AddHttpClient” does not exist
so I used the Inject system of Asp .Net Core by putting in the directive you suggested:
Application.Services.AddService<IServiceProvider>(app.Services);
This works, but the Session Inject does not work, i.e. the “Application.Services.AddService<T>(ServiceLifetime.Session)“.
But I also need to keep some data in the session for the connected user, to do this I used
“Application.Session[“myKey”] = myObj”
but I’m not quite sure if this is clean compared to the of the expected Inject type “Application.Services.AddService<T>(ServiceLifetime.Session)” in particular on its live cicle.
So I would like to ask what do you think is the best method?
– Keep the ASP.Net Core system as described above:
-> In this case I would like to ask the best method to manage the object lifecycle in “Application.Session”.
– Or keep the WiseJ Inject method that natively provides for the Inject with “ServiceLifetime.Session”:
-> In this case I would like to ask how I can properly register the “IClientFactory” object in order to
inject it into the code
Bets Regards
Tiziano
Hi Tiziano,
Session lifetime doesn’t work with ASPNet Core Services becauseASPNet Core doesn’t support it; It’s either a singleton or a new instance each tine the service is requested or a new instance each time the service is requested per thread.
Unfortunately, we cannot change the way it works.
However, Wisej.NET Service Manager instead can register services per session. Any service; Just use AddService() passing the service type.
You can also register a service using a factory callback and create the service instance in code however it needs to be created.
As for Dependency Injection, for Wisej.NET containers it’s automatic when you use [Inject].
For services created by ASPNet it cannot work but if you use a factory callback that you can use apnet service container to create a service instance using theirDependency Injection.
HTH,
Alaa
Please login first to submit.