To configure a static class with DI in .NET Core I can do this:
public static class MyStaticClass
{
private static IHttpContextAccessor _contextAccessor;
public static void Configure(IHttpContextAccessor contextAccessor)
{
_contextAccessor = contextAccessor;
}
}
Then in Startup:
MyStaticClass.Configure(app.ApplicationServices.GetService<IHttpContextAccessor>());
How to register a GENERIC static class on Startup?
public static class MyGenericStaticClass<T>
{
private static IHttpContextAccessor _contextAccessor;
public static void Configure(IHttpContextAccessor contextAccessor)
{
_contextAccessor = contextAccessor;
}
}
Then on Startup what?
I tried this but obviously it is missing the generic type and throwing an error:
MyGenericStaticClass.Configure(app.ApplicationServices.GetService<IHttpContextAccessor>);
and I am not sure where to declare the type?
you have to declare type like below.