Skip to content

Commit

Permalink
Required changes for 3.0.0 preview3 (#112)
Browse files Browse the repository at this point in the history
* Required changes for 3.0.0 preview3

* Add scripts to install dotnet SDK
  • Loading branch information
John Luo committed Feb 21, 2019
1 parent f29c274 commit c7ca9ce
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 23 deletions.
6 changes: 2 additions & 4 deletions .travis.yml
Expand Up @@ -9,9 +9,7 @@ branches:
only: only:
- master - master
install: install:
- curl -o dotnet-sdk.tar.gz -sSL https://download.visualstudio.microsoft.com/download/pr/efa6dde9-a5ee-4322-b13c-a2a02d3980f0/dad445eba341c1d806bae5c8afb47015/dotnet-sdk-3.0.100-preview-010184-linux-x64.tar.gz - ./build/get-dotnet.sh
- mkdir -p $PWD/dotnet - export PATH="~/.dotnet/:$PATH"
- tar zxf dotnet-sdk.tar.gz -C $PWD/dotnet
- export PATH="$PWD/dotnet:$PATH"
script: script:
- ./build_and_test.sh - ./build_and_test.sh
8 changes: 6 additions & 2 deletions README.md
Expand Up @@ -27,11 +27,15 @@ Documentation and guides are coming soon! In the mean time we suggest referring


## To develop gRPC for ASP.NET Core ## To develop gRPC for ASP.NET Core


Install [.NET Core SDK 3 preview 2](https://dotnet.microsoft.com/download/dotnet-core/3.0). Installing .NET Core SDK:
```
# Run this script before building the project.
./build/get-dotnet.sh or ./build/get-dotnet.ps1
```


Setting up local feed with unreleased Grpc.* packages: Setting up local feed with unreleased Grpc.* packages:
``` ```
# For the time being, we are depending on unreleased Grpc.* packages. # We may depend on unreleased Grpc.* packages.
# Run this script before building the project. # Run this script before building the project.
./build/get-grpc.sh or ./build/get-grpc.ps1 ./build/get-grpc.sh or ./build/get-grpc.ps1
``` ```
Expand Down
44 changes: 44 additions & 0 deletions build/get-dotnet.ps1
@@ -0,0 +1,44 @@
#!/usr/bin/env powershell

<#
.PARAMETER Upgrade
Upgrade the version of gRPC packages to be downloaded to the latest on https://packages.grpc.io/
.NOTES
This function will create a file grpc-lock.txt. This lock file can be committed to source, but does not have to be.
When the lock file is not present, the script will create one using latest available version from https://packages.grpc.io/.
#>

param (
[switch]$Upgrade = $false
)

Set-StrictMode -Version 2
$ErrorActionPreference = 'Stop'

# Variables

$WorkingDir = $PSScriptRoot
$TempDir = Join-Path $WorkingDir 'obj'
$InstallScriptUrl = 'https://dot.net/v1/dotnet-install.ps1'
$InstallScriptPath = Join-Path $TempDir 'dotnet-install.ps1'
$GlobalJsonPath = Join-Path $WorkingDir '..' | Join-Path -ChildPath 'global.json'

# Functions

function Ensure-Dir([string]$path) {
if (!(Test-Path $path -PathType Container)) {
New-Item -ItemType Directory -Force -Path $path | Out-Null
}
}

# Main

# Resolve SDK version
$GlobalJson = Get-Content -Raw $GlobalJsonPath | ConvertFrom-Json
$SDKVersion = $GlobalJson.sdk.version

# Download install script
Ensure-Dir $TempDir
Write-Host "Downloading install script: $InstallScriptUrl => $InstallScriptPath"
Invoke-WebRequest -Uri $InstallScriptUrl -OutFile $InstallScriptPath
&$InstallScriptPath -Version $SDKVersion
29 changes: 29 additions & 0 deletions build/get-dotnet.sh
@@ -0,0 +1,29 @@
#!/usr/bin/env bash

set -euo pipefail

# variables
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
OBJDIR="$DIR/obj"
global_json_path="$DIR/../global.json"
install_script_url="https://dot.net/v1/dotnet-install.sh"
install_script_path="$OBJDIR/dotnet-install.sh"

# functions
ensure_dir() {
[ -d $1 ] || mkdir $1
}

# main

# resolve SDK version
sdk_version=$(jq -r .sdk.version $global_json_path)

# download dotnet-install.sh
ensure_dir $OBJDIR

echo "Downloading install script: $install_script_url => $install_script_path"
curl -sSL -o $install_script_path $install_script_url
chmod +x $install_script_path

$install_script_path -v $sdk_version
4 changes: 1 addition & 3 deletions examples/Server/Startup.cs
Expand Up @@ -16,9 +16,7 @@


#endregion #endregion


using Grpc.AspNetCore;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;


namespace GRPCServer namespace GRPCServer
Expand All @@ -34,7 +32,7 @@ public void ConfigureServices(IServiceCollection services)
} }


// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env) public void Configure(IApplicationBuilder app)
{ {
app.UseRouting(routes => app.UseRouting(routes =>
{ {
Expand Down
2 changes: 1 addition & 1 deletion global.json
@@ -1,5 +1,5 @@
{ {
"sdk": { "sdk": {
"version": "3.0.100-preview-010184" "version": "3.0.100-preview3-010313"
} }
} }
4 changes: 1 addition & 3 deletions perf/benchmarkapps/BenchmarkServer/Startup.cs
Expand Up @@ -16,9 +16,7 @@


#endregion #endregion


using Grpc.AspNetCore;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;


namespace BenchmarkServer namespace BenchmarkServer
Expand All @@ -33,7 +31,7 @@ public void ConfigureServices(IServiceCollection services)
} }


// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env) public void Configure(IApplicationBuilder app)
{ {
app.UseRouting(routes => app.UseRouting(routes =>
{ {
Expand Down
24 changes: 23 additions & 1 deletion src/Grpc.AspNetCore.Server/Internal/PipeExtensions.cs
Expand Up @@ -55,6 +55,28 @@ public static Task WriteMessageAsync(this PipeWriter pipeWriter, byte[] messageD
return Task.FromException(new RpcException(SendingMessageExceedsLimitStatus)); return Task.FromException(new RpcException(SendingMessageExceedsLimitStatus));
} }


// Must call StartAsync before the first pipeWriter.GetSpan() in WriteHeader
var response = serverCallContext.HttpContext.Response;
if (!response.HasStarted)
{
var startAsyncTask = response.StartAsync();
if (!startAsyncTask.IsCompletedSuccessfully)
{
return pipeWriter.WriteMessageCoreAsyncAwaited(messageData, serverCallContext, flush, startAsyncTask);
}
}

return pipeWriter.WriteMessageCoreAsync(messageData, serverCallContext, flush);
}

private static async Task WriteMessageCoreAsyncAwaited(this PipeWriter pipeWriter, byte[] messageData, HttpContextServerCallContext serverCallContext, bool flush, Task startAsyncTask)
{
await startAsyncTask;
await pipeWriter.WriteMessageCoreAsync(messageData, serverCallContext, flush);
}

private static Task WriteMessageCoreAsync(this PipeWriter pipeWriter, byte[] messageData, HttpContextServerCallContext serverCallContext, bool flush)
{
WriteHeader(pipeWriter, messageData.Length); WriteHeader(pipeWriter, messageData.Length);
pipeWriter.Write(messageData); pipeWriter.Write(messageData);


Expand All @@ -77,7 +99,7 @@ public static Task WriteMessageAsync(this PipeWriter pipeWriter, byte[] messageD


private static void WriteHeader(PipeWriter pipeWriter, int length) private static void WriteHeader(PipeWriter pipeWriter, int length)
{ {
Span<byte> headerData = pipeWriter.GetSpan(HeaderSize); var headerData = pipeWriter.GetSpan(HeaderSize);
// Messages are currently always uncompressed // Messages are currently always uncompressed
headerData[0] = 0; headerData[0] = 0;
EncodeMessageLength(length, headerData.Slice(1)); EncodeMessageLength(length, headerData.Slice(1));
Expand Down
1 change: 1 addition & 0 deletions test/FunctionalTests/Infrastructure/GrpcTestFixture.cs
Expand Up @@ -20,6 +20,7 @@
using System.Net.Http; using System.Net.Http;
using FunctionalTestsWebsite.Infrastructure; using FunctionalTestsWebsite.Infrastructure;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.TestHost; using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;


Expand Down
Expand Up @@ -78,11 +78,11 @@ public void MapGrpcService_CanBind_CreatesEndpoints()


var routeEndpoint1 = (RouteEndpoint)endpoints[0]; var routeEndpoint1 = (RouteEndpoint)endpoints[0];
Assert.AreEqual("/Greet.Greeter/SayHello", routeEndpoint1.RoutePattern.RawText); Assert.AreEqual("/Greet.Greeter/SayHello", routeEndpoint1.RoutePattern.RawText);
Assert.AreEqual("POST", ((IHttpMethodMetadata)routeEndpoint1.Metadata.Single()).HttpMethods.Single()); Assert.AreEqual("POST", routeEndpoint1.Metadata.GetMetadata<IHttpMethodMetadata>().HttpMethods.Single());


var routeEndpoint2 = (RouteEndpoint)endpoints[1]; var routeEndpoint2 = (RouteEndpoint)endpoints[1];
Assert.AreEqual("/Greet.Greeter/SayHellos", routeEndpoint2.RoutePattern.RawText); Assert.AreEqual("/Greet.Greeter/SayHellos", routeEndpoint2.RoutePattern.RawText);
Assert.AreEqual("POST", ((IHttpMethodMetadata)routeEndpoint2.Metadata.Single()).HttpMethods.Single()); Assert.AreEqual("POST", routeEndpoint2.Metadata.GetMetadata<IHttpMethodMetadata>().HttpMethods.Single());
} }


[Test] [Test]
Expand Down
@@ -0,0 +1,32 @@
#region Copyright notice and license

// Copyright 2019 The gRPC Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#endregion

using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http.Features;

namespace FunctionalTestsWebsite.Infrastructure
{
internal class TestHttpResponseStartFeature : IHttpResponseStartFeature
{
public Task StartAsync(CancellationToken token = default(CancellationToken))
{
return Task.CompletedTask;
}
}
}
2 changes: 2 additions & 0 deletions testassets/FunctionalTestsWebsite/Startup.cs
Expand Up @@ -67,6 +67,8 @@ public void Configure(IApplicationBuilder app)
// Workaround for https://github.com/aspnet/AspNetCore/issues/7449 // Workaround for https://github.com/aspnet/AspNetCore/issues/7449
context.Features.Set<IHttpRequestLifetimeFeature>(new TestHttpRequestLifetimeFeature()); context.Features.Set<IHttpRequestLifetimeFeature>(new TestHttpRequestLifetimeFeature());
// Workaround for https://github.com/aspnet/AspNetCore/issues/7780
context.Features.Set<IHttpResponseStartFeature>(new TestHttpResponseStartFeature());
return next(); return next();
}); });
Expand Down
8 changes: 1 addition & 7 deletions testassets/InteropTestsWebsite/Startup.cs
Expand Up @@ -16,15 +16,9 @@


#endregion #endregion


using Grpc.AspNetCore;
using Grpc.Testing; using Grpc.Testing;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;


namespace InteropTestsWebsite namespace InteropTestsWebsite
{ {
Expand All @@ -38,7 +32,7 @@ public void ConfigureServices(IServiceCollection services)
} }


// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env) public void Configure(IApplicationBuilder app)
{ {
app.UseRouting(builder => app.UseRouting(builder =>
{ {
Expand Down

0 comments on commit c7ca9ce

Please sign in to comment.