1

I had SignalR working with a HubConnection prior to .NET Core 6. Now the same code is generating an error when you try to start the connection that the builder is creating:

New-line characters are not allowed in header values.

I also think my other version was working with HTTP and this new project is using HTTPS. The Hub is up and working fine within the website itself. But my external app is having trouble establishing a connection using the following code:

var notifyConnection = new HubConnectionBuilder()
    .WithUrl(baseUrl + "/notify", options =>
    {
        options.AccessTokenProvider = async () =>
        {
            var stringData = JsonConvert.SerializeObject(new { username = user, password = pass });
            var content = new StringContent(stringData);
            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            var response = await httpClient.PostAsync(baseUrl + "/api/token", content);
            response.EnsureSuccessStatusCode();
            return await response.Content.ReadAsStringAsync();
        };
    })
    .WithAutomaticReconnect()
    .Build();

notifyConnection.Closed += async (error) =>
{
    if (Debug) _logger.LogInformation("Hub connection closed: " + error.Message);
    await Task.Delay(new Random().Next(0, 5) * 1000);
    await notifyConnection.StartAsync();
};
try
{
    var task = notifyConnection.StartAsync();
    task.Wait();
}
catch (Exception ex)
{
    if (Debug) _logger.LogInformation("Hub connection start error: " + ex.Message);
}

The exception happens when the connection is attempted to be started asynchronously. Anyone run into a similar issue? I have verified that stringData does not have new-line characters. SignalR client is 6.0.6. I am stumped. Any help is greatly appreciated.

UPDATE:

I had trouble with the token generator. I was able to get past this error. Now it is throwing the following error:

The SSL connection could not be established, see inner exception.

The inner exception says that the certificate is invalid. That is because the certificate is for an external connection rather than the server's IP address. The HttpClient is already ignoring certificate errors with the following code:

var handler = new HttpClientHandler();
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
handler.ServerCertificateCustomValidationCallback =
    (httpRequestMessage, cert, cetChain, policyErrors) =>
    {
         return true;
    };
httpClient = new HttpClient(handler);

UPDATE 2:

It is now working. I had to also tell the web sockets to ignore certificate errors. Found the solution in another question:

https://stackoverflow.com/a/63973431/2022236

Psyfun
  • 353
  • 3
  • 15

0 Answers0