How to configure in Startup a generic static class?

583 Views Asked by At

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?

1

There are 1 best solutions below

0
CodingMytra On

you have to declare type like below.

MyGenericStaticClass<declare type here>.Configure(app.ApplicationServices.GetService<IHttpContextAccessor>);