1

I have an ASP.NET Core 8.0 MVC (upgraded from .NET 6.0) project; it's working fine in a Docker container except for the pages that contain Blazor code. I'm getting an error

blazor.server.js:1 [2025-04-01T04:53:37.291Z] Error: System.Net.Http.HttpRequestException: Connection refused (localhost:32784)

After investigation, I found that HttpRequest in .razor page was causing the issue.

protected async override void OnInitialized()
{
    var request = new HttpRequestMessage(HttpMethod.Get, uri);
    var response = await client.SendAsync(request);
}

Program.cs:

builder.Services.AddServerSideBlazor().AddCircuitOptions(options => { options.DetailedErrors = true; });

builder.Services.AddControllersWithViews();

builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

builder.Services.AddDistributedMemoryCache();

builder.Services.AddHttpClient();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
    app.UseMigrationsEndPoint();
}
else
{
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.UseSession();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.MapRazorPages();

app.MapBlazorHub();

.csproj:

<PackageReference Include="HtmlAgilityPack" Version="1.11.43" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="8.0.14" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="8.0.14" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.14" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="8.0.14" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.14" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.14">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="8.0.7" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.2.19" />
16
  • Hi msd, does your problem only occur in docker? I tested it and vs2022 can also reproduce the problem. Commented Apr 1 at 4:18
  • 1
    Yes, only in a Docker container Commented Apr 1 at 4:21
  • After analyzing your scenario, I think you want to reload the page to reconnect after the websocket is disconnected. So I don't think your current solution is appropriate. Imagine that your website user is filling in the form information. When the websocket is disconnected, the page is reloaded, which is very unfriendly. Commented Apr 1 at 4:21
  • I honestly don’t remember why there’s a reload. As I recall, I just copied this code. Fortunately, the page doesn’t include any input; it only displays text and a few buttons. Commented Apr 1 at 4:31
  • 1
    Wait a minute, I just discovered that this error occurs regardless of whether I run it in Docker or not. It seems unrelated to the issue. However, a new error has occurred only in Docker. My bad, I will edit my question now. Commented Apr 1 at 5:00

2 Answers 2

1

We can find the issue is related to the httpclient from the error message.

System.Net.Http.HttpRequestException: Connection refused (localhost:32784)

We can fix it by using configurations like below.

builder.Services.AddHttpClient("MyClient", client =>
{
    // you can use host.docker.internal in docker environment
    client.BaseAddress = new Uri("http://host.docker.internal:your_port/");
    // or use below setting according to our needs
    // client.BaseAddress = new Uri("http://api-service:8080/");
});
Sign up to request clarification or add additional context in comments.

Comments

0

I found it! It turns out I wasn’t using the Docker ports (EXPOSE 8080, EXPOSE 8081) in the HttpRequest URI.

Comments

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.