'SignalR - Error 400 (Request on loopback from external IP)

in the same solution, I have created two project, the first one is a Web Api project which include a SignalR server and the other one is a console project which include the SignalR client.

Here the Startup.cs

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {

        services.AddCors(options => options.AddPolicy("CorsPolicy",
    builder =>
    {
        builder.AllowAnyHeader()
               .AllowAnyMethod()
               .SetIsOriginAllowed((host) => true)
               //.WithOrigins("http://localhost:37720")
               .AllowCredentials();
    }));

        services.AddControllers();
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebService_0LF_POC", Version = "v1" });
        });

        services.AddSignalR(options =>
        options.EnableDetailedErrors = true);

    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseSwagger();
            app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebService_0LF_POC v1"));
        }

        app.UseCors(options =>
            options
            .AllowAnyOrigin()
            .AllowAnyMethod()
           
            //.AllowCredentials()
            );

        app.UseRouting();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
            endpoints.MapHub<FirstHub>("/FirstHub");
        });
        app.UseCors("CorsPolicy");

    }
}

and the Hub class

public class FirstHub : Hub
{
    
    public FirstHub()
    {
       
    }


    #region Public Methods
    public override Task OnConnectedAsync()
    {
        return base.OnConnectedAsync();
    }
    public override Task OnDisconnectedAsync(Exception exception)
    {
        return base.OnDisconnectedAsync(exception);
    }
    public async Task SendMessage(string user, string message)
    {
        await Clients.All.SendAsync("ReceiveMessage", user, message);
    }

    #endregion
}

Below the C# Client code

public class SRClient
{
    HubConnection connection;
    public SRClient()
    {
        connection = new HubConnectionBuilder()
           .WithAutomaticReconnect()
           .WithUrl("http://localhost:37720/FirstHub")
           .Build();
    }

    public async Task Connect()
    {
        try
        {
            await connection.StartAsync();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}
class Program
    {
        static async Task Main(string[] args)
        {

            SRClient sRClient = new SRClient();
            await sRClient.Connect();
            Console.ReadLine();

        
        
        
        }
    }

The StartAsync() throws the following exception: Response status code does not indicate success: 400 (Request on loopback from external IP).

what am I forgetting?

Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source