Reputation: 680
I have a .Net Framework Web Application (v 4.6.1) that currently uses ASP.Net SignalR. I'm trying to add another SignalR Hub, but allow this one to connect using the Azure SignalR Service so that we can use the REST API to send one-way messages to end-users.
I've been following these examples here and here to implement the feature.
There's 3 basic parts
The Hub and Azure SignalR Service have been created and are simple, no need to display.
Code Samples
Hub Connection in .Net Framework Web App Startup.cs
app.MapAzureSignalR(GetType().FullName, new HubConfiguration { EnableDetailedErrors = true },
options =>
{
options.ConnectionString =
ConfigurationManager.ConnectionStrings["AzureSignalR"].ConnectionString;
});
MapAzureSignalR
comes from this nuget package
I've also got client-side JavaScript to handle hub connections and events, this has been up-and-running for a few months with standard ASP.Net SignalR.
The client side connection logs the following information:
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 has the following packages:
<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" />
Hub Connection in .Net Framework Console App
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();
When logging the connection state it displays Disconnected, Connecting, Disconnected.
.Net Core Code Sample
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);
This connects successfully and received messages from my REST API broadcast (see below)
REST API Broadcast
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);
This returns a 202 Accepted
code like I expect and sends the message to the .Net Core Console App.
On my Azure portal, I'm able to see connections but I'm not able to receive messages in my Web App's Hub Connection
Upvotes: 3
Views: 2887
Reputation: 680
I ended up solving this by creating a .Net Core Web Application that just managed the Hub and then connected to it using the (now deprecated) @aspnet/signalr npm package. This package has now been replaced by the @microsoft/signalr package
Upvotes: 2