1

I have added .NET Aspire to my ASP.NET Core Web API backend. But I get the following error when building the app on builder.MapDefaultEndpoints():

Error (active) CS1929 'WebApplicationBuilder' does not contain a definition for 'MapDefaultEndpoints' and the best extension method overload 'Extensions.MapDefaultEndpoints(WebApplication)' requires a receiver of type 'Microsoft.AspNetCore.Builder.WebApplication'

How can I solve this?

This is my program.cs:

try
{
    Log.Information("Starting Rest API");
    var builder = WebApplication.CreateBuilder(args);

    var startup = new Startup(builder.Configuration);

    startup.ConfigureServices(builder.Services);

    builder.AddServiceDefaults();
    builder.MapDefaultEndpoints();

    ConfigureLogger(builder.Host);

    var app = builder.Build();

    startup.Configure(app, app.Environment);

    app.Run();
}
catch (Exception ex)
{
    Log.Fatal(ex, "Host terminated unexpectedly");
}
finally
{
    Log.CloseAndFlush();
}

1 Answer 1

1

I reproduced your problem. enter image description here

The reason is that WebApplicationBuilder does not have a MapDefaultEndpoints method. In the documentation, we can learn that MapDefaultEndpoints() is an extension method provided by .NET Aspire for configuring default endpoints. In the Extensions.cs file, we can see that:

public static WebApplication MapDefaultEndpoints(this WebApplication app)
{
     // Adding health checks endpoints to applications in non-development environments has security implications.
     // See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments.
     if (app.Environment.IsDevelopment())
     {
         // All health checks must pass for app to be considered ready to accept traffic after starting
         app.MapHealthChecks("/health");

         // Only health checks tagged with the "live" tag must pass for app to be considered alive
         app.MapHealthChecks("/alive", new HealthCheckOptions
         {
             Predicate = r => r.Tags.Contains("live")
         });
     }

     return app;
}

The MapDefaultEndpoints method is defined as a WebApplication type, so This method should be called on WebApplication, not WebApplicationBuilder, we should move MapDefaultEndpoints() to the app object:

app.MapDefaultEndpoints();
Sign up to request clarification or add additional context in comments.

5 Comments

Tnx Yuning For your reply. i'll try it and let you know.
Hi Yuning. I Have added app.MapDefaultEndpoints() after var app = builder.Build() And it does compile now. But in the aspire dashboard i don't get the url under "endpoints". It says Endpoints None
I'm have also added swagger in my api
You can check if you have added the project to the application model in the App host project. For example: var builder = DistributedApplication.CreateBuilder(args); builder.AddProject<Projects.WebApplication44>("webapplication44"); i.sstatic.net/buWe6ZUr.png Note: WebApplication44 is my sample API project, you should change it to your corresponding project.
For more .NET Aspire integration, you can refer to these documents:learn.microsoft.com/en-us/dotnet/aspire/fundamentals/… ,learn.microsoft.com/en-us/dotnet/aspire/get-started/… . If you still have questions, you can create a new case to ask.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.