ASP.NET Core Web API-无法构造某些服务:验证服务描述符ServiceType时出错



在ASP.NET Core-6应用程序中,我正在协调SOAP服务(WCF)。我已经将其作为SchoolReference添加到Connected服务中。然后我有了这个进一步的代码:

学校服务:

using SchoolApp.Interfaces;
using SchoolReference;
using System.Data;
using System.Net.Http;
using SchoolApp.DTOs.Request;
namespace SchoolApp.Implementations
{
   public class SchoolService : ISchoolService
   {
       private readonly SchoolAppClient _client;
       public SchoolService(
           SchoolAppClient client
           )
       {
           _client = client;
       }
       public async Task<SchoolResponse> GetStudentDetailAsync(StudentDetailDto model)
       {
           SCHOOL_DATA requestBody = new SCHOOL_DATA
           {
               DATA_BODY = new DATA_BODY
               {
                   StudentRecord = new StudentRecordType { 
                       fullName = model.FullName, 
                       regNum = model.RegNum
                   }
               }
           };
           var response = await _client.SchoolAsync(requestBody);
           return response;
       }
   }
}

然后,我有依赖注入如下所示:

public static class DIServiceExtension
{
   public static void AddDependencyInjection(this IServiceCollection services)
   {
       services.AddScoped<ISchoolService, SchoolService>();
       services.AddTransient<IValidator<StudentDetailDto>, StudentDetailDtoValidator>();
   }
}

然后程序.cs

程序.cs

using FluentValidation.AspNetCore;
using System.Reflection;
var builder = WebApplication.CreateBuilder(args);
ConfigurationManager configuration = builder.Configuration;
var environment = builder.Environment;
// Add services to the container.
builder.Services.AddHttpClient();
builder.Services.AddMvc();
builder.Services.AddControllers();
builder.Services.AddControllers().AddNewtonsoftJson(op => op.SerializerSettings.ReferenceLoopHandling
           = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
builder.Services.AddControllers()
               .AddFluentValidation(options =>
               {
                   options.AutomaticValidationEnabled = false;
                   options.RegisterValidatorsFromAssembly(Assembly.GetExecutingAssembly());
               });
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// Register Dependency Injection Service Extension
builder.Services.AddDependencyInjection();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
   app.UseSwagger();
   app.UseSwaggerUI();
}
app.UseCors(x => x
       .AllowAnyOrigin()
       .AllowAnyMethod()
       .AllowAnyHeader());
app.UseHttpsRedirection();
app.MapControllers();
app.Run();

然而,当我试图在IIS上启动应用程序时,我在浏览器上遇到了以下错误:

这是第32行:

System.AggregateException
HResult=0x80131500
Message=Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: 
Interfaces.ISchoolService Lifetime: Scoped ImplementationType: Implementations.SchoolService': Unable to resolve service for type 'SchoolReference.SchoolReferenceClient' while attempting to activate 'Implementations.SchoolService'.)
Source=Microsoft.Extensions.DependencyInjection
StackTrace:
  at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(ICollection`1 serviceDescriptors, ServiceProviderOptions options)
  at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options)
  at Microsoft.Extensions.DependencyInjection.DefaultServiceProviderFactory.CreateServiceProvider(IServiceCollection containerBuilder)
  at Microsoft.Extensions.Hosting.Internal.ServiceFactoryAdapter`1.CreateServiceProvider(Object containerBuilder)
  at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()
  at Microsoft.Extensions.Hosting.HostBuilder.Build()
  at Microsoft.AspNetCore.Builder.WebApplicationBuilder.Build()
  at Program.<Main>$(String[] args) in C:SchoolService.ServiceProgram.cs:line 32
var app = builder.Build();

在程序.cs中,我添加了Depency Injection作为AddDependencyInjection

如何解决此问题?

您的SchoolService类构造函数依赖于未在依赖项集合中定义的SchoolAppClient,并在为同一创建依赖项时引发问题