Skip to content

Cross-compile SignalR client libraries and enable platform compat ana… #26140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
5 commits merged into from
Sep 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions global.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"sdk": {
"version": "5.0.100-rc.1.20452.10"
"version": "6.0.100-alpha.1.20472.11"
},
"tools": {
"dotnet": "5.0.100-rc.1.20452.10",
"dotnet": "6.0.100-alpha.1.20472.11",
"runtimes": {
"dotnet/x64": [
"2.1.18",
Expand Down
19 changes: 19 additions & 0 deletions src/Shared/OperatingSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

#if NETCOREAPP
#error Use System.OperatingSystem instead.
#else

using System.Runtime.InteropServices;

namespace Microsoft.AspNetCore
{
internal sealed class OperatingSystem
{
private static readonly bool _isBrowser = RuntimeInformation.IsOSPlatform(OSPlatform.Create("browser"));

public static bool IsBrowser() => _isBrowser;
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -1459,7 +1459,7 @@ private async Task ReconnectAsync(Exception closeException)

private OperationCanceledException GetOperationCanceledException(string message, Exception innerException, CancellationToken cancellationToken)
{
#if NETSTANDARD2_1
#if NETSTANDARD2_1 || NETCOREAPP
return new OperationCanceledException(message, innerException, _state.StopCts.Token);
#else
return new OperationCanceledException(message, innerException);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<Description>Client for ASP.NET Core SignalR</Description>
<TargetFrameworks>$(DefaultNetFxTargetFramework);netstandard2.0;netstandard2.1</TargetFrameworks>
<TargetFrameworks>$(DefaultNetCoreTargetFramework);$(DefaultNetFxTargetFramework);netstandard2.0;netstandard2.1</TargetFrameworks>
<RootNamespace>Microsoft.AspNetCore.SignalR.Client</RootNamespace>
</PropertyGroup>

Expand Down Expand Up @@ -38,4 +38,8 @@
<InternalsVisibleTo Include="Microsoft.AspNetCore.SignalR.Client.Tests" />
</ItemGroup>

<ItemGroup>
<SupportedPlatform Include="browser" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Description>Client for ASP.NET Core SignalR</Description>
<TargetFrameworks>$(DefaultNetFxTargetFramework);netstandard2.0</TargetFrameworks>
<TargetFrameworks>$(DefaultNetCoreTargetFramework);$(DefaultNetFxTargetFramework);netstandard2.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand All @@ -14,4 +14,8 @@
<InternalsVisibleTo Include="Microsoft.AspNetCore.SignalR.Client.Tests" />
<InternalsVisibleTo Include="Microsoft.AspNetCore.SignalR.Client.FunctionalTests" />
</ItemGroup>

<ItemGroup>
<SupportedPlatform Include="browser" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Linq;
using System.Net.Http;
using System.Net.WebSockets;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Connections;
Expand Down Expand Up @@ -38,7 +39,6 @@ public partial class HttpConnection : ConnectionContext, IConnectionInherentKeep
private bool _started;
private bool _disposed;
private bool _hasInherentKeepAlive;
private bool _isRunningInBrowser;

private readonly HttpClient _httpClient;
private readonly HttpConnectionOptions _httpConnectionOptions;
Expand Down Expand Up @@ -152,9 +152,7 @@ public HttpConnection(HttpConnectionOptions httpConnectionOptions, ILoggerFactor
_httpClient = CreateHttpClient();
}

_isRunningInBrowser = Utils.IsRunningInBrowser();

if (httpConnectionOptions.Transports == HttpTransportType.ServerSentEvents && _isRunningInBrowser)
if (httpConnectionOptions.Transports == HttpTransportType.ServerSentEvents && OperatingSystem.IsBrowser())
{
throw new ArgumentException("ServerSentEvents can not be the only transport specified when running in the browser.", nameof(httpConnectionOptions));
}
Expand Down Expand Up @@ -376,7 +374,7 @@ private async Task SelectAndStartTransport(TransferFormat transferFormat, Cancel
continue;
}

if (transportType == HttpTransportType.ServerSentEvents && _isRunningInBrowser)
if (transportType == HttpTransportType.ServerSentEvents && OperatingSystem.IsBrowser())
{
Log.ServerSentEventsNotSupportedByBrowser(_logger);
transportExceptions.Add(new TransportFailedException("ServerSentEvents", "The transport is not supported in the browser."));
Expand Down Expand Up @@ -529,42 +527,49 @@ private HttpClient CreateHttpClient()
var httpClientHandler = new HttpClientHandler();
HttpMessageHandler httpMessageHandler = httpClientHandler;

var isBrowser = OperatingSystem.IsBrowser();

if (_httpConnectionOptions != null)
{
if (_httpConnectionOptions.Proxy != null)
if (!isBrowser)
{
httpClientHandler.Proxy = _httpConnectionOptions.Proxy;
}
// Configure options that do not work in the browser inside this if-block
if (_httpConnectionOptions.Proxy != null)
{
httpClientHandler.Proxy = _httpConnectionOptions.Proxy;
}

try
{
// On supported platforms, we need to pass the cookie container to the http client
// so that we can capture any cookies from the negotiate response and give them to WebSockets.
httpClientHandler.CookieContainer = _httpConnectionOptions.Cookies;
}
// Some variants of Mono do not support client certs or cookies and will throw NotImplementedException or NotSupportedException
// Also WASM doesn't support some settings in the browser
catch (Exception ex) when (ex is NotSupportedException || ex is NotImplementedException)
{
Log.CookiesNotSupported(_logger);
}
try
{
// On supported platforms, we need to pass the cookie container to the http client
// so that we can capture any cookies from the negotiate response and give them to WebSockets.
httpClientHandler.CookieContainer = _httpConnectionOptions.Cookies;
}
catch (Exception ex) when (ex is NotSupportedException || ex is NotImplementedException)
{
// Some variants of Mono do not support client certs or cookies and will throw NotImplementedException or NotSupportedException
Log.CookiesNotSupported(_logger);
}

// Only access HttpClientHandler.ClientCertificates
// if the user has configured those options
// https://github.com/aspnet/SignalR/issues/2232
var clientCertificates = _httpConnectionOptions.ClientCertificates;
if (clientCertificates?.Count > 0)
{
httpClientHandler.ClientCertificates.AddRange(clientCertificates);
}
// Only access HttpClientHandler.ClientCertificates
// if the user has configured those options
// https://github.com/aspnet/SignalR/issues/2232

if (_httpConnectionOptions.UseDefaultCredentials != null)
{
httpClientHandler.UseDefaultCredentials = _httpConnectionOptions.UseDefaultCredentials.Value;
}
if (_httpConnectionOptions.Credentials != null)
{
httpClientHandler.Credentials = _httpConnectionOptions.Credentials;
var clientCertificates = _httpConnectionOptions.ClientCertificates;
if (clientCertificates?.Count > 0)
{
httpClientHandler.ClientCertificates.AddRange(clientCertificates);
}

if (_httpConnectionOptions.UseDefaultCredentials != null)
{
httpClientHandler.UseDefaultCredentials = _httpConnectionOptions.UseDefaultCredentials.Value;
}

if (_httpConnectionOptions.Credentials != null)
{
httpClientHandler.Credentials = _httpConnectionOptions.Credentials;
}
}

httpMessageHandler = httpClientHandler;
Expand Down Expand Up @@ -645,7 +650,7 @@ private void CheckDisposed()

private static bool IsWebSocketsSupported()
{
#if NETSTANDARD2_1
#if NETSTANDARD2_1 || NETCOREAPP
// .NET Core 2.1 and above has a managed implementation
return true;
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Net;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Connections;
Expand Down Expand Up @@ -86,23 +87,22 @@ internal static HttpConnectionOptions ShallowCopyHttpConnectionOptions(HttpConne
{
HttpMessageHandlerFactory = options.HttpMessageHandlerFactory,
Headers = options.Headers,
Cookies = options.Cookies,
Url = options.Url,
Transports = options.Transports,
SkipNegotiation = options.SkipNegotiation,
AccessTokenProvider = options.AccessTokenProvider,
CloseTimeout = options.CloseTimeout,
Credentials = options.Credentials,
Proxy = options.Proxy,
UseDefaultCredentials = options.UseDefaultCredentials,
DefaultTransferFormat = options.DefaultTransferFormat,
WebSocketConfiguration = options.WebSocketConfiguration,
};

// WASM doesn't support Crypto APIs and our setter throws if you try to assign null
if (options.ClientCertificates != null)
if (!OperatingSystem.IsBrowser())
{
newOptions.Cookies = options.Cookies;
newOptions.ClientCertificates = options.ClientCertificates;
newOptions.Credentials = options.Credentials;
newOptions.Proxy = options.Proxy;
newOptions.UseDefaultCredentials = options.UseDefaultCredentials;
newOptions.WebSocketConfiguration = options.WebSocketConfiguration;
}

return newOptions;
Expand Down
Loading