Dependency injection with asp.net core for properties - asp.net-core

In spring boot you have the ability to inject properties or environment variables into your code so that they are available upon start time. If they are not set then the application fails upon starting.
Is there anything similar in ASP.net core? Basically I want to inject properties/environment variables and they are not set then the app should fail at startup and notify this.

In ASP.NET Core DI system, you can only inject a dependency via the constructor but you can use another DI System like AutoFac, Ninject or Lamar to use Property injection.

Related

Is IHttpClientFactory registered in Asp .Net Core 3 by default?

I'm working on Asp.Net Core 3.1 project and it seems that it's possible to resolve an instance of IHttpClientFactory via DI without adding Microsoft.Extensions.Http package to my project and without registering the middleware (via AddHttpClient() method).
public ServiceClientProxy(IHttpClientFactory httpClientFactory)
{
this.httpClientFactory = httpClientFactory; // it just works
}
In the previous versions of Asp.Net Core the additional setup was required (installing nuget, registering the middleware).
Does it mean that this service is registered in DI by default automatically under the hood in version 3.1?
Does it make sense to call AddHttpClient() explicitly in the project's middleware?
The ability to resolve IHttpClientFactory via DI without adding Microsoft.Extensions.Http package to my project can be caused by the fact that other third party services used in my project call AddHttpClient() in their code.
In case an empty Asp.Net Core 3.1 project is created, IHttpClientFactory instance cannot be resolved via DI without proper registration in Startup.cs
So it makes sense to explicitly call AddHttpClient() method in Startup.cs

Resolve a multiTenant Service outside asp.net core pipeline

Our project is using multitenancy to resolve some Service, lets say MyService based on SaasKit.
We also have a background task, which shares some of it's dependencies with the asp.net core controllers.
In the background task, any object that depends on MyService will get a null reference.
I can implement workarounds to get instances of MyService, like using service locator pattern, but this approach fails to create classes that depend on MyService without breaking DI and IOC logic.
So the question is: How can I get the same services that I can get from HttpContext with multitenancy, but get them without an HttpCoontext?

How does Kephas integrate with ASP.NET Core?

So much I could understand, Kephas does not provide its own DI container, but it builds adapters on existing ones (Kephas has its own dependecy injection container. Why another framework, why not use an existing one?). Existing adapters are for System.Composition (MEF2) and, as I learned recently, for Autofac (starting with version 6.5.0, Cannot use constructors in open generic services with Kephas).
However, ASP.NET comes with its own implementation of a DI container. In this regard, is it possible to use Kephas with ASP.NET Core now, and if yes, how?
Starting with version 6.5.0, Kephas will provide also an ASP.NET Core adapter so, yes, it will be shortly possible to use Kephas with ASP.NET Core. However, the built-in Dependency Injection does not have all the features Kephas requires, naming metadata and lazy instantiation. There will also be an adapter for Microsoft.Extensions.DependencyInjection, but without the aforementioned functionality, so I do not really recommend it. The Autofac adapter is the recommended one (event the Microsoft ASP.NET Core recommends it for advanced scenarios).
On the other hand, you could let Kephas manage its dependencies using MEF2 or Autofac and provide to ASP.NET Core a service provider aggregating the default one (or the one of your choice) and the one from Kephas. This has the following drawbacks:
You will end up with two containers.
The Kephas container will not have access to the services provided by ASP.NET Core.

How to save info in asp.net core startup to be available in app

I have a set of 7 .Net Framework WebApi-based services that all share some common design elements. One shared element is that each will include the service version in the data that it returns from any of its endpoints. In each service, I determine the version from the executing assembly using reflection. I do this in Application_Start and store the result in a property that I create on the Global class that inherits from System.Web.HttpApplication. That way I do the reflection work once and access the result later from each of my methods.
I'm building a new service and this one is built on ASP.NET Core. So I'm trying to figure out how to do the same thing in ASP.NET Core. I can add the reflection logic in Startup.Configure (though it's not really about configuring the Http pipeline which is what Configure is supposed to be doing). Is there a better place than Startup.ConfigureServices or Startup.Configure, to put code that you want to run once on startup?
And where would I store the result to make it readily accessible to each of the downstream methods called from my controller actions?

Implementing Custom Session State Store Provider in dotnet core

I was just planning to implement a custom session state provider for a distributed cache product in .net core but I could not find SessionStateStoreProviderBase and related classes in dotnet core. Can somebody tell me where to find them?
Session is very different in ASP.NET Core. It's designed around IDistributedCache and takes the implementation from DI.
https://github.com/aspnet/Caching/blob/rel/1.1.2/src/Microsoft.Extensions.Caching.Abstractions/IDistributedCache.cs
This is more relevant now:
https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.session?view=aspnetcore-2.2
I believe you'll want to inject in your own ISessionStore for the SessionManager to pick up.
SessionManager