.NET Documentation

MSDN Archives - Blazor Deployment

Deploying Blazor Applications

This document provides comprehensive guidance on deploying your Blazor applications. Blazor offers flexibility in deployment targets, allowing you to choose between client-side execution (Blazor WebAssembly) and server-side execution (Blazor Server).

Understanding Blazor Deployment Models

Blazor Server

In Blazor Server, your application's UI runs on the server, and a real-time connection is established using SignalR. UI updates are sent to the client over this connection. This model is suitable for applications requiring access to server-side resources or when reducing client-side download size is a priority.

Key characteristics:

  • UI runs on the server.
  • Requires a persistent SignalR connection.
  • Initial download size is small.
  • Sensitive to network latency.

Blazor WebAssembly

Blazor WebAssembly allows your .NET code to run directly in the browser using WebAssembly. The application is downloaded to the client, and the .NET runtime executes it. This model offers a native application-like experience with no server dependency after the initial download.

Key characteristics:

  • UI runs in the browser.
  • No persistent server connection required after load.
  • Larger initial download size compared to Blazor Server.
  • Suitable for offline applications and rich client experiences.

Deployment Targets

Blazor applications can be deployed to a variety of platforms:

  • IIS: A common choice for Windows environments.
  • Azure App Service: A scalable cloud hosting solution from Microsoft.
  • Docker Containers: Enables consistent deployments across different environments.
  • Other Web Servers: Nginx, Apache, etc., often used with reverse proxies.

Deployment Steps for Blazor Server

Deploying a Blazor Server application typically involves publishing the .NET application and configuring your web server.

Publishing with .NET CLI:

dotnet publish -c Release

Example Configuration (IIS):

Ensure you have the ASP.NET Core Hosting Bundle installed on your IIS server. Configure your site to point to the published output directory.

Note: For Blazor Server, ensure your server infrastructure can handle persistent WebSocket connections.

Deployment Steps for Blazor WebAssembly

Blazor WebAssembly applications are essentially static websites once published. They can be hosted on any static file web server.

Publishing with .NET CLI:

dotnet publish -c Release -o "publish_output"

Hosting Static Files:

The `publish_output` directory will contain all necessary files (HTML, CSS, JS, .NET assemblies). You can deploy this entire folder to any web server capable of serving static content.

Tip: For Blazor WebAssembly, consider configuring Gzip compression on your web server to reduce download times.

Single Page Application (SPA) Configuration:

Most static web servers will require configuration to serve `index.html` for all non-file requests to ensure Blazor's routing works correctly. For example, in Nginx:


location / {
    try_files $uri $uri/ /index.html;
}
                

Hosting Options and Best Practices

Azure App Service

Azure App Service provides a managed environment for hosting web applications. It supports both Blazor Server and Blazor WebAssembly.

  • Blazor Server: Deploy as a standard ASP.NET Core application.
  • Blazor WebAssembly: Deploy the static files to an App Service configured for static sites or use it in conjunction with Azure Static Web Apps.

Docker

Containerizing your Blazor application offers portability and consistency.

Create a `Dockerfile` in your project's root directory:


# Example Dockerfile for a Blazor Server app
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["YourApp.csproj", "./"]
RUN dotnet restore
COPY . .
RUN dotnet publish -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=build /app/publish .
ENTRYPOINT ["dotnet", "YourApp.dll"]
                

Security Considerations

When deploying Blazor applications, consider the following security aspects:

  • Authentication and Authorization: Implement robust security measures for user authentication.
  • HTTPS: Always use HTTPS to encrypt communication.
  • Input Validation: Sanitize and validate all user inputs to prevent injection attacks.
  • Dependency Updates: Keep your .NET SDK and Blazor packages up-to-date to mitigate known vulnerabilities.

Troubleshooting Common Deployment Issues

  • Mixed Content Errors: Ensure your site is served entirely over HTTPS.
  • Runtime Errors in Browser (WASM): Check browser developer console for specific error messages. Often related to missing assemblies or incorrect configuration.
  • Connection Issues (Server): Verify SignalR is configured correctly and firewall rules allow WebSocket traffic.
  • Incorrect Routing: Ensure your web server is configured for SPA routing for Blazor WebAssembly.