Skip to content

Commit 6c8cff2

Browse files
authored
Merge pull request #17840 from dotnet/master
Update live with master
2 parents 0908d08 + 9a66c86 commit 6c8cff2

4 files changed

Lines changed: 92 additions & 8 deletions

File tree

aspnetcore/grpc/browser.md

Lines changed: 11 additions & 2 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: 02/16/2020
7+
ms.date: 04/15/2020
88
uid: grpc/browser
99
---
1010
# Use gRPC in browser apps
@@ -23,6 +23,15 @@ By [James Newton-King](https://twitter.com/jamesnk)
2323
2424
It is not possible to call a HTTP/2 gRPC service from a browser-based app. [gRPC-Web](https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-WEB.md) is a protocol that allows browser JavaScript and Blazor apps to call gRPC services. This article explains how to use gRPC-Web in .NET Core.
2525

26+
## gRPC-Web in ASP.NET Core vs. Envoy
27+
28+
There are two choices for how to add gRPC-Web to an ASP.NET Core app:
29+
30+
* Support gRPC-Web alongside gRPC HTTP/2 in ASP.NET Core. This option uses middleware provided by the `Grpc.AspNetCore.Web` package.
31+
* Use the [Envoy proxy's](https://www.envoyproxy.io/) gRPC-Web support to translate gRPC-Web to gRPC HTTP/2. The translated call is then forwarded onto the ASP.NET Core app.
32+
33+
There are pros and cons to each approach. If you're already using Envoy as a proxy in your app's environment, it might make sense to also use it to provide gRPC-Web support. If you want a simple solution for gRPC-Web that only requires ASP.NET Core, `Grpc.AspNetCore.Web` is a good choice.
34+
2635
## Configure gRPC-Web in ASP.NET Core
2736

2837
gRPC services hosted in ASP.NET Core can be configured to support gRPC-Web alongside HTTP/2 gRPC. gRPC-Web does not require any changes to services. The only modification is startup configuration.
@@ -94,7 +103,7 @@ The preceding code:
94103
The `GrpcWebHandler` has the following configuration options when created:
95104

96105
* **InnerHandler**: The underlying <xref:System.Net.Http.HttpMessageHandler> that makes the gRPC HTTP request, for example, `HttpClientHandler`.
97-
* **Mode**: An enumeration type that specifies whether the gRPC HTTP request request `Content-Type` is `application/grpc-web` or `application/grpc-web-text`.
106+
* **Mode**: An enumeration type that specifies whether the gRPC HTTP request `Content-Type` is `application/grpc-web` or `application/grpc-web-text`.
98107
* `GrpcWebMode.GrpcWeb` configures content to be sent without encoding. Default value.
99108
* `GrpcWebMode.GrpcWebText` configures content to be base64 encoded. Required for server streaming calls in browsers.
100109
* **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/configuration.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ gRPC services are configured with `AddGrpc` in *Startup.cs*. The following table
1616

1717
| Option | Default Value | Description |
1818
| ------ | ------------- | ----------- |
19-
| MaxSendMessageSize | `null` | The maximum message size in bytes that can be sent from the server. Attempting to send a message that exceeds the configured maximum message size results in an exception. |
20-
| MaxReceiveMessageSize | 4 MB | The maximum message size in bytes that can be received by the server. If the server receives a message that exceeds this limit, it throws an exception. Increasing this value allows the server to receive larger messages, but can negatively impact memory consumption. |
19+
| MaxSendMessageSize | `null` | The maximum message size in bytes that can be sent from the server. Attempting to send a message that exceeds the configured maximum message size results in an exception. When set to `null`, the message size is unlimited. |
20+
| MaxReceiveMessageSize | 4 MB | The maximum message size in bytes that can be received by the server. If the server receives a message that exceeds this limit, it throws an exception. Increasing this value allows the server to receive larger messages, but can negatively impact memory consumption. When set to `null`, the message size is unlimited. |
2121
| EnableDetailedErrors | `false` | If `true`, detailed exception messages are returned to clients when an exception is thrown in a service method. The default is `false`. Setting `EnableDetailedErrors` to `true` can leak sensitive information. |
2222
| CompressionProviders | gzip | A collection of compression providers used to compress and decompress messages. Custom compression providers can be created and added to the collection. The default configured providers support **gzip** compression. |
2323
| <span style="word-break:normal;word-wrap:normal">ResponseCompressionAlgorithm</span> | `null` | The compression algorithm used to compress messages sent from the server. The algorithm must match a compression provider in `CompressionProviders`. For the algorithm to compress a response, the client must indicate it supports the algorithm by sending it in the **grpc-accept-encoding** header. |
@@ -41,8 +41,8 @@ gRPC client configuration is set on `GrpcChannelOptions`. The following table de
4141
| HttpClient | New instance | The `HttpClient` used to make gRPC calls. A client can be set to configure a custom `HttpClientHandler`, or add additional handlers to the HTTP pipeline for gRPC calls. If no `HttpClient` is specified, then a new `HttpClient` instance is created for the channel. It will automatically be disposed. |
4242
| DisposeHttpClient | `false` | If `true`, and an `HttpClient` is specified, then the `HttpClient` instance will be disposed when the `GrpcChannel` is disposed. |
4343
| LoggerFactory | `null` | The `LoggerFactory` used by the client to log information about gRPC calls. A `LoggerFactory` instance can be resolved from dependency injection or created using `LoggerFactory.Create`. For examples of configuring logging, see <xref:grpc/diagnostics#grpc-client-logging>. |
44-
| MaxSendMessageSize | `null` | The maximum message size in bytes that can be sent from the client. Attempting to send a message that exceeds the configured maximum message size results in an exception. |
45-
| <span style="word-break:normal;word-wrap:normal">MaxReceiveMessageSize</span> | 4 MB | The maximum message size in bytes that can be received by the client. If the client receives a message that exceeds this limit, it throws an exception. Increasing this value allows the client to receive larger messages, but can negatively impact memory consumption. |
44+
| MaxSendMessageSize | `null` | The maximum message size in bytes that can be sent from the client. Attempting to send a message that exceeds the configured maximum message size results in an exception. When set to `null`, the message size is unlimited. |
45+
| <span style="word-break:normal;word-wrap:normal">MaxReceiveMessageSize</span> | 4 MB | The maximum message size in bytes that can be received by the client. If the client receives a message that exceeds this limit, it throws an exception. Increasing this value allows the client to receive larger messages, but can negatively impact memory consumption. When set to `null`, the message size is unlimited. |
4646
| Credentials | `null` | A `ChannelCredentials` instance. Credentials are used to add authentication metadata to gRPC calls. |
4747
| CompressionProviders | gzip | A collection of compression providers used to compress and decompress messages. Custom compression providers can be created and added to the collection. The default configured providers support **gzip** compression. |
4848

aspnetcore/mvc/views/view-compilation.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ author: rick-anderson
44
description: Learn how compilation of Razor files occurs in an ASP.NET Core app.
55
ms.author: riande
66
ms.custom: mvc
7-
ms.date: 04/13/2020
7+
ms.date: 04/14/2020
88
uid: mvc/views/view-compilation
99
---
1010
# Razor file compilation in ASP.NET Core
@@ -77,13 +77,23 @@ In the following example, runtime compilation is enabled in the Development envi
7777

7878
No code changes are needed in the project's `Startup` class. At runtime, ASP.NET Core searches for an [assembly-level HostingStartup attribute](xref:fundamentals/configuration/platform-specific-configuration#hostingstartup-attribute) in `Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation`. The `HostingStartup` attribute specifies the app startup code to execute. That startup code enables runtime compilation.
7979

80+
## Enable runtime compilation for a Razor Class Library
81+
82+
Consider a scenario in which a Razor Pages project references a [Razor Class Library (RCL)](xref:razor-pages/ui-class) named *MyClassLib*. The RCL contains a *_Layout.cshtml* file that all of your team's MVC and Razor Pages projects consume. You want to enable runtime compilation for the *_Layout.cshtml* file in that RCL. Make the following changes in the Razor Pages project:
83+
84+
1. Enable runtime compilation with the instructions at [Conditionally enable runtime compilation in an existing project](#conditionally-enable-runtime-compilation-in-an-existing-project).
85+
1. Configure the runtime compilation options in `Startup.ConfigureServices`:
86+
87+
[!code-csharp[](~/mvc/views/view-compilation/samples/3.1/Startup.cs?name=snippet_ConfigureServices&highlight=5-10)]
88+
89+
In the preceding code, an absolute path to the *MyClassLib* RCL is constructed. The [PhysicalFileProvider API](xref:fundamentals/file-providers#physicalfileprovider) is used to locate directories and files at that absolute path. Finally, the `PhysicalFileProvider` instance is added to a file providers collection, which allows access to the RCL's *.cshtml* files.
90+
8091
## Additional resources
8192

8293
* [RazorCompileOnBuild and RazorCompileOnPublish](xref:razor-pages/sdk#properties) properties.
8394
* <xref:razor-pages/index>
8495
* <xref:mvc/views/overview>
8596
* <xref:razor-pages/sdk>
86-
* See the [runtime compilation sample on GitHub](https://github.com/aspnet/samples/tree/master/samples/aspnetcore/mvc/runtimecompilation) for a sample that shows making runtime compilation work across projects.
8797

8898
::: moniker-end
8999

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System.IO;
2+
using Microsoft.AspNetCore.Builder;
3+
using Microsoft.AspNetCore.Hosting;
4+
using Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation;
5+
using Microsoft.Extensions.Configuration;
6+
using Microsoft.Extensions.DependencyInjection;
7+
using Microsoft.Extensions.FileProviders;
8+
using Microsoft.Extensions.Hosting;
9+
10+
namespace MyApp
11+
{
12+
public class Startup
13+
{
14+
public Startup(IConfiguration configuration, IWebHostEnvironment env)
15+
{
16+
Configuration = configuration;
17+
HostEnvironment = env;
18+
}
19+
20+
public IConfiguration Configuration { get; }
21+
22+
public IWebHostEnvironment HostEnvironment { get; }
23+
24+
#region snippet_ConfigureServices
25+
public void ConfigureServices(IServiceCollection services)
26+
{
27+
services.AddRazorPages();
28+
29+
services.Configure<MvcRazorRuntimeCompilationOptions>(options =>
30+
{
31+
var libraryPath = Path.GetFullPath(
32+
Path.Combine(HostEnvironment.ContentRootPath, "..", "MyClassLib"));
33+
options.FileProviders.Add(new PhysicalFileProvider(libraryPath));
34+
});
35+
}
36+
#endregion
37+
38+
public void Configure(IApplicationBuilder app)
39+
{
40+
if (HostEnvironment.IsDevelopment())
41+
{
42+
app.UseDeveloperExceptionPage();
43+
}
44+
else
45+
{
46+
app.UseExceptionHandler("/Home/Error");
47+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
48+
app.UseHsts();
49+
}
50+
app.UseHttpsRedirection();
51+
app.UseStaticFiles();
52+
53+
app.UseRouting();
54+
55+
app.UseAuthorization();
56+
57+
app.UseEndpoints(endpoints =>
58+
{
59+
endpoints.MapControllerRoute(
60+
name: "default",
61+
pattern: "{controller=Home}/{action=Index}/{id?}");
62+
});
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)