Не получается сбилдить ServiceProvider из-за сгенерированног grpc клиента
У меня есть grpc-клиент который автогенерируется через Grpc.Core
и есть сервис в который инъектируется этот клиент. Выглядит так:
public ShoppingCartService(Products.ProductsClient client)
{
_client = client;
}
В Program:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddGrpc();
builder.Services.AddTransient<IShoppingCartService, ShoppingCartService>();
var app = builder.Build(); //Вот тут исключение
app.MapGrpcService<Products.ProductsClient>();
Получаю следующее исключение:
System.AggregateException:
Some services are not able to be constructed (Error while validating the service descriptor
'ServiceType: CartService.Service.IShoppingCartService Lifetime: Transient
ImplementationType: CartService.Service.ShoppingCartService':
Unable to resolve service for type 'ProductService.Service.Products+ProductsClient'
while attempting to activate 'CartService.Service.ShoppingCartService'.)
InvalidOperationException: Unable to resolve service for type
'ProductService.Service.Products+ProductsClient'
while attempting to activate 'CartService.Service.ShoppingCartService'.
Я пытался добавить вот эту строчку (до и после AddGrpc()
):
builder.Services.AddTransient<Products.ProductsClient>();
,
но это не помогло, ошибка осталась той же. В чём может быть проблема?
Источник: Stack Overflow на русском