C
C#23h ago
Robert

Signalr

Hi! For work I have to connect the middleware with the backend in a fast way so I ended up deciding to use signalr. I have a serverless signalr instance running in azure and an azure function for the bindings I am start with a small poc to fully understand how it's working The azure function is uploaded as a file as it is too big
1 Reply
Robert
RobertOP23h ago
Here's the Program.cs ( i use the isolated worker as in-process will be deprecated)
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using DotnetIsolated_ClassBased;

var host = new HostBuilder()
.ConfigureFunctionsWebApplication(b => b.Services
.AddServerlessHub<Functions>())
.Build();

host.Run();
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using DotnetIsolated_ClassBased;

var host = new HostBuilder()
.ConfigureFunctionsWebApplication(b => b.Services
.AddServerlessHub<Functions>())
.Build();

host.Run();
Here is the sample webapi
using Microsoft.AspNetCore.SignalR.Client;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

var connection = new HubConnectionBuilder()
.WithUrl("http://localhost:7104/api/", (options) =>
{
options.Headers.Add("UserId", "Sal");
})
.Build();

connection.On<string>("Broadcast", Console.WriteLine);
await connection.StartAsync();
await connection.SendAsync("Broadcast", "This is a broadcast");

app.UseHttpsRedirection();
app.Run();
using Microsoft.AspNetCore.SignalR.Client;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

var connection = new HubConnectionBuilder()
.WithUrl("http://localhost:7104/api/", (options) =>
{
options.Headers.Add("UserId", "Sal");
})
.Build();

connection.On<string>("Broadcast", Console.WriteLine);
await connection.StartAsync();
await connection.SendAsync("Broadcast", "This is a broadcast");

app.UseHttpsRedirection();
app.Run();
I have the azure function running locally on 7104, and the 2 webapis are discoverable through public webtunnels. Negotiation works fine
[2024-11-20T11:11:26.291Z] Executing 'Functions.negotiate' (Reason='This function was programmatically called via the host APIs.', Id=3fb28b70-9442-4792-9d63-8d1f33c04f56) [2024-11-20T11:11:26.321Z] C# HTTP trigger function processed a request. [2024-11-20T11:11:26.324Z] Executed 'Functions.negotiate' (Succeeded, Id=3fb28b70-9442-4792-9d63-8d1f33c04f56, Duration=33ms) [2024-11-20T11:20:27.626Z] Executing 'Functions.negotiate' (Reason='This function was programmatically called via the host APIs.', Id=c3fd10f6-9d87-4a4c-b82b-8fcca120ebbc) [2024-11-20T11:21:43.723Z] C# HTTP trigger function processed a request. [2024-11-20T11:21:43.730Z] Executed 'Functions.negotiate' (Succeeded, Id=c3fd10f6-9d87-4a4c-b82b-8fcca120ebbc, Duration=76104ms)
[2024-11-20T11:11:26.291Z] Executing 'Functions.negotiate' (Reason='This function was programmatically called via the host APIs.', Id=3fb28b70-9442-4792-9d63-8d1f33c04f56) [2024-11-20T11:11:26.321Z] C# HTTP trigger function processed a request. [2024-11-20T11:11:26.324Z] Executed 'Functions.negotiate' (Succeeded, Id=3fb28b70-9442-4792-9d63-8d1f33c04f56, Duration=33ms) [2024-11-20T11:20:27.626Z] Executing 'Functions.negotiate' (Reason='This function was programmatically called via the host APIs.', Id=c3fd10f6-9d87-4a4c-b82b-8fcca120ebbc) [2024-11-20T11:21:43.723Z] C# HTTP trigger function processed a request. [2024-11-20T11:21:43.730Z] Executed 'Functions.negotiate' (Succeeded, Id=c3fd10f6-9d87-4a4c-b82b-8fcca120ebbc, Duration=76104ms)
but any other method invoked from the client to server just doesn't work Am I approaching the problem wrong? My goal is to send messages back and forth in a fast way And the reason i "decided" on signalr is for the auto scalability which is needed
Want results from more Discord servers?
Add your server