Home > other >  Asp.Net Core 5.0 Issue with IServiceCollection services
Asp.Net Core 5.0 Issue with IServiceCollection services

Time:04-25

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

So this is my "StartUp" file and I have an error that says :

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

I just.. am stuck. Why can it not find "AddInfrastructureData"?

CodePudding user response:

Try to add a using namespace where the AddInfrastructureData resides.

Replace Travel.Extensions with the appropriate namespace from your code:

namespace Travel.Extensions;

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

// Startup.cs

using Travel.Extensions;

Please provide more info in case that it was not helpful.

CodePudding user response:

you could check your guide book if you have missed any packages

and you could check your guide book if you have missed to copy the codes like below:

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

enter image description here

  • Related