Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

[Dependency Injection] Registering multiple generic interfaces.

Hi there guys,
I've been trying to solve this for the past 2 hours without any luck of even finding hints on where, how, or what to look for.

Lets say I have 2 interfaces, a public one will be exposed to those using my library, and internal one exposed to my code allowing extra functionality, and a class that implements them:
PHP Code

<?php 
public interface IRequest<out T>
    {    }

    
internal interface IRequestSet<T>: IRequest<T>
    {    }
    public class 
Request<T> : IRequestSet<T>
    {    } 

Now, I want to register those 2 interfaces for DI, but I want them to have the same object, so I did this:
PHP Code

<?php 
services
.AddScoped(typeof(IRequestSet<>), typeof(Request<>));
            
services.AddScoped(typeof(IRequest<>), provider => provider.GetService(typeof(IRequestSet<>))); 
However, when I try this, I'm getting a run time exception: "System.ArgumentException: 'Open generic service type 'Interfaces.IRequest`1[T]' requires registering an open generic implementation type.'"

Anyone got idea how to solve this?? or even pointers on what to look for??
Have a cool signature? hit me up and I might use it.
check https://stackoverflow.com/a/24454423 or https://stackoverflow.com/a/14402629
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
(Jul 5, 2017 09:13 AM)HFDone Wrote:  check https://stackoverflow.com/a/24454423 or https://stackoverflow.com/a/14402629

Forgot to mention, I'm using the Microsoft DI container, due to some limitations I'm not allowed to change it.
Also, I've already done the part of using same object but for a non generic type, no idea why it's not working this time...

Side question: any idea how to consume services during MVC setup?
Spoiler (Click to View)

Problem solved by changing the approach, closing this thread now.
Have a cool signature? hit me up and I might use it.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by john ronaldsadJul 20, 2022 06:49 PM 4 237 Jul 20, 2022 06:49 PM
Last Post: john ronaldsad
[.NET] by cannibalherdMar 17, 2022 08:07 AM 5 161 Mar 17, 2022 08:07 AM
Last Post: benio2
[C#] by SicknessAug 16, 2020 10:19 AM 19 424 Aug 16, 2020 10:19 AM
Last Post: Sickness