Getting "500 - InternalServerError" when using REST API for groups (POST)
See original GitHub issueDescribe the bug
On our Hangfire Service we use the Azure SignalR RESTful API for broadcasting messages to groups: https://github.com/Azure/azure-signalr/blob/dev/docs/swagger/v1.md#post-broadcast-a-message-to-all-clients-within-the-target-group.
Since 10th or 11th of march last week this POST call does not work anymore, even in our production environment, where we didn’t change anything.
Sadly the Azure SignalR logs doesn’t show anything more then the “InternalServerError” message, with no additional information.
To Reproduce
I tried to expand all needed code into these 2 methods, not our actual code, but it also reproduces the error:
using System;
namespace Core.Application.Hangfire.SignalR
{
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Claims;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Tokens;
using JsonSerializer = System.Text.Json.JsonSerializer;
public class SignalRNotificationService
{
private readonly ILogger<SignalRNotificationService> logger;
public SignalRNotificationService(ILogger<SignalRNotificationService> logger)
{
this.logger = logger;
}
/// <inheritdoc />
public async Task PublishMessageAsync(CancellationToken cancellationToken)
{
var signalRServiceName = "<AZURE-SIGNALR-SERVICE-NAME>";
var accessKey = "<AZURE-SIGNALR-SERVICE-ACCESSKEY>";
var hubName = "<AZURE-SIGNALR-HUB-NAME>"; // any string
var groupName = "<AZURE-SIGNALR-GROUP-NAME>"; // any string
var serverName = "<SIGNALR-SERVER-NAME>"; // any string
var url = $"https://{signalRServiceName}.service.signalr.net/api/v1/hubs/{hubName}/groups/{WebUtility.UrlEncode(groupName)}";
var jwtToken = GenerateAccessTokenExample(url, new[]
{
new Claim(ClaimTypes.NameIdentifier, serverName)
}, accessKey);
var payload = new SignalRPayloadMessage();
payload.Target = "TestTarget";
payload.Arguments = new object[] { "{\"test\": \"value\"}" };
var httpRequest = new HttpRequestMessage(HttpMethod.Post, new UriBuilder(url).Uri);
httpRequest.Headers.Authorization =
new AuthenticationHeaderValue("Bearer", jwtToken);
httpRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpRequest.Content = new StringContent(JsonSerializer.Serialize(payload), Encoding.Unicode, "application/json");
var httpClient = HttpClientFactory.Create();
using var response = await httpClient.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
if (response.StatusCode != HttpStatusCode.Accepted)
{
var errorBody = await response.Content.ReadAsStringAsync();
this.logger.LogError($"Error while sending signalR message to group {groupName}. Error response body: {errorBody}");
}
else
{
this.logger.LogInformation(
$"Sending message to group '{groupName}'.");
}
}
private string GenerateAccessTokenExample(string audience, IEnumerable<Claim> claims, string accessKey)
{
var jwtTokenHandler = new JwtSecurityTokenHandler();
var lifetime = TimeSpan.FromHours(1);
var expire = DateTime.UtcNow.Add(lifetime);
var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(accessKey));
var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
var token = jwtTokenHandler.CreateJwtSecurityToken(
issuer: null,
audience: audience,
subject: claims == null ? null : new ClaimsIdentity(claims),
expires: expire,
signingCredentials: credentials);
return jwtTokenHandler.WriteToken(token);
}
}
public class SignalRPayloadMessage
{
public string Target { get; set; }
public object[] Arguments { get; set; }
}
}
Exceptions (if any)
“500 - InternalServerError” response from Azure SignalR service
Further technical details
- Server AspNetCore Version: 3.1
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
500 Internal server error while I consume the Rest API.
The HTTP 500 means that the external API that you called has an exception. If you have no access to the logging of...
Read more >REST API error code 500 handling
It is caused by a programming error (server), triggered by the request (client). And what if the request variable isn't required by the...
Read more >Should a REST API return a 500 Internal Server Error to ...
A 500-series HTTP error indicates a server malfunction. Apart from 501 Not Implemented and 505 HTTP Version Not Supported , using these error ......
Read more >500 Internal Server Error | B2B Integration
Hello Team, When trying to post the same request via postman or swagger to the same endpoint, it is working fine.But when trying...
Read more >Fixing a 500 internal server error response
A 500 Internal Server Error is an HTTP status code that indicates that the server encountered an unexpected error while processing the request....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Now he sent it to you
Should be resolved now.