dotnet / aspire-samples

MIT License
571 stars 158 forks source link

[WebToolsE2E][Aspire]Open eShopLite.sln and run, page show "There is a problem loading our products, Please try again later" after select "Products" #259

Open v-yuwzh opened 2 months ago

v-yuwzh commented 2 months ago

INSTALL STEPS

  1. Clean machine: Win11 x64 23h2 ENU
  2. Install Dev17.10.0 Preview 7 [rel.d17.10-34902.84] (Includes Aspire 8.0.0-preview.7.24251.11)
  3. Apply NuGet Feeds.
  4. Install Git.

REPRO STEPS

  1. Run command "git clone https://github.com/MicrosoftDocs/mslearn-dotnet-cloudnative-devops.git eShopLite" in Command Prompt.
  2. Open eShopLite.sln solution >right click eShopLite solution > select Configure Startup Projects > select Multiple startup projects > in the Action column, select Start for both the Products and Store projects > press OK button.
  3. Press F5 to run the project.
  4. Select Products in the page.

Note: When do scenario Tutorial: Add .NET Aspire to an existing .NET 8 microservices app meet this issue, can refer this for details information.

ACTUAL: Show "There is a problem loading our products, Please try again later" image

EXPECTED Page show with no problem.

vicperdana commented 2 months ago

Could you post the errors from the console in VS or VSCode?

v-yuwzh commented 2 months ago

@vicperdana, the error information for eShopLite\Store\bin\Debug\net8.0\Store.exe as below: image

info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:7085
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5158
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\aa\eShopLite\Store
info: System.Net.Http.HttpClient.ProductService.LogicalHandler[100]
      Start processing HTTP request GET http://localhost:5200/api/Product
info: System.Net.Http.HttpClient.ProductService.ClientHandler[100]
      Sending HTTP request GET http://localhost:5200/api/Product
fail: Store.Services.ProductService[0]
      Error during GetProducts.
      System.Net.Http.HttpRequestException: No connection could be made because the target machine actively refused it. (localhost:5200)
       ---> System.Net.Sockets.SocketException (10061): No connection could be made because the target machine actively refused it.
         at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
         at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
         at System.Net.Sockets.Socket.<ConnectAsync>g__WaitForConnectWithCancellation|285_0(AwaitableSocketAsyncEventArgs saea, ValueTask connectTask, CancellationToken cancellationToken)
         at System.Net.Http.HttpConnectionPool.ConnectToTcpHostAsync(String host, Int32 port, HttpRequestMessage initialRequest, Boolean async, CancellationToken cancellationToken)
         --- End of inner exception stack trace ---
         at System.Net.Http.HttpConnectionPool.ConnectToTcpHostAsync(String host, Int32 port, HttpRequestMessage initialRequest, Boolean async, CancellationToken cancellationToken)
         at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
         at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
         at System.Net.Http.HttpConnectionPool.AddHttp11ConnectionAsync(QueueItem queueItem)
         at System.Threading.Tasks.TaskCompletionSourceWithCancellation`1.WaitWithCancellationAsync(CancellationToken cancellationToken)
         at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
         at System.Net.Http.DiagnosticsHandler.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
         at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
         at Microsoft.Extensions.Http.Logging.LoggingHttpMessageHandler.<SendCoreAsync>g__Core|5_0(HttpRequestMessage request, Boolean useAsync, CancellationToken cancellationToken)
         at Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.<SendCoreAsync>g__Core|5_0(HttpRequestMessage request, Boolean useAsync, CancellationToken cancellationToken)
         at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
         at Store.Services.ProductService.GetProducts() in C:\aa\eShopLite\Store\Services\ProductService.cs:line 21

the information for eShopLite\Products\bin\Debug\net8.0\Products.exe as below: image

info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (41ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT COUNT(*) FROM "sqlite_master" WHERE "type" = 'table' AND "rootpage" IS NOT NULL;
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT EXISTS (
          SELECT 1
          FROM "Product" AS "p")
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:7130
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5228
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\aa\eShopLite\Products
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT "p"."Id", "p"."Description", "p"."ImageUrl", "p"."Name", "p"."Price"
      FROM "Product" AS "p"
vicperdana commented 2 months ago

Could you paste in your AppHost Program.cs?

v-yuwzh commented 2 months ago

@vicperdana, in solution "eShopLite", there is no AppHost project, image so I provide program.cs in Products as below:

using Microsoft.EntityFrameworkCore;
using Products.Data;
using Products.Endpoints;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<ProductDataContext>(options =>
    options.UseSqlite(builder.Configuration.GetConnectionString("ProductsContext") ?? throw new InvalidOperationException("Connection string 'ProductsContext' not found.")));

// Add services to the container.
var app = builder.Build();

// Configure the HTTP request pipeline.
app.MapProductEndpoints();

app.UseStaticFiles();

app.CreateDbIfNotExists();

app.Run();

provide Program.cs in Store as below:

using Store.Components;
using Store.Services;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddSingleton<ProductService>();
builder.Services.AddHttpClient<ProductService>(c =>
{
    var url = builder.Configuration["ProductEndpoint"] ?? throw new InvalidOperationException("ProductEndpoint is not set");

    c.BaseAddress = new(url);
});

// Add services to the container.
builder.Services.AddRazorComponents()
    .AddInteractiveServerComponents();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error", createScopeForErrors: true);
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();

app.UseStaticFiles();
app.UseAntiforgery();

app.MapRazorComponents<App>()
    .AddInteractiveServerRenderMode();

app.Run();
vicperdana commented 2 months ago

This doesn't seem related to Aspire, but to run this successfully, you'll need to ensure that the applicationUrl in Products launchSettings.json is defined exactly the same per appSettings.json ProductEndpoint in Store.

The default config seems to point to different address http://localhost:5200 vs http://localhost:5228

image

v-yuwzh commented 2 months ago

@vicperdana , After I ensure that the applicationUrl in ProductslaunchSettings.json is defined exactly the same per appSetting.json ProductEndpoint in Store.

this issue works well, first thanks for you suggestion, for the user, need change applicationUrl value is unfriendly to the user after clone this solution, could you help to update this solution or do you know who is owner for this solution? Thanks in advance. image image

vicperdana commented 2 months ago

I am not aware - perhaps raise an issue here?

v-yuwzh commented 2 months ago

I am not aware - perhaps raise an issue here?

Ok, thanks, I will open an issue in here

jamesmontemagno commented 2 months ago

Can you please PR the sample