I have a class library which I am converting for use with DI in asp.net core. I wish to use this library in an asp.net core web application which mixes both MVC and Blazor server components. I need one particular interface used within this library (say IServiceA) to have two different implementations (ServiceAForMVC and ServiceAForBlazor).
i.e. I need the classes in this library which use constructor injection to get IServiceA to receive an instance of ServiceForMVC when created during a normal mvc page request, and to receive an instance of ServiceAForBlazor when created inside of a Blazor component.
Can this be done?
Initially I had one implementation of this interface, and this implementation used DI to get an IHttpContextAccessor instance.
I was hoping that the IHttpContextAccessor instance would have a null value for HttpContext when requested inside of a Blazor server component, and most of the time this is the case. But I have run into situations where the HttpContext value is not null, but it is disposed, and raises an ObjectDisposedException when I reference the HttpContext property.
So alternatively I'd like to create a 2nd implementation of IServiceA which does not require IHttpContextAccessor, and which would be returned when requested from inside a Blazor component.