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

问题描述 投票:0回答:1

在 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>();
    }
}

然后是Program.cs

Program.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.Service\Program.cs:line 32

var app = builder.Build();

IN Program.cs 我将依赖注入添加为 AddDependencyInjection

如何解决这个问题?

c# asp.net-core wcf asp.net-web-api
1个回答
0
投票

您的

SchoolService
类构造函数依赖于
SchoolAppClient
,它未在依赖项集合中定义,并在为 same

创建依赖项时导致问题
© www.soinside.com 2019 - 2024. All rights reserved.