В приложении ASP.NET Core-6 я использую службу SOAP (WCF). Я уже добавил его в сервис Connected как SchoolReference. Затем у меня есть следующий код:
ШколаСервис:
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
Программа.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();
В Program.cs я добавил Depency Injection как AddDependencyInjection
Как мне решить эту проблему?
Ваш конструктор класса SchoolService
зависит от SchoolAppClient
, который не определен в коллекции зависимостей и вызывает проблему при создании зависимости для того же