Sqlserver
 sql >> Base de données >  >> RDS >> Sqlserver

Transfert de données de SQL Server vers une application Web avec SignalR

Eh bien, je me suis rendu compte un peu tard de la bibliothèque SignalR.Client.NET.35.

Au moment de la rédaction, il n'est pas empaqueté dans NuGet, donc le code doit être téléchargé du site du projet GitHub SignalR et ajouté en tant que projet à la solution (à la fois SignalR.Client.NET et SignalR.Client.NET35 obligatoire).

Voici la solution finale, au cas où cela pourrait aider quelqu'un à l'avenir :

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 à noter :avec la bibliothèque CLR SQL SERVER 2008R2 compilée, vous devrez placer les dll suivantes dans le même dossier :

  • Newtonsoft.Json
  • SignalR.Client.Net35 évidemment
  • SMdiagnostics
  • System.Runtime.Serialization
  • System.ServiceModedans la bonne version (reportez-vous à la version indiquée dans le GAC dans C:\Windows\assembly en cas d'incompatibilités).
  • System.Threading

enfin dans 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 

Pour appeler ut_sendMsgToHub j'utilise un service broker afin d'être sûr que tout problème d'exécution de la fonction est découplé des procédures stockées traitant les données