Skip to content

Commit

Permalink
Multi-target .NET 5
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK committed Jul 20, 2020
1 parent 6b65f9b commit a3295c1
Show file tree
Hide file tree
Showing 92 changed files with 231 additions and 210 deletions.
3 changes: 3 additions & 0 deletions build/get-dotnet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ echo "Downloading install script: $install_script_url => $install_script_path"
curl -sSL -o $install_script_path $install_script_url
chmod +x $install_script_path

# Require .NET Core 3.x SDK to run 3.x test targets
$install_script_path -v 3.1.300 -i $dotnet_install_path

$install_script_path -v $sdk_version -i $dotnet_install_path
23 changes: 4 additions & 19 deletions build_and_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ example_solutions=( $( ls examples/**/*.sln ) )

for example_solution in "${example_solutions[@]}"
do
# dotnet build uses msbuild, and attempts to speed consecutive builds by reusing processes.
# This can become a problem when multiple versions of Grpc.Tools are used between builds.
# The different versions will conflict. Shutdown build processes between builds to avoid conflicts.
# Will be fixed in msbuild 16.5 - https://github.com/microsoft/msbuild/issues/1754
dotnet build-server shutdown

dotnet build $example_solution -c Release
done

Expand All @@ -42,20 +36,11 @@ test_projects=( $( ls test/**/*Tests.csproj ) )

for test_project in "${test_projects[@]}"
do
# "dotnet test" is hanging when it writes to console for an unknown reason
# Tracking issue at https://github.com/microsoft/vstest/issues/2080
# Write test output to a text file and then write the text file to console as a workaround
{
dotnet test $test_project -c Release -v n --no-build &> ${test_project##*/}.log.txt &&
echo "Success" &&
cat ${test_project##*/}.log.txt
} || {
echo "Failure" &&
cat ${test_project##*/}.log.txt &&
exit 1
}
# https://github.com/microsoft/vstest/issues/2080#issuecomment-539879345
dotnet test $test_project -c Release -v n --no-build < /dev/null
done

echo "Tests finished"

source grpcweb_interoptests.sh
# Temporarily disable while using nightly .NET SDK build
# source grpcweb_interoptests.sh
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sdk": {
"version": "3.1.300"
"version": "5.0.100-rc.1.20367.2"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<IsGrpcPublishedPackage>true</IsGrpcPublishedPackage>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFrameworks>netcoreapp3.0;net5.0</TargetFrameworks>

<!-- Disable analysis for ConfigureAwait(false) -->
<WarningsNotAsErrors>$(WarningsNotAsErrors);CA2007</WarningsNotAsErrors>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<IsGrpcPublishedPackage>true</IsGrpcPublishedPackage>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFrameworks>netcoreapp3.0;net5.0</TargetFrameworks>

<!-- Disable analysis for ConfigureAwait(false) -->
<WarningsNotAsErrors>$(WarningsNotAsErrors);CA2007</WarningsNotAsErrors>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<IsGrpcPublishedPackage>true</IsGrpcPublishedPackage>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFrameworks>netcoreapp3.0;net5.0</TargetFrameworks>

<!-- Disable analysis for ConfigureAwait(false) -->
<WarningsNotAsErrors>$(WarningsNotAsErrors);CA2007</WarningsNotAsErrors>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static IServiceCollection AddGrpcReflection(this IServiceCollection servi
var grpcEndpointMetadata = endpointDataSource.Endpoints
.Select(ep => ep.Metadata.GetMetadata<GrpcMethodMetadata>())
.Where(m => m != null)
.OfType<GrpcMethodMetadata>()
.ToList();
var serviceTypes = grpcEndpointMetadata.Select(m => m.ServiceType).Distinct().ToList();
Expand Down
2 changes: 1 addition & 1 deletion src/Grpc.AspNetCore.Server/Grpc.AspNetCore.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<IsGrpcPublishedPackage>true</IsGrpcPublishedPackage>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFrameworks>netcoreapp3.0;net5.0</TargetFrameworks>

<!-- Disable analysis for ConfigureAwait(false) -->
<WarningsNotAsErrors>$(WarningsNotAsErrors);CA2007</WarningsNotAsErrors>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ internal DefaultDeserializationContext DeserializationContext

internal bool HasResponseTrailers => _responseTrailers != null;

protected override string MethodCore => HttpContext.Request.Path.Value;
protected override string MethodCore => HttpContext.Request.Path.Value!;

protected override string HostCore => HttpContext.Request.Host.Value;

Expand Down Expand Up @@ -313,7 +313,7 @@ protected override AuthContext AuthContextCore

public ServerCallContext ServerCallContext => this;

protected override IDictionary<object, object> UserStateCore => HttpContext.Items;
protected override IDictionary<object, object> UserStateCore => HttpContext.Items!;

internal bool HasBufferedMessage { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public RequestDelegate CreateUnimplementedMethod()
var unimplementedMethod = httpContext.Request.RouteValues["unimplementedMethod"]?.ToString() ?? "<unknown>";
Log.MethodUnimplemented(logger, unimplementedMethod);
GrpcEventSource.Log.CallUnimplemented(httpContext.Request.Path.Value);
GrpcEventSource.Log.CallUnimplemented(httpContext.Request.Path.Value!);
GrpcProtocolHelpers.SetStatus(GrpcProtocolHelpers.GetTrailersDestination(httpContext.Response), new Status(StatusCode.Unimplemented, "Method is unimplemented."));
return Task.CompletedTask;
Expand Down Expand Up @@ -142,7 +142,7 @@ public RequestDelegate CreateUnimplementedService()
var unimplementedService = httpContext.Request.RouteValues["unimplementedService"]?.ToString() ?? "<unknown>";
Log.ServiceUnimplemented(logger, unimplementedService);
GrpcEventSource.Log.CallUnimplemented(httpContext.Request.Path.Value);
GrpcEventSource.Log.CallUnimplemented(httpContext.Request.Path.Value!);
GrpcProtocolHelpers.SetStatus(GrpcProtocolHelpers.GetTrailersDestination(httpContext.Response), new Status(StatusCode.Unimplemented, "Service is unimplemented."));
return Task.CompletedTask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static string[] GetDnsFromExtensions(X509Certificate2 cert)
foreach (X509Extension ext in cert.Extensions)
{
// Extension is SAN2
if (ext.Oid.Value == X509SubjectAlternativeNameConstants.Oid)
if (ext.Oid?.Value == X509SubjectAlternativeNameConstants.Oid)
{
string asnString = ext.Format(false);
if (string.IsNullOrWhiteSpace(asnString))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ internal static void CreateUnimplementedEndpoints(

private static IEndpointConventionBuilder CreateUnimplementedEndpoint(IEndpointRouteBuilder endpointRouteBuilder, string pattern, string displayName, RequestDelegate requestDelegate)
{
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
// https://github.com/dotnet/aspnetcore/issues/24042
var routePattern = RoutePatternFactory.Parse(pattern, defaults: null, new { contentType = GrpcUnimplementedConstraint.Instance });
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
var endpointBuilder = endpointRouteBuilder.Map(routePattern, requestDelegate);

endpointBuilder.Add(ep =>
Expand All @@ -154,7 +157,7 @@ private class GrpcUnimplementedConstraint : IRouteConstraint
{
public static readonly GrpcUnimplementedConstraint Instance = new GrpcUnimplementedConstraint();

public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection)
public bool Match(HttpContext? httpContext, IRouter route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection)
{
if (httpContext == null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Grpc.AspNetCore/Grpc.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<PackageTags>gRPC RPC HTTP/2 aspnetcore</PackageTags>

<IsGrpcPublishedPackage>true</IsGrpcPublishedPackage>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFrameworks>netcoreapp3.0;net5.0</TargetFrameworks>

<!-- Don't include empty DLL or PDB in metapackage -->
<IncludeBuildOutput>false</IncludeBuildOutput>
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion src/Grpc.Net.Client.Web/Grpc.Net.Client.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<IsGrpcPublishedPackage>true</IsGrpcPublishedPackage>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TargetFramework>netstandard2.1</TargetFramework>
<TargetFrameworks>netstandard2.1;net5.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Grpc.Net.Client.Web/GrpcWebHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage reques

private async Task<HttpResponseMessage> SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
{
request.Content = new GrpcWebRequestContent(request.Content, GrpcWebMode);
request.Content = new GrpcWebRequestContent(request.Content!, GrpcWebMode);

// Set WebAssemblyEnableStreamingResponse to true on gRPC-Web request.
// https://github.com/mono/mono/blob/a0d69a4e876834412ba676f544d447ec331e7c01/sdks/wasm/framework/src/System.Net.Http.WebAssemblyHttpHandler/WebAssemblyHttpHandler.cs#L149
Expand Down
14 changes: 10 additions & 4 deletions src/Grpc.Net.Client.Web/Internal/Base64RequestStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,13 @@ public override async ValueTask WriteAsync(ReadOnlyMemory<byte> data, Cancellati

EnsureSuccess(
Base64.EncodeToUtf8(data.Span.Slice(0, encodeLength), localBuffer.Span, out var bytesConsumed, out var bytesWritten, isFinalBlock: false),
OperationStatus.NeedMoreData);
#if NETSTANDARD2_1
OperationStatus.NeedMoreData
#else
// React to fix https://github.com/dotnet/runtime/pull/281
encodeLength == bytesConsumed ? OperationStatus.Done : OperationStatus.NeedMoreData
#endif
);

var base64Remainder = _buffer.Length - localBuffer.Length;
await _inner.WriteAsync(_buffer.AsMemory(0, bytesWritten + base64Remainder), cancellationToken).ConfigureAwait(false);
Expand Down Expand Up @@ -113,7 +119,7 @@ private static void EnsureSuccess(OperationStatus status, OperationStatus expect
{
if (status != expectedStatus)
{
throw new InvalidOperationException("Error encoding content to base64: " + status);
throw new InvalidOperationException($"Error encoding content to base64. Expected status: {expectedStatus}, actual status: {status}.");
}
}

Expand Down Expand Up @@ -146,7 +152,7 @@ protected override void Dispose(bool disposing)
base.Dispose(disposing);
}

#region Stream implementation
#region Stream implementation
public override bool CanRead => _inner.CanRead;
public override bool CanSeek => _inner.CanSeek;
public override bool CanWrite => _inner.CanWrite;
Expand Down Expand Up @@ -183,6 +189,6 @@ public override void Write(byte[] buffer, int offset, int count)
WriteAsync(buffer.AsMemory(0, count)).AsTask().GetAwaiter().GetResult();
FlushAsync().GetAwaiter().GetResult();
}
#endregion
#endregion
}
}
2 changes: 1 addition & 1 deletion src/Grpc.Net.Client.Web/Internal/GrpcWebRequestContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public GrpcWebRequestContent(HttpContent inner, GrpcWebMode mode)
: GrpcWebProtocolConstants.GrpcWebHeader;
}

protected override Task SerializeToStreamAsync(Stream stream, TransportContext context) =>
protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context) =>
_mode == GrpcWebMode.GrpcWebText
? SerializeTextToStreamAsync(stream)
: _inner.CopyToAsync(stream);
Expand Down
2 changes: 1 addition & 1 deletion src/Grpc.Net.Client.Web/Internal/GrpcWebResponseContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public GrpcWebResponseContent(HttpContent inner, GrpcWebMode mode, HttpHeaders r
Headers.ContentType = GrpcWebProtocolConstants.GrpcHeader;
}

protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context)
protected override async Task SerializeToStreamAsync(Stream stream, TransportContext? context)
{
// This method will only be called by tests when response content is
// accessed via ReadAsBytesAsync. The gRPC client will always
Expand Down
2 changes: 1 addition & 1 deletion src/Grpc.Net.Client/Grpc.Net.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<IsGrpcPublishedPackage>true</IsGrpcPublishedPackage>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TargetFramework>netstandard2.1</TargetFramework>
<TargetFrameworks>netstandard2.1;net5.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down
13 changes: 12 additions & 1 deletion src/Grpc.Net.Client/GrpcChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,18 @@ private static HttpMessageInvoker CreateInternalHttpInvoker(HttpMessageHandler?
{
// HttpMessageInvoker should always dispose handler if Disposed is called on it.
// Decision to dispose invoker is controlled by _shouldDisposeHttpClient.
var httpInvoker = new HttpMessageInvoker(handler ?? new HttpClientHandler(), disposeHandler: true);
if (handler == null)
{
#if NET5_0
// TODO(JamesNK): Wrap sockets handler with a handler that sets traceparent header
handler = new SocketsHttpHandler();
#else
handler = new HttpClientHandler();
#endif
}


var httpInvoker = new HttpMessageInvoker(handler, disposeHandler: true);

return httpInvoker;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Grpc.Net.Client/Internal/GrpcCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ public Exception CreateFailureStatusException(Status status)

private (bool diagnosticSourceEnabled, Activity? activity) InitializeCall(HttpRequestMessage request, TimeSpan? timeout)
{
GrpcCallLog.StartingCall(Logger, Method.Type, request.RequestUri);
GrpcCallLog.StartingCall(Logger, Method.Type, request.RequestUri!);
GrpcEventSource.Log.CallStart(Method.FullName);

// Deadline will cancel the call CTS.
Expand Down Expand Up @@ -862,7 +862,7 @@ private long GetTimerDueTime(TimeSpan timeout)
return timeout;
}

private void DeadlineExceededCallback(object state)
private void DeadlineExceededCallback(object? state)
{
// Deadline is only exceeded if the timeout has passed and
// the response has not been finished or canceled
Expand Down
2 changes: 1 addition & 1 deletion src/Grpc.Net.Client/Internal/PushStreamContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public PushStreamContent(HttpContentClientStreamWriter<TRequest, TResponse> stre
_streamWriter = streamWriter;
}

protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context)
protected override async Task SerializeToStreamAsync(Stream stream, TransportContext? context)
{
// Immediately flush request stream to send headers
// https://github.com/dotnet/corefx/issues/39586#issuecomment-516210081
Expand Down
2 changes: 1 addition & 1 deletion src/Grpc.Net.Client/Internal/PushUnaryContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public PushUnaryContent(TRequest content, GrpcCall<TRequest, TResponse> call, st
Headers.ContentType = mediaType;
}

protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context)
{
var writeMessageTask = _call.WriteMessageAsync(stream, _content, _grpcEncoding, _call.Options);
if (writeMessageTask.IsCompletedSuccessfully)
Expand Down
2 changes: 1 addition & 1 deletion src/Grpc.Net.ClientFactory/Grpc.Net.ClientFactory.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<IsGrpcPublishedPackage>true</IsGrpcPublishedPackage>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TargetFramework>netstandard2.1</TargetFramework>
<TargetFrameworks>netstandard2.1;net5.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down
10 changes: 5 additions & 5 deletions src/Grpc.Net.ClientFactory/Internal/TypeNameHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private static void ProcessType(StringBuilder builder, Type type, in DisplayName
}
else
{
var name = options.FullName ? type.FullName : type.Name;
var name = options.FullName ? type.FullName! : type.Name;
builder.Append(name);

if (options.NestedTypeDelimiter != DefaultNestedTypeDelimiter)
Expand All @@ -107,7 +107,7 @@ private static void ProcessArrayType(StringBuilder builder, Type type, in Displa
var innerType = type;
while (innerType.IsArray)
{
innerType = innerType.GetElementType();
innerType = innerType.GetElementType()!;
}

ProcessType(builder, innerType, options);
Expand All @@ -117,7 +117,7 @@ private static void ProcessArrayType(StringBuilder builder, Type type, in Displa
builder.Append('[');
builder.Append(',', type.GetArrayRank() - 1);
builder.Append(']');
type = type.GetElementType();
type = type.GetElementType()!;
}
}

Expand All @@ -126,14 +126,14 @@ private static void ProcessGenericType(StringBuilder builder, Type type, Type[]
var offset = 0;
if (type.IsNested)
{
offset = type.DeclaringType.GetGenericArguments().Length;
offset = type.DeclaringType!.GetGenericArguments().Length;
}

if (options.FullName)
{
if (type.IsNested)
{
ProcessGenericType(builder, type.DeclaringType, genericArguments, offset, options);
ProcessGenericType(builder, type.DeclaringType!, genericArguments, offset, options);
builder.Append(options.NestedTypeDelimiter);
}
else if (!string.IsNullOrEmpty(type.Namespace))
Expand Down
2 changes: 1 addition & 1 deletion src/Grpc.Net.Common/Grpc.Net.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<IsGrpcPublishedPackage>true</IsGrpcPublishedPackage>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TargetFramework>netstandard2.1</TargetFramework>
<TargetFrameworks>netstandard2.1;net5.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit a3295c1

Please sign in to comment.