好吧,我意识到 SignalR.Client.NET.35 库有点晚。
在撰写本文时,它没有打包在 NuGet 中,因此必须从 GitHub SignalR 项目站点下载代码并作为项目添加到解决方案中(需要SignalR.Client.NET和SignalR.Client.NET35)。
这是最终的解决方案,以防将来可以帮助某人:
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);
}
}
需要注意的重要一点:连同已编译的 SQL SERVER 2008R2 CLR 库,您必须将以下 dll 放在同一文件夹中:
- 牛顿软件.Json
- SignalR.Client.Net35 显然
- SM诊断
- System.Runtime.Serialization
- 正确版本中的 System.ServiceModel(如果不兼容,请参阅 C:\Windows\assembly 中 GAC 中所述的版本)。
- 系统线程
最后在 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
为了调用 ut_sendMsgToHub,我使用了一个服务代理,以便我确信函数执行的任何问题都与处理数据的存储过程分离