0

In CORS configuration, I am allowing OPTIONS method, but when I send a request, an error is thrown

OPTIONS http://x.x.x.x:port/function 405 (Method Not Allowed)

my CORS Policy in ConfigureServices method in Startup.cs is as follows:

services.AddCors(options =>
{
    options.AddPolicy("AllowAny", x =>
  {
    x.AllowAnyOrigin()
     .AllowAnyHeader()
     .WithMethods("POST", "OPTIONS");
  });
});

And it is used as follows:

app.UseCors("AllowAny");

Hub Code:

public async Task SendMessage(string user, string message) {
   string outputMessage = SensorsControl.controlSensors(message.ToString()); 
   await Clients.All.SendAsync("ReceiveMessage", user, outputMessage);
}

SignalR Code in angular:

this._hubConnection = new HubConnection('http://x.x.x.x:port/function'); 
this._hubConnection.start() 
                   .then(() => console.log('Connection started!')) 
                   .catch(err => console.log('Error while establishing connection')); 
this._hubConnection.on('ReceiveMessage', (user: string, outputMessage: string) => 
{ 
  const text = ${user}: ${outputMessage}; this.messages.push(text); 
}); 
7
  • 5
    Can you show your signalr code and your hub code ? Commented Jul 29, 2019 at 16:20
  • 6
    @TonyNgo anything else needed? Commented Jul 29, 2019 at 16:29
  • 7
    update in your question not your comment Commented Jul 29, 2019 at 23:02
  • 6
    Done :) @TonyNgo Commented Jul 30, 2019 at 6:18
  • 5
    You can also try adding the headers elsewhere. I've often used web.config for this purpose: gist.github.com/Jalalhejazi/5653347 Commented Jul 30, 2019 at 7:39

1 Answer 1

1

I solved it guys :D The problem was that my angular app was hosted on localhost which was supposed to be my IPv4 IP, but it turned to be 127.0.0.1 which is not understood by the SignalR server. With no changes, I served angular using ng serve --host 0.0.0.0, and then connected using my IPv4 IP, worked as charm :D thanks for your help tho! Cheers!!

Sign up to request clarification or add additional context in comments.

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.