使用 Azure SignalR 服务连接到 SignalR 集线器

问题描述 投票:0回答:1

我正在尝试创建一个拥有天蓝色信号服务的设置。

然后我托管一个本地服务器,它是一个 dotnet core 控制台应用程序,它使用连接字符串“连接”到 azure signalr 服务。

然后我有一个客户端(winforms),它尝试使用相同的连接字符串连接到相同的azure signalr服务。

但我收到此错误:System.UriFormatException:“无效的 URI:URI 方案无效。” 当我尝试启动客户端时。

我已在 azure 门户网站上创建了 azure signalr 服务。

然后我创建了 C# dotnet core 控制台应用程序,当它运行时,它使用连接字符串添加 azure signalR 服务:

// Add services to the container.
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd"));

builder.Services.AddSignalR().AddAzureSignalR(@"Endpoint=<my url>.service.signalr.net;AccessKey=<my accesskey>;Version=1.0;");

var app = builder.Build();
app.MapHub<ChatSampleHub>("/ChatSampleHub");

它运行良好,当我启动控制台应用程序时,我在天蓝色门户网站上看到服务器已启动并正在运行。

我有一个像这样的 ChatSampleHub 类:

public class ChatSampleHub : Hub
{
    public Task BroadcastMessage(string name, string message) =>
    Clients.All.SendAsync("broadcastMessage", name, message);

    public Task SendMessage(string message) =>
    Clients.All.SendAsync("MessageReceived", message);

    public Task Echo(string name, string message) =>
        Clients.Client(Context.ConnectionId)
                .SendAsync("echo", name, $"{message} (echo from server)");
}

现在我创建了一个 winforms 应用程序,我想调用聊天中心并向所有其他客户端广播。 到目前为止,当用户单击连接按钮时,我在客户端上有这个:

private async void buttonConnect_Click(object sender, EventArgs e)
{
    hubConnection = new HubConnection(AzureSignalRConnectionString);
    hubProxy = hubConnection.CreateHubProxy("/ChatSampleHub");

    hubProxy.On<string>("MessageReceived", message =>
    {
        // Handle incoming message
        // This is just an example; you can update your UI or perform any desired action here
        AppendMessageToTextBox(message);
    });

    try
    {
        await hubConnection.Start();
        MessageBox.Show("Connected to Azure SignalR");
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error connecting to Azure SignalR: " + ex.Message);
    }
}

AzureSignalRConnectionString 与我在控制台应用程序中使用的连接字符串相同。 但是当我调用等待 hubConnection.Start(); 时我收到此错误: System.UriFormatException:“无效的 URI:URI 方案无效。”

我做错了什么?

azure winforms asp.net-core signalr
1个回答
0
投票

这是 Azure SignalR 服务工作流程。

在您的场景中,您已在 dotnet core 控制台应用程序中使用了 azure signalr 服务连接字符串,该应用程序应该是一个 Web 应用程序。

这个控制台应用程序是我的屏幕截图中的

Web App
,其他客户端(Javascript或winform(.net core))应该与这个Web应用程序协商并获得安全性,之后客户端可以连接azure signalr服务。

© www.soinside.com 2019 - 2024. All rights reserved.