Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Issue with implementing Keyed DI in C#

I have 3 SFTP providers for my application. I am trying to use newly introduced .net 8 Keyed DI feature to register these SFTP providers:

var hostname1 = "hostname";
var portNo1 = 22;
var username1 = "username";
var password1 = "password";

services.AddKeyedTransient<SftpClient>("SFTP1", new SftpClient(hostname1, portNo1, username1, password1));

Later retreive it like:

public IActionResult Get([FromKeyedServices("SFTP1")] SftpClient client)
 {
    do something with the SFTPclient...
 }

The issue is that the code is not compiling:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The error is:

Severity Code Description Project File Line Suppression State
Error CS1503 Argument 3: cannot convert from ‘Renci.SshNet.SftpClient’ to ‘System.Func<System.IServiceProvider, object?, Renci.SshNet.SftpClient>’ TestSFTP C:\Users\Windows 10 VM\source\repos\TestSFTP\TestSFTP\Program.cs 18 Active

error screenshot

I do not know how to use IServiceProvider to register keyed SftpClient.
Please help me.

>Solution :

A transient service means "new instance every time it’s required".

Therefore, it doesn’t make sense to pass an instance to AddKeyedTransient, you need to pass a factory method instead:

services.AddKeyedTransient<SftpClient>(
    "SFTP1",
    (provider, key) => new SftpClient(hostname1, portNo1, username1, password1));

Every time that service is requested from the provider, the factory method is invoked, and you get a new instance.


If you do want to register a single instance, then use AddKeyedSingleton for that:

services.AddKeyedSingleton<SftpClient>(
    "SFTP1",
    new SftpClient(hostname1, portNo1, username1, password1));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading