c# - 使用Web API作为SignalR服务器并从Windows服务使用它

标签 c# azure asp.net-web-api windows-services signalr

我在同一台服务器上有一个 Web 应用程序和一个 Windows 服务,Web 应用程序使用 .net 远程处理与 Windows 服务进行通信。 Windows 服务检查与 LDAP 的连接是否正常工作,然后返回 true,否则抛出异常。 Windows 服务的状态在网站上更新。

现在基础设施将要改变。 Web 应用程序在 Azure 上运行,Windows 服务将保留在客户端计算机上(因为 LDAP 位于客户端)。我需要像现在一样更新网络应用程序上的状态。我引入了 Web API 作为 Web 应用程序和 Windows 服务之间的中间层。

我找不到更好的解决方案来实现这种情况。我考虑使用 SignalR 或 Akka.remote。

到目前为止我的想法是,如果我在 Web API 和 Windows 服务中使用 SignalR 并执行以下操作:

  • Web 应用程序使用 Web API 方法
  • Web API 方法使用 SignalR 并向 Windows 服务发送信号
  • Windows 服务检查 LDAP 连接并调用 Web API 方法返回状态。

注意:我不知道如何使 Windows 服务作为客户端并使其能够监听 Web api 向其发送的信号,因为我不需要为 Windows 服务使用自托管。我们可以使用 Web API,因为它已经托管了。

可以实现吗?或者有什么更好的解决方案吗?请帮忙。 提前致谢。

最佳答案

我能够解决这个问题并找到解决方案。

Web API 中的startup.cs 中的SignalR 配置

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.MapSignalR("/signalr", new Microsoft.AspNet.SignalR.HubConfiguration());
    }
}

在 Web API 中添加了中心

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

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

    }

在 Web API 操作方法中

    public IEnumerable<string> Get()
    {
        // Query service to check status
        ServiceStatusHub.GetStatus("Please check status of the LDAP!");
        return new string[] { "val1", "val2" };
    }

在控制台应用程序中添加 SignalR 客户端

public 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 类中

class Program
{
    static void Main(string[] args)
    {
        var client = new SignalRMasterClient("http://localhost:9321/signalr");

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

        Console.ReadKey();

        // Stop connection with the server to immediately call "OnDisconnected" event 
        // in server hub class.
        client.Stop();
    }
}

现在在 postman 中运行 Web API 并运行控制台应用程序。将建立双向通信。

注意:以下代码修复了控制台关闭时未立即触发 OnDisconnected 事件的问题。

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

Check the image showing result.

关于c# - 使用Web API作为SignalR服务器并从Windows服务使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46364036/

相关文章:

azure - Azure Devops Pipelines 中的循环和数组

c# - Web Api 2.2 OData V4 函数路由

c# - OData 无法识别我的集合属性

json - ASP.NET web api : documenting/specifying a service

c# - 用四个不同的数字随机填充列表

c# - 要调用此方法, "Membership.Provider"属性必须是 "ExtendedMembershipProvider"的实例

c# - BeginInvoke 会阻止 UI,而 Invoke 不会。为什么?

c# - 内存流问题

azure - 如何使用 Microsoft Graph API 更新 Azure AD 中的现有用户

asp.net - 配置 Azure 的事件中心以接收来自 ASP.NET MVC Web 应用程序的事件