I'm running a web-Service on Azure which connects to SignalR as following showen:
My Hub-Class:
public class ChangeRequest : Hub
{
protected IHubContext<ChangeRequest> _context;
public ChangeRequest(IHubContext<ChangeRequest> context) => _context = context;
public Task? Send(Guid id) =>
_context.Clients?.All?.SendAsync("SendMessage", id);
}
My Programm.cs
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<CPLopsEntities>(options => options.UseSqlServer(@"Server=tcp:noris.database.windows.net,1433;Initial Catalog=CPLopsTest;Persist Security Info=False;User ID=user;Password=!Pw12345;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"));
builder.Services.AddTransient<ChangeRequest>();
builder.Services.AddSignalR().AddAzureSignalR("Endpoint=https://xxxxxxx.service.signalr.net;AccessKey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;Version=1.0;");
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.UseAzureSignalR(r => r.MapHub<ChangeRequest>("/com"));
using (var scope = app.Services.CreateScope())
scope.ServiceProvider.GetRequiredService<CPLopsEntities>().Database.Migrate();
app.Run();
When a new Entry is added by calling the web-Service, I call _hub.Send():
private readonly CPLopsEntities _context;
private readonly ChangeRequest _hub;
public CompanyController(CPLopsEntities context, ChangeRequest hub)
{
_context = context;
_hub = hub;
}
public IActionResult Insert(Entry entry)
{
Guid newId = CompanyDB.Instance.InsertEntry(entry);
_hub.Send(newId);
return Ok();
}
Since my Client-Side is a WPF-Application, I did not found quiet a lot information about implementation. What I found so far is this:
private const string host = "https://xxxxxxxx.service.signalr.net";
private HubConnection connection;
private Thread thread;
public MainViewModel()
{
thread = new Thread(() =>
{
connection = new HubConnectionBuilder()
.WithUrl(host + "/com")
.WithAutomaticReconnect()
.Build();
connection.On<Guid>("ReceiveMessage", g => ExternalLoadCompany(g));
connection.StartAsync();
});
thread.Start();
}
private void ExternalLoadCompany(Guid id)
{
if (ApiCall.GetCompany(id).Result is Company c)
{
Companies.Clear();
Companies.Add(c);
}
}
So far, my WPF-Application does not recognize any messages sent by the web-Service which makes sense, since I did not authenticate it, however, I found no information about how to pass the full Connectionstring including the AccessKey when trying to register my WPF-App and all information I found so far are blazor-apps or other kind of web-apps.