Skip to content

Commit 9a57cca

Browse files
authored
Blazor WASM browser compatibility analyzer (#19904)
1 parent 698769b commit 9a57cca

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

aspnetcore/blazor/components/class-libraries.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,34 @@ An RCL can include static assets. The static assets are available to any app tha
155155

156156
For more information, see <xref:blazor/host-and-deploy/webassembly#static-assets-and-class-libraries>.
157157

158+
::: moniker range=">= aspnetcore-5.0"
159+
160+
## Browser compatibility analyzer for Blazor WebAssembly
161+
162+
Blazor WebAssembly apps target the full .NET API surface area, but not all .NET APIs are supported on WebAssembly due to browser sandbox constraints. Unsupported APIs throw <xref:System.PlatformNotSupportedException> when running on WebAssembly. A platform compatibility analyzer warns the developer when the app uses APIs that aren't supported by the app's target platforms. For Blazor WebAssembly apps, this means checking that APIs are supported in browsers. Annotating .NET framework APIs for the compatibility analyzer is an on-going process, so not all .NET framework API is currently annotated.
163+
164+
Blazor WebAssembly and Razor class library projects *automatically* enable browser compatibilty checks by adding `browser` as a supported platform with the `SupportedPlatform` MSBuild item. Library developers can manually add the `SupportedPlatform` item to a library's project file to enable the feature:
165+
166+
```xml
167+
<ItemGroup>
168+
<SupportedPlatform Include="browser" />
169+
</ItemGroup>
170+
```
171+
172+
When authoring a library, indicate that a particular API isn't supported in browsers by specifying `browser` to <xref:System.Runtime.Versioning.UnsupportedOSPlatformAttribute>:
173+
174+
```csharp
175+
[UnsupportedOSPlatform("browser")]
176+
private static string GetLoggingDirectory()
177+
{
178+
...
179+
}
180+
```
181+
182+
For more information, see [Annotating APIs as unsupported on specific platforms (dotnet/designs GitHub repository](https://github.com/dotnet/designs/blob/main/accepted/2020/platform-exclusion/platform-exclusion.md#build-configuration-for-platforms).
183+
184+
::: moniker-end
185+
158186
## Build, pack, and ship to NuGet
159187

160188
Because component libraries are standard .NET libraries, packaging and shipping them to NuGet is no different from packaging and shipping any library to NuGet. Packaging is performed using the [`dotnet pack`](/dotnet/core/tools/dotnet-pack) command in a command shell:

0 commit comments

Comments
 (0)