【问题标题】:System.AggregateException in API "Some services are not able to be constructed"API中的System.AggregateException“无法构造某些服务”
【发布时间】:2022-02-18 01:00:02
【问题描述】:

在我的 API 中,我必须包装 WCF 服务,我添加了对项目的引用并 我将此添加到 IServiceCollection

services.AddScoped<IServiceClient, ServiceClient>(sp =>
  new ServiceClient($"{configuration.GetValue<string>(Constant.Url)}"));


我的 ServiceClient 类:

public class ServiceClient: IServiceClient 
{

  private string urlService;

  // WCF Service to wrapp
  private WCFServiceReference.WCFServiceServiceClient _WCFServiceServiceClient; 

  public ServiceClient(string urlService) 
  {
    this.urlService = urlService;
    BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
    this._WCFServiceServiceClient = new WCFServiceReference.WCFServiceServiceClient(basicHttpBinding, new EndpointAddress(this.urlService));
  }

  public Task<CalculateResponse> CalculateAsync(Request input) 
  {
    return CalculateAsync(input, CancellationToken.None);
  }

  public Task<GetProductListResponse> GetProductListAsync(ProductRequest input) 
  {
    return GetElencoProdottiAsync(input, CancellationToken.None);
  }

  public Task<GetVariableOfProductResponse> GetVariableOfProductAsync(GetVariableRequest input) 
  {
    return GetVariableOfProductAsync(input, CancellationToken.None);
  }

  private Task<GetProductListResponse> GetProductListAsync(ProductRequest input, CancellationToken none
  {
    return this._WCFServiceServiceClient.GetProductListAsync(input);
  }

  private Task<GetVariableOfProductResponse> GetVariableOfProductAsync(GetVariableRequest input, CancellationToken none) 
  {
    return this._WCFServiceServiceClient.GetVariableOfProductAsync(input);
  }

  private Task<CalculateResponse> CalculateAsync(Request input, CancellationToken none) 
  {
    return this._WCFServiceServiceClient.CalculateAsync(input);
  }

}

当我启动我的应用程序时,此调用 CreateHostBuilder(args).Build().Run(); 在 Main 上升以下 Excpetion:

某些服务无法构建(验证时出错 服务描述符 'ServiceType: Namespace.IProductService 生命周期:Scoped ImplementationType:Namespace.ProductService':无法 在尝试时解析“Namespace.ServiceClient”类型的服务 激活“Namespace.ProductService”。)

【问题讨论】:

    标签: c# wcf


    【解决方案1】:

    您的ProductService服务生命周期是多少?

    单例无法解析 Scope 服务

    【讨论】:

    • ProductService 服务生命周期有范围
    【解决方案2】:

    或许你可以试试这个,具体可以看this post

     public class ServiceClient : IServiceClient
        {
          ...
           public ServiceClient(IServiceClient ServiceClient)
           {
               ...
           }
        }
    

    【讨论】:

      猜你喜欢
      • 2022-08-20
      • 2021-12-12
      • 2020-11-19
      • 1970-01-01
      • 1970-01-01
      • 2021-07-07
      • 2021-07-09
      • 1970-01-01
      • 2021-02-27
      相关资源
      最近更新 更多