从.Net Framework Web App,控制台App连接到Azure SignalR Hub

时间:2019-02-07 18:25:19

标签: c# asp.net azure-signalr

我有一个.Net Framework Web应用程序(v 4.6.1),当前使用ASP.Net SignalR。我正在尝试添加另一个SignalR集线器,但是允许使用 Azure SignalR服务连接该集线器,以便我们可以使用REST API向最终用户发送单向消息。

我一直在遵循以下示例herehere来实现该功能。

有3个基本部分

  • 连接到中心(一半正常工作)
  • 通过REST API发送消息(成功运行)
  • 以最终用户身份接收消息(正常工作)

集线器和Azure SignalR服务已创建并且非常简单,无需显示。

代码示例

.Net Framework Web App中的集线器连接 Startup.cs

app.MapAzureSignalR(GetType().FullName, new HubConfiguration { EnableDetailedErrors = true },
    options =>
    {
        options.ConnectionString =
                    ConfigurationManager.ConnectionStrings["AzureSignalR"].ConnectionString;
    });

MapAzureSignalR来自this nuget package

我还具有客户端JavaScript来处理集线器连接和事件,这已经使用标准ASP.Net SignalR运行了几个月。

客户端连接记录以下信息:

Error: You are using a version of the client that isn't compatible with the server. Client version 1.5, server version 2.0.
at Object.error (jquery.signalR.js:179)
at Object.success (jquery.signalR.js:728)
at i (scripts.bundle.js:2)
at Object.fireWith [as resolveWith] (scripts.bundle.js:2)
at A (scripts.bundle.js:4)
at XMLHttpRequest.<anonymous> (scripts.bundle.js:4)
at XMLHttpRequest.wrapFn (zone.js:1166)
at ZoneDelegate.invokeTask (zone.js:421)
at Zone.runTask (zone.js:188)
at ZoneTask.invokeTask [as invoke] (zone.js:496)

Web App具有以下软件包:

  <package id="Microsoft.AspNet.SignalR" version="2.4.0" targetFramework="net461" />
  <package id="Microsoft.AspNet.SignalR.Core" version="2.4.0" targetFramework="net461" />
  <package id="Microsoft.AspNet.SignalR.JS" version="2.4.0" targetFramework="net461" />
  <package id="Microsoft.AspNet.SignalR.SqlServer" version="2.2.2" targetFramework="net45" />
  <package id="Microsoft.AspNet.SignalR.SystemWeb" version="2.4.0" targetFramework="net461" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Owin" version="5.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNetCore.Authentication.Abstractions" version="2.1.0" targetFramework="net461" />
  <package id="Microsoft.AspNetCore.Authorization" version="2.1.0" targetFramework="net461" />
  <package id="Microsoft.AspNetCore.Authorization.Policy" version="2.1.0" targetFramework="net461" />
  <package id="Microsoft.AspNetCore.Connections.Abstractions" version="2.1.0" targetFramework="net461" />
  <package id="Microsoft.AspNetCore.Hosting.Abstractions" version="2.1.0" targetFramework="net461" />
  <package id="Microsoft.AspNetCore.Hosting.Server.Abstractions" version="2.1.0" targetFramework="net461" />
  <package id="Microsoft.AspNetCore.Http" version="2.1.0" targetFramework="net461" />
  <package id="Microsoft.AspNetCore.Http.Abstractions" version="2.1.0" targetFramework="net461" />
  <package id="Microsoft.AspNetCore.Http.Connections" version="1.0.0" targetFramework="net461" />
  <package id="Microsoft.AspNetCore.Http.Connections.Client" version="1.0.0" targetFramework="net461" />
  <package id="Microsoft.AspNetCore.Http.Connections.Common" version="1.0.0" targetFramework="net461" />
  <package id="Microsoft.AspNetCore.Http.Extensions" version="2.1.0" targetFramework="net461" />
  <package id="Microsoft.AspNetCore.Http.Features" version="2.1.0" targetFramework="net461" />
  <package id="Microsoft.AspNetCore.Routing" version="2.1.0" targetFramework="net461" />
  <package id="Microsoft.AspNetCore.Routing.Abstractions" version="2.1.0" targetFramework="net461" />
  <package id="Microsoft.AspNetCore.SignalR" version="1.0.0" targetFramework="net461" />
  <package id="Microsoft.AspNetCore.SignalR.Common" version="1.0.0" targetFramework="net461" />
  <package id="Microsoft.AspNetCore.SignalR.Core" version="1.0.0" targetFramework="net461" />
  <package id="Microsoft.AspNetCore.SignalR.Protocols.Json" version="1.0.0" targetFramework="net461" />
  <package id="Microsoft.AspNetCore.WebSockets" version="2.1.0" targetFramework="net461" />
  <package id="Microsoft.AspNetCore.WebUtilities" version="2.1.0" targetFramework="net461" />
  <package id="Microsoft.Azure.SignalR" version="1.0.6" targetFramework="net461" />
  <package id="Microsoft.Azure.SignalR.AspNet" version="1.0.0-preview1-10317" targetFramework="net461" />
  <package id="Microsoft.Azure.SignalR.Protocols" version="1.0.6" targetFramework="net461" />

.Net Framework控制台应用程序中的集线器连接

var serviceUtils = new ServiceUtils(ConfigurationManager.ConnectionStrings["Azure:SignalR:ConnectionString"].ConnectionString);

var url = GetClientUrl(serviceUtils.Endpoint);

_connection = new HubConnection(url)
{
    ConnectionToken = serviceUtils.GenerateAccessToken(url, userId)
};

IHubProxy proxy = _connection.CreateHubProxy(hubName);

proxy.On("broadcastMessage", (string server, string message) =>
{
    Console.WriteLine($"[{DateTime.Now.ToString()}] Received message from server {server}: {message}");
});


_connection.Start();

记录连接状态时,它显示已断开连接,正在连接,已断开连接

.Net核心代码示例

var serviceUtils = new ServiceUtils(connectionString);

var url = GetClientUrl(serviceUtils.Endpoint);

_connection = new HubConnectionBuilder()
    .WithUrl(url, options =>
    {
        options.AccessTokenProvider = () =>
            {
                return Task.FromResult(serviceUtils.GenerateAccessToken(url, userId));
            };
    }).Build();

_connection.On("broadcastMessage", (string server, string message) =>
{
    Console.WriteLine($"[{DateTime.Now.ToString()}] Received message from server {server}: {message}");
});

await _connection.StartAsync(CancellationToken.None);

这将成功连接并从我的REST API广播中接收消息(请参见下文)

REST API广播

var url = $"{authenticationUtility.Endpoint}/api/v1/hubs/{hubName.ToLower()}";
var request = new HttpRequestMessage
{
    Method = HttpMethod.Post,
    RequestUri = new Uri(url)
};

request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authenticationUtility.GenerateAccessToken(url, sender));
request.Headers.Accept.Clear();
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.AcceptCharset.Clear();
request.Headers.AcceptCharset.Add(new StringWithQualityHeaderValue("UTF-8"));

var content = JsonConvert.SerializeObject(new MessageContent{ Target = "broadcastMessage", Arguments = new []{ sender, message}});
request.Content = new StringContent(content, Encoding.UTF8, "application/json");
var response = await httpClient.SendAsync(request);

这会像我期望的那样返回202 Accepted代码,并将消息发送到.Net Core Console App。

在我的Azure门户上,我能够看到连接,但无法在Web应用程序的集线器连接中接收消息

0 个答案:

没有答案