I'm new on asp.net core with .NET7.0, and I'm trying to develop a simple web app that comunicates with a PC on the same LAN and responds to an http POST sent by the web server. To send the httpPOST I use the HttpClient library. First of all, my Program.cs looks like this:
using Microsoft.AspNetCore.Cors;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using RazorPagesMovie.Data;
using RazorPagesMovie.Models;
using System.Net;
using System.Reflection.PortableExecutable;
var builder = WebApplication.CreateBuilder(args);
var MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
builder.Services.AddCors(options =>
{
options.AddPolicy(name: MyAllowSpecificOrigins,
policy =>
{
policy.AllowAnyOrigin();
policy.AllowAnyHeader();
policy.AllowAnyMethod();
});
});
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddDbContext<RazorPagesMovieContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("RazorPagesMovieContext") ?? throw new InvalidOperationException("Connection string 'RazorPagesMovieContext' not found.")));
builder.Services.AddCors();
builder.Services.AddControllers();
var app = builder.Build();
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
SeedData.Initialize(services);
}
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// 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.UseRouting();
app.UseCors(MyAllowSpecificOrigins);
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/echo",
context => context.Response.WriteAsync("echo"))
.RequireCors(MyAllowSpecificOrigins);
endpoints.MapControllers()
.RequireCors(MyAllowSpecificOrigins);
endpoints.MapGet("/echo2",
context => context.Response.WriteAsync("echo2"));
endpoints.MapRazorPages();
});
app.MapRazorPages();
app.Run();
Then, the method that I use to send a POST is this:
[HttpOptions]
[EnableCors("_myAllowSpecificOrigins")]
private async void sendMessage(string message)
{
using (var httpClient = new HttpClient())
{
var content = new StringContent(message, Encoding.UTF8, "text/plain");
var response = httpClient.PostAsync("http://ip:8000/", content).Result;
if (response.IsSuccessStatusCode)
{
Console.WriteLine("Success");
}
else
{
Console.WriteLine("Fail");
}
}
}
Once I've explained this, I'll expose my error. Once I start the web app, I start a script on linux that constantly listens to any http message sent. This script should receive a message when I click a certain button that launches the http POST method, but the nothing is sent.
The message is sent when the app reaches its timeout and shows this warning:
var response = httpClient.PostAsync("http://ip:8000/", content).Result;
System.AggregateException: 'One or more errors occurred. (A task was canceled.)'
Also it is sent when, while the app loads trying to send it I close the server program.
My guess is that it is sent in these occasions because in that moment the ip/port is not occupied (I'm no expert at all in this).
Also, as you can see I've implemented the solution obvserved in other stackoverflow questions of the CORS problem... and nothing worked.
Does somebody know what might be the issue?
Edit:
I've sniffed the server-side with wireshark and I've seen the following behavoir:
- The HTTP connection is stablished by the typical 3-step handshake.
- The POST request is successfully sent and acknowledged by the client.
- During the next 100 seconds the
PostAsync()
method is waiting for a response that is never sent. - When the timeout is reached the method launches an exception and decides to request the end of the http socket, which is succesfully done(I think so).
- Then the client prints "Answered to the request: ..." (don't know exactly if that happens before or after the [ACK+FIN] TCP packets, it is almost simultaneous)
Below you can see the TCP/HTTP frames sniffed: [![Traffic sniffed during problem reproduction][1]][1]
To confirm that the error is server-side, here you have the code used in my linux terminal:
#!/bin/bash
PORT=5555
while true; do
message=$(nc -l -p $PORT | grep -i "HELLO")
if [[ ! -z "$message" ]]; then
echo "How are you?" | nc -q 0 server_ip 5555
echo "Answered to the request: $message"
fi
done
PD: As you can see the string HELLO is what I send on the POST request.