Maltion
Maltion

Reputation: 53

Unable to resolve service for type 'DbToProcessor.Interfaces.ICosmosDbRepository' while attempting to activate 'DbToProcessor.InfoProcessor'

I have an .NET 6.0 application (Azure FunctionApp) and use Dependency Injection to inject a repository to my processor. However, I am getting an error: Microsoft.Extensions.DependencyInjection.Abstractions: Unable to resolve service for type 'DbToProcessor.Interfaces.IMyCosmosDbRepository' while attempting to activate 'DbToProcessor.ModelProcessor'.

ICosmosDbRepository:

  public class MyCosmosDbRepository : IMyCosmosDbRepository
  {
    private readonly ICosmosDbRepository<Model> _myCosmosDbRepository;
    public MyCosmosDbRepository(ILogger<MyCosmosDbRepository> logger,
        ICosmosDbRepository<Model> myCosmosDbRepository)
    {
        _logger = logger;
        _myCosmosDbRepository= myCosmosDbRepository;
    }

    public async Task<IList<Model>> GetSoftDeletedMlModelInfoAsync()
    {
        try
        {
            return await _myCosmosDbRepository.FindAsync(x => x.IsDeleted);
        }
        catch (Exception ex)
        {
            _logger.LogError("Deleted....| Exception: {ExceptionMessage}", ex.Message);
            throw;
        }
    }

}

DbToProcessor private readonly ILogger _logger;

    private readonly IMyCosmosDbRepository _repository;

    public ModelProcessor(ILogger<ModelProcessor> logger,
        IMyCosmosDbRepository repository)
    {
        _repository = repository;
        _logger = logger;
    }
    [FunctionName("CleanUp")]
    public async Task ModelCosmosCleanUpAsync([TimerTrigger("* * * * *")] TimerInfo timer)
    {
        try
        {
            var softDeletedModels = await _repository.GetSoftDeletedModelAsync();


            foreach (var model in softDeletedModels)
            {
                await _repository.DeleteModelAsync(mlModel.Id, mlModel.SiteId);
            }

        }
        catch (Exception exception)
        {
            ...
        }
    }

Startup

        builder.Services.AddSingleton(sp => {

            var client = new DocumentClient(new Uri(Convert.ToString("")), Convert.ToString(""));

            return new CosmosDbBuilder()
                 .WithId("MyDatabaseName")
                 .AddCollection<SiteMLModelInfo>("model", builder =>
                     builder.IncludePartitionkeyPath("/myId")
                            .IncludePartitionkeySelector(b => b.MyId))
                .Build(client);
        });

        builder.Services.AddSingleton(sp => sp.GetRequiredService<ICosmosDb>().Repository<Model>());

        builder.Services.AddScoped<IMyCosmosDbRepository, MyCosmosDbRepository>();

Probably I am missing something in Startup initialization but I can't find what could cause this exception.

Upvotes: 0

Views: 47

Answers (0)

Related Questions