Home > database >  Minimal API: Dependency injection error - Unable to resolve service for type
Minimal API: Dependency injection error - Unable to resolve service for type

Time:10-20

I could not find answer that would resolve my issue. I'm getting 2 exceptions in my minimal API:

1: System.AggregateException

Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: MinApiEFCore.Data.ConnectRepo Lifetime: Singleton ImplementationType: MinApiEFCore.Data.ConnectRepo': Unable to resolve service for type 'DocuWare.Platform.ServerClient.ServiceConnection' while attempting to activate 'MinApiEFCore.Data.ConnectRepo'.) (Error while validating the service descriptor 'ServiceType: MinApiEFCore.Data.IConnectRepo Lifetime: Scoped ImplementationType: MinApiEFCore.Data.ConnectRepo': Unable to resolve service for type 'DocuWare.Platform.ServerClient.ServiceConnection' while attempting to activate 'MinApiEFCore.Data.ConnectRepo'.)'

2: InvalidOperationException

Unable to resolve service for type 'DocuWare.Platform.ServerClient.ServiceConnection' while attempting to activate 'MinApiEFCore.Data.ConnectRepo'.

"ServiceConnection" is from an "external" class from DocuWare (via NuGet) used for creating a connection.

My IConnectRepo interface looks like this:

public interface IConnectRepo
{
    ServiceConnection CreateServiceConnectionAsync(Uri uri, string user, string password);        
}

ConnectRepo class:

public class ConnectRepo : IConnectRepo
{
    private ServiceConnection _serviceConnection;

    public ConnectRepo(ServiceConnection serviceConnection)
    {
        _serviceConnection = serviceConnection;
    }

    public ServiceConnection CreateServiceConnectionAsync(Uri uri, string user, string password)
    {
        //Organizations == null means I am not connected
        if (_serviceConnection.Organizations.FirstOrDefault() == null)
        {
            _serviceConnection = ServiceConnection.Create(uri, user, password);
        }
        return _serviceConnection;
    }
}

Program.cs

..
builder.Services.AddScoped<IConnectRepo, ConnectRepo>();
..

Still not sure why I cannot inject ServiceConnection in the constructor. Any help would be appreciated.

CodePudding user response:

You cannot inject ServiceConnection as it has not been added to DI container. You can register the service like following...

..
builder.Services.AddScoped<IConnectRepo, ConnectRepo>();
builder.Services.AddScoped<ServiceConnection>();
..

...but then you will run into another problem as ServiceConnection doesn't have public parameterless constructor.

Resolution for this is to instruct DI container how to create ServiceConnection instance.

..
builder.Services.AddScoped<IConnectRepo, ConnectRepo>();
builder.Services.AddScoped<ServiceConnection>(sp => ServiceConnection.Create("some service URI"));
..

Be aware that you might need to use different service lifetime to avoid performance issues.

ServiceConnection documentation

IServiceCollection extension methods documentation

CodePudding user response:

Since you use IConnectRepo as depandency, when dependancy injection tries to create an instance of ConnectRepo, faced with another dependancy which is ServiceConnection.

you must register ServiceConnection into your dependency injection tool. but regarding to your code, you don't need to get ServiceConnection inside constructor remove it from constructor to fix problem.

public class ConnectRepo : IConnectRepo {
    private ServiceConnection _serviceConnection;

     public ConnectRepo()
     {
     }
 
     public ServiceConnection CreateServiceConnectionAsync(Uri uri, string user, string password)
     {
         //Organizations == null means I am not connected
         if (_serviceConnection.Organizations.FirstOrDefault() == null)
         {
             _serviceConnection = ServiceConnection.Create(uri, user, password);
         }
         return _serviceConnection;
     } }
  • Related