Configuring .Net Client
Learn to configure the .Net client.
Introduction
Before we start, we need to make sure all of the following namespace references are present in the Program.cs
file of the DotnetClient
project.
-SignalRServer-DotnetClient-Program.cs
We can locate this file by writing the following path in the terminal below:
cd SignalR-on-.NET-6---the-complete-guide/Chapter-04/Part-03/LearningSignalR/DotnetClient
Then, we'll locate the initialization statement for the hubConnection
object and replace it with the following:
using Microsoft.AspNetCore.Connections;using Microsoft.AspNetCore.Http.Connections;using Microsoft.AspNetCore.SignalR.Client;using Microsoft.Extensions.Logging;
The hubConnection
object
var hubConnection = new HubConnectionBuilder().WithUrl(url,HttpTransportType.WebSockets,options => {options.AccessTokenProvider = null;options.HttpMessageHandlerFactory = null;options.Headers["CustomData"] = "value";options.SkipNegotiation = true;options.ApplicationMaxBufferSize = 1_000_000;options.ClientCertificates = new System.Security.Cryptography.X509Certificates.X509CertificateCollection();options.CloseTimeout = TimeSpan.FromSeconds(5);options.Cookies = new System.Net.CookieContainer();options.DefaultTransferFormat = TransferFormat.Text;options.Credentials = null;options.Proxy = null;options.UseDefaultCredentials = true;options.TransportMaxBufferSize = 1_000_000;options.WebSocketConfiguration = null;options.WebSocketFactory = null;}).ConfigureLogging(logging => {logging.SetMinimumLevel(LogLevel.Information);logging.AddConsole();}).Build();
You might have noticed that we have applied HttpTransportType.WebSockets
as one of the call parameters. This is one of the ways of applying allowed transport mechanisms to .NET clients. Another way is to set it as one of the options. But whichever way you choose, you can still use bitwise operations to apply multiple transport mechanisms.
As you can see, there are more configuration options on the .NET client compared to the JavaScript one. But, as before, we will get an overview of them all.