首页 文章

从Azure函数调用SignalR

提问于
浏览
3

我正在尝试使用我的Azure系统设置SignalR . 设置相当标准

  • 用ASP.NET MVC编写的Azure App Service中的主Web门户

  • 执行业务逻辑的 Cloud 服务工作者角色 . worker角色侦听门户网站写入的队列 . 由于依赖于传统的C业务逻辑,我不得不使用 Cloud 服务

当用户从门户开始耗时的操作时,我希望 Cloud 服务能够通过SignalR通知所有连接的浏览器 Cloud 服务已完成 .

我想我可以使用Azure函数(如SignalR客户端)来执行此操作,可以触发该操作以强制SignalR使用Azure函数中的代码来广播消息

HubConnection hub = new HubConnection(*URL OF PORTAL GOES HERE*);

    var proxy = hub.CreateHubProxy(*NAME OF HUB GOES HERE*);
    await hub.Start();
    await proxy.Invoke("Hello"); // Name of default hub method

ASP.NET MVC方面,我也使用Azure Service Bus队列,这似乎是扩展时的建议 . 所以,在我的Startup.cs中,我得到了

public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            GlobalHost.DependencyResolver.UseServiceBus(@"*SERVICE BUS CONNNECTION STRING GOES HERE*", "blah");
            app.MapSignalR();
        }
    }

注意:为了使其与最新的SignalR一起工作,我必须使用Microsoft.AspNet.SignalR.ServiceBus3 nuget包,否则我在调用UseServiceBus时遇到异常 .

现在,这一切似乎都有效 . 我可以从PostMan调用Azure功能,并且在我的浏览器中确实可以通过信号器调用 .

但是,我不确定这是否是正确的做法 . 在这种情况下缩放是否会起作用,还是有更好的方法可以强制信号器广播消息?

谢谢

1 回答

  • 4

    你有正确的想法 . 我运行相同的架构,除了我使用WebJob而不是Azure功能 . 几点:

    • 它's a good choice to go with the Service Bus backplane for SignalR. I'已经运行了一些相当高的量,并且它表现良好 . 我在这里与StackOverflow聊过的另一位开发人员使用了Redis背板路线并遇到了很多问题 .

    • 如果您没有尝试重新 Build 连接的逻辑,则've got the right approach with creating a proxy in your Azure Function. However, establishing a HubConnection is a heavyweight operation. For performance, consider creating the HubConnection and proxy once, then reusing. You will need to make sure that you handle the case where SignalR loses the connection to your Hub, exhausts retries, then gives up. If you are reusing the HubConnection and this happens, you'会遇到麻烦 .

相关问题