Asp.Net IServiceCollection 服务的 Core 5.0 问题

Asp.Net Core 5.0 Issue with IServiceCollection services

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using Travel.Application;
using Travel.WebApi.Filters;
using Travel.Data;
using Travel.Shared;

namespace Travel.WebApi
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddApplication();
            services.AddInfrastructureData();
            services.AddInfrastructureShared(Configuration);

            services.AddHttpContextAccessor();

            services.AddControllers();

            services.AddControllersWithViews(options =>
                options.Filters.Add(new ApiExceptionFilter()));
            services.Configure<ApiBehaviorOptions>(options =>
                options.SuppressModelStateInvalidFilter = true
            );

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "Travel.WebApi", Version = "v1" });
            });
        }
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseSwagger();
                app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Travel.WebApi v1"));
            }

            app.UseHttpsRedirection();
            app.UseRouting();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}

所以这是我的“启动”文件,我有一条错误消息:

Severity Code Description Project File Line Suppression State Error CS1061 'IServiceCollection' does not contain a definition for 'AddInfrastructureData' and no accessible extension method 'AddInfrastructureData' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?) Travel.WebApi C:\Users\Alexandru\Desktop\WebDev\AdminApp\src\presentation\Travel.WebApi\Startup.cs 26 Active

我只是..卡住了。为什么找不到“AddInfrastructureData”?

如果您错过了任何包裹,您可以查看您的指南

如果您没有复制下面的代码,您可以查看您的指南:

 public static class DependencyInjection
    {
        public static IServiceCollection AddInfrastructureData(this IServiceCollection services)
        {   
            return services;
        }
        public static IServiceCollection AddInfrastructureShared(this IServiceCollection services,IConfiguration configuration)
        {
            return services;
        }
    }

尝试在 AddInfrastructureData 所在的位置添加一个 using 命名空间。

Travel.Data 替换为您代码中的适当命名空间:

namespace Travel.Data;

public static class ServiceCollectionExtensions
{
  public static IServiceCollection AddInfrastructureData(this IServiceCollection servicesCollection)
  {
    //...
    return servicesCollection;
  }
}

// Startup.cs

using Travel.Data;

如果扩展方法位于不同的项目或包中,请通过右键单击项目的 Dependencies 子菜单和 select“添加项目引用...”或“管理 NuGet”来添加它包..."。

如果没有帮助,请提供更多信息。 AddInfrastructureData 是您自己的方法还是外部方法等?