【问题标题】:Signal R in Web ApiWeb Api 中的信号器
【发布时间】:2018-03-16 12:15:55
【问题描述】:

我有 Web Api,它服务于来自 Web 应用、Android 应用和桌面的 CRUD 帖子

我想将 SignalR 添加到 Web Api,每次调用 Controller 中的 Action Create 时,我想通知所有用户 Post 已创建。

问题是,我只能在 Web Api 中找到任何实现,所有实现都在带有 Web Api 的 Web App 或类似的东西中。我阅读了所有关于它的 MSDN 文档。我现在挣扎了3-4天。

我设法实现了 SignalR,并且我的服务器没有创建任何我需要从 Web 应用脚本调用的信号器/集线器文件。它仅在我在本地运行应用程序时创建 - 如果我在 Azure 上发布它,则不会创建该文件。

任何人都有仅在 Web Api 中实施的具体步骤吗?

我试过这个博客,它有 Web Api 的东西,但在项目中添加了 js 并添加了本地 html。它不是独立的 REST api。

这不是关于不创建信号器/集线器文件。它是关于使用 SignalR 创建独立的 Web Api。

我有启动:

public void Configuration(IAppBuilder app) {
            // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888

            app.MapSignalR("/signalr", new Microsoft.AspNet.SignalR.HubConfiguration());
        }

我的中心:

public class ServiceStatusHub : Hub {
        private static IHubContext hubContext =
            GlobalHost.ConnectionManager.GetHubContext<ServiceStatusHub>();

        public static void SetStatus(string message) {
            hubContext.Clients.All.acknowledgeMessage(message);
        }

    }

在我的 Api 控制器中我调用:

ServiceStatusHub.SetStatus("Status changed!");

我制作了控制台应用程序来测试 Api,添加了 Signal R 客户端和类:

class SignalRMasterClient {

        public string Url { get; set; }
        public HubConnection Connection { get; set; }
        public IHubProxy Hub { get; set; }

        public SignalRMasterClient(string url) {
            Url = url;
            Connection = new HubConnection(url, useDefaultUrl: false);
            Hub = Connection.CreateHubProxy("ServiceStatusHub");
            Connection.Start().Wait();

            Hub.On<string>("acknowledgeMessage", (message) =>
            {
                Console.WriteLine("Message received: " + message);

                /// TODO: Check status of the LDAP
                /// and update status to Web API.
            });
        }

        public void SayHello(string message) {
            Hub.Invoke("hello", message);
            Console.WriteLine("hello method is called!");
        }

        public void Stop() {
            Connection.Stop();
        }

    }

program.cs:

 var client = new SignalRMasterClient("myUrl.com/signalr");

            // Send message to server.
            client.SayHello("Message from client to Server!");

我收到 500 内部服务器错误。

如何测试我的 Web Api signalR 是否确实有效?

【问题讨论】:

  • 请不要重复发布相同的问题
  • 我编辑了我的问题以明确,不是同一个问题...如果可以,请帮助,我知道规则 - 谢谢。

标签: c# asp.net-web-api signalr


【解决方案1】:

我看到了一些问题:

1) 您不需要集线器中的 hubContext 字段。您从 Hub 继承。这个类已经包含一个“Clients”属性。

public class ServiceStatusHub : Hub {          
        public static void SetStatus(string message) {
            Clients.All.acknowledgeMessage(message);
        }
    }

2) 在服务器启动时记录错误。

public SignalRMasterClient(string url) {
            Url = url;
            Connection = new HubConnection(url, useDefaultUrl: false);
            Hub = Connection.CreateHubProxy("ServiceStatusHub");
            Connection.Start().ContinueWith(task => { if (task.IsFaulted) {
                    Console.WriteLine("There was an error opening the connection:{0}",
                                      task.Exception.GetBaseException());
                } else {
                    Console.WriteLine("Connected");
                }
            });

            Hub.On<string>("acknowledgeMessage", (message) =>
            {
                Console.WriteLine("Message received: " + message);

                /// TODO: Check status of the LDAP
                /// and update status to Web API.
            }).Wait();
        }

3) 检查客户端网址。在创建客户端时,您的路径中不需要信号器。 只是:

var client = new SignalRMasterClient("myUrl.com/");

在这里,您会找到一个运行示例,它可以满足您的所有需求: SignalR Console app example

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-27
    • 1970-01-01
    • 2019-11-26
    相关资源
    最近更新 更多