Pushing data from SQL Server to Web Application with SignalR

∥☆過路亽.° 提交于 2019-11-30 00:49:17

Well, I realized a bit late about the SignalR.Client.NET.35 library.

At the time of writing, it is not packaged in NuGet, so the code must be downloaded from the GitHub SignalR Project Site and added as a project to the solution (both SignalR.Client.NET and SignalR.Client.NET35 required).

Here is the final solution, in case it can help someone in the future:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Xml;
using Microsoft.SqlServer.Server;
using System.Data.SqlTypes;
using System.Net;
using System.IO;
using System.Xml.XPath;
using SignalR.Client.Hubs;

    internal static HubConnection connectionT = null;
    internal static IHubProxy msgHubT = null;

    /// <summary>
    /// allows SSRV to send a message to the Web Socket hub
    /// </summary>
    /// <param name="URL">URL of the Hub</param>
    /// <param name="hubName">Name of the message Hub to be used for broadcasting.</param>
    /// <param name="hubMethod">Hub method to be used for broadcasting.</param>
    /// <param name="message">Message to be broadcasted.</param>
    [SqlFunction()]
    public static void ut_sendMsgToHub(string URL, string hubName, string hubMethod, string message)
    { 
      try
        {
        if (connectionT == null)
        {
            connectionT = new HubConnection(URL.Trim()); // "http://localhost:56844/M2Hub"
        }
        if (msgHubT == null)
        {
            msgHubT = connectionT.CreateProxy(hubName.Trim());//"M2data"
        }

            if (!(connectionT.State == SignalR.Client.ConnectionState.Connected 
                || connectionT.State == SignalR.Client.ConnectionState.Reconnecting
                 || connectionT.State == SignalR.Client.ConnectionState.Connecting))
                connectionT.Start().Wait();
            msgHubT.Invoke(hubMethod.Trim(), message.Trim()).Wait();//"Send"
        }
        catch (Exception exc)
        {
            SqlContext.Pipe.Send("ut_sendMsgToHub error: " + exc.Message + Environment.NewLine);
        }
    }

Important to notice: together with the compiled SQL SERVER 2008R2 CLR library you'll have to place the following dlls in the same folder:

  • Newtonsoft.Json
  • SignalR.Client.Net35 obviously
  • SMdiagnostics
  • System.Runtime.Serialization
  • System.ServiceModel in the right version (refer to version as stated in the GAC in C:\Windows\assembly in case of incompatibilities).
  • System.Threading

finally in SQL SERVER:

CREATE ASSEMBLY CLR_Bridge from 'C:\PathToLibraries\Library_CLR.dll' 
WITH PERMISSION_SET = UNSAFE --UNSAFE required
CREATE PROCEDURE ut_sendMsgToHub 
@url nchar(125) ,
@hubName nchar(75),
@hubMethod NCHAR(75),
@message NVARCHAR(MAX)
AS
EXTERNAL NAME CLR_Bridge.[LibraryNamespace.CLR_Bridge].ut_sendMsgToHub 

To call ut_sendMsgToHub I use a service broker so that I am sure that any problem with the execution of the function is decoupled from the stored procedures processing the data

Have you looked at SignalR? You can use it to asynchronously push data to your UI (although it isn't a true push). This may not be acceptable for your specific situation, but I would suggest taking a look. Might be what you need. Hope that helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!