Skip to content

Commit 4b20e4e

Browse files
authored
React to 2.29.0 API changes in gRPC-Web (#18373)
1 parent 2aed546 commit 4b20e4e

3 files changed

Lines changed: 9 additions & 11 deletions

File tree

aspnetcore/grpc/browser.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ author: jamesnk
44
description: Learn how to configure gRPC services on ASP.NET Core to be callable from browser apps using gRPC-Web.
55
monikerRange: '>= aspnetcore-3.0'
66
ms.author: jamesnk
7-
ms.date: 04/15/2020
7+
ms.date: 05/19/2020
88
no-loc: [Blazor, "Identity", "Let's Encrypt", Razor, SignalR]
99
uid: grpc/browser
1010
---
@@ -40,7 +40,7 @@ gRPC services hosted in ASP.NET Core can be configured to support gRPC-Web along
4040
To enable gRPC-Web with an ASP.NET Core gRPC service:
4141

4242
* Add a reference to the [Grpc.AspNetCore.Web](https://www.nuget.org/packages/Grpc.AspNetCore.Web) package.
43-
* Configure the app to use gRPC-Web by adding `AddGrpcWeb` and `UseGrpcWeb` to *Startup.cs*:
43+
* Configure the app to use gRPC-Web by adding `UseGrpcWeb` and `EnableGrpcWeb` to *Startup.cs*:
4444

4545
[!code-csharp[](~/grpc/browser/sample/Startup.cs?name=snippet_1&highlight=10,14)]
4646

@@ -49,9 +49,9 @@ The preceding code:
4949
* Adds the gRPC-Web middleware, `UseGrpcWeb`, after routing and before endpoints.
5050
* Specifies the `endpoints.MapGrpcService<GreeterService>()` method supports gRPC-Web with `EnableGrpcWeb`.
5151

52-
Alternatively, configure all services to support gRPC-Web by adding `services.AddGrpcWeb(o => o.GrpcWebEnabled = true);` to ConfigureServices.
52+
Alternatively, the gRPC-Web middleware can be configured so all services support gRPC-Web by default and `EnableGrpcWeb` isn't required. Specify `new GrpcWebOptions { DefaultEnabled = true }` when the middleware is added.
5353

54-
[!code-csharp[](~/grpc/browser/sample/AllServicesSupportExample_Startup.cs?name=snippet_1&highlight=6,13)]
54+
[!code-csharp[](~/grpc/browser/sample/AllServicesSupportExample_Startup.cs?name=snippet_1&highlight=12)]
5555

5656
> [!NOTE]
5757
> There is a known issue that causes gRPC-Web to fail when [hosted by Http.sys](xref:fundamentals/servers/httpsys) in .NET Core 3.x.
@@ -91,7 +91,7 @@ The .NET gRPC client can be configured to make gRPC-Web calls. This is useful fo
9191
To use gRPC-Web:
9292

9393
* Add a reference to the [Grpc.Net.Client.Web](https://www.nuget.org/packages/Grpc.Net.Client.Web) package.
94-
* Ensure the reference to [Grpc.Net.Client](https://www.nuget.org/packages/Grpc.Net.Client) package is 2.27.0 or greater.
94+
* Ensure the reference to [Grpc.Net.Client](https://www.nuget.org/packages/Grpc.Net.Client) package is 2.29.0 or greater.
9595
* Configure the channel to use the `GrpcWebHandler`:
9696

9797
[!code-csharp[](~/grpc/browser/sample/Handler.cs?name=snippet_1)]
@@ -101,10 +101,10 @@ The preceding code:
101101
* Configures a channel to use gRPC-Web.
102102
* Creates a client and makes a call using the channel.
103103

104-
The `GrpcWebHandler` has the following configuration options when created:
104+
`GrpcWebHandler` has the following configuration options:
105105

106106
* **InnerHandler**: The underlying <xref:System.Net.Http.HttpMessageHandler> that makes the gRPC HTTP request, for example, `HttpClientHandler`.
107-
* **Mode**: An enumeration type that specifies whether the gRPC HTTP request `Content-Type` is `application/grpc-web` or `application/grpc-web-text`.
107+
* **GrpcWebMode**: An enumeration type that specifies whether the gRPC HTTP request `Content-Type` is `application/grpc-web` or `application/grpc-web-text`.
108108
* `GrpcWebMode.GrpcWeb` configures content to be sent without encoding. Default value.
109109
* `GrpcWebMode.GrpcWebText` configures content to be base64 encoded. Required for server streaming calls in browsers.
110110
* **HttpVersion**: HTTP protocol `Version` used to set [HttpRequestMessage.Version](xref:System.Net.Http.HttpRequestMessage.Version) on the underlying gRPC HTTP request. gRPC-Web doesn't require a specific version and doesn't override the default unless specified.

aspnetcore/grpc/browser/sample/AllServicesSupportExample_Startup.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,13 @@ public class Startup
2424
public void ConfigureServices(IServiceCollection services)
2525
{
2626
services.AddGrpc();
27-
services.AddGrpcWeb(o => o.GrpcWebEnabled = true);
2827
}
2928

3029
public void Configure(IApplicationBuilder app)
3130
{
3231
app.UseRouting();
3332

34-
app.UseGrpcWeb(); // Must be added between UseRouting and UseEndpoints
33+
app.UseGrpcWeb(new GrpcWebOptions { DefaultEnabled = true });
3534

3635
app.UseEndpoints(endpoints =>
3736
{

aspnetcore/grpc/browser/sample/Handler.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#region snippet_1
2-
var handler = new GrpcWebHandler(GrpcWebMode.GrpcWebText, new HttpClientHandler());
32
var channel = GrpcChannel.ForAddress("https://localhost:5001", new GrpcChannelOptions
43
{
5-
HttpClient = new HttpClient(handler)
4+
HttpHandler = new GrpcWebHandler(new HttpClientHandler())
65
});
76

87
var client = new Greeter.GreeterClient(channel);

0 commit comments

Comments
 (0)