Mihaimyh
Mihaimyh

Reputation: 1420

Blazor on docker containers

Anyone managed to run a server-side hosted Blazor application on a docker container?

I am getting the following exception when I run it:

warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
      No XML encryptor configured. Key {c1fe0008-8b2b-46a3-9f80-233ae59e5d17} may be persisted to storage in unencrypted form.
crit: Microsoft.AspNetCore.Hosting.Internal.GenericWebHostService[6]
      Application startup exception
System.InvalidOperationException: Could not load the embedded file manifest 'Microsoft.Extensions.FileProviders.Embedded.Manifest.xml' for assembly 'Microsoft.AspNetCore.Components.Server'.
   at Microsoft.Extensions.FileProviders.Embedded.Manifest.ManifestParser.Parse(Assembly assembly, String name)
   at Microsoft.Extensions.FileProviders.ManifestEmbeddedFileProvider..ctor(Assembly assembly)
   at Microsoft.AspNetCore.Components.Server.ConfigureStaticFilesOptions.PostConfigure(String name, StaticFileOptions options)
   at Microsoft.Extensions.Options.OptionsFactory`1.Create(String name)
   at Microsoft.Extensions.Options.OptionsManager`1.<>c__DisplayClass5_0.<Get>b__0()
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.get_Value()
   at Microsoft.Extensions.Options.OptionsCache`1.GetOrAdd(String name, Func`1 createOptions)
   at Microsoft.Extensions.Options.OptionsManager`1.Get(String name)
   at Microsoft.Extensions.Options.OptionsManager`1.get_Value()
   at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware..ctor(RequestDelegate next, IWebHostEnvironment hostingEnv, IOptions`1 options, ILoggerFactory loggerFactory)
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.Extensions.Internal.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
   at Microsoft.Extensions.Internal.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
   at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.<>c__DisplayClass4_0.<UseMiddleware>b__0(RequestDelegate next)
   at Microsoft.AspNetCore.Builder.Internal.ApplicationBuilder.Build()
   at Microsoft.AspNetCore.Hosting.Internal.GenericWebHostService.StartAsync(CancellationToken cancellationToken)

Unhandled Exception: System.InvalidOperationException: Could not load the embedded file manifest 'Microsoft.Extensions.FileProviders.Embedded.Manifest.xml' for assembly 'Microsoft.AspNetCore.Components.Server'.
   at Microsoft.Extensions.FileProviders.Embedded.Manifest.ManifestParser.Parse(Assembly assembly, String name)
   at Microsoft.Extensions.FileProviders.ManifestEmbeddedFileProvider..ctor(Assembly assembly)
   at Microsoft.AspNetCore.Components.Server.ConfigureStaticFilesOptions.PostConfigure(String name, StaticFileOptions options)
   at Microsoft.Extensions.Options.OptionsFactory`1.Create(String name)
   at Microsoft.Extensions.Options.OptionsManager`1.<>c__DisplayClass5_0.<Get>b__0()
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.get_Value()
   at Microsoft.Extensions.Options.OptionsCache`1.GetOrAdd(String name, Func`1 createOptions)
   at Microsoft.Extensions.Options.OptionsManager`1.Get(String name)
   at Microsoft.Extensions.Options.OptionsManager`1.get_Value()
   at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware..ctor(RequestDelegate next, IWebHostEnvironment hostingEnv, IOptions`1 options, ILoggerFactory loggerFactory)
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.Extensions.Internal.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
   at Microsoft.Extensions.Internal.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
   at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.<>c__DisplayClass4_0.<UseMiddleware>b__0(RequestDelegate next)
   at Microsoft.AspNetCore.Builder.Internal.ApplicationBuilder.Build()
   at Microsoft.AspNetCore.Hosting.Internal.GenericWebHostService.StartAsync(CancellationToken cancellationToken)
   at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host)
   at BlazorUI.ServerSided.Program.Main(String[] args) in /src/BlazorUI.ServerSided/Program.cs:line 19

EDIT: This is a clean project, added by Visual Studio when creating new ASP.Net Core Web Application > Blazor (server-side). No other changes were made to the project.

Dockerfile was created by selecting the project (right-click) > add > docker support:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-stretch-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.0-stretch AS build
WORKDIR /src
COPY ["BlazorUI.ServerSided/BlazorUI.ServerSided.csproj", "BlazorUI.ServerSided/"]
RUN dotnet restore "BlazorUI.ServerSided/BlazorUI.ServerSided.csproj"
COPY . .
WORKDIR "/src/BlazorUI.ServerSided"
RUN dotnet build "BlazorUI.ServerSided.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "BlazorUI.ServerSided.csproj" -c Release -o /app

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

I am using .net-core 3-preview4-011223 and linux containers.

The image is published here.

Upvotes: 2

Views: 3554

Answers (2)

Ipax77
Ipax77

Reputation: 1

This is what I found out so far and it is working for now (with https and subdomain):

Setup subdomain in Startup.cs -> Configure:

        string basePath = Environment.GetEnvironmentVariable("ASPNETCORE_BASEPATH");
        if (!string.IsNullOrEmpty(basePath))
        {
            app.Use((context, next) =>
            {
                context.Request.Scheme = "https";
                return next();
            });

            app.Use((context, next) =>
            {
                context.Request.PathBase = new PathString(basePath);
                if (context.Request.Path.StartsWithSegments(basePath, out var remainder))
                {
                    context.Request.Path = remainder;
                }
                return next();
            });
        }
        app.UseForwardedHeaders(new ForwardedHeadersOptions

Apache config for example subdomain ‘helloapp’ and docker port 9124 aspnetcore-3.0#configure-apache

<VirtualHost *:*>
RequestHeader set "X-Forwarded-Proto" expr=%{REQUEST_SCHEME}
</VirtualHost>
<VirtualHost *:443>
  ServerName www.example.com
  ServerAlias example.com

  ErrorLog ${APACHE_LOG_DIR}helloapp-error.log
  CustomLog ${APACHE_LOG_DIR}helloapp-access.log common

  UseCanonicalName On
  SSLEngine on
  SSLCertificateFile /etc/letsencrypt/live/example.com/cert.pem
  SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
  SSLCipherSuite HIGH

  SSLProxyEngine On
  ProxyPreserveHost On

  ProxyRequests Off
  RewriteEngine On
  RewriteCond %{HTTP:Upgrade} =websocket
  RewriteRule /helloapp/(.*) wss://localhost:9124/$1 [P,QSA,L]

  <Location /helloapp >
    LoadModule proxy_module modules/mod_proxy.so
    LoadModule proxy_http_module    modules/mod_proxy_http.so
    LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so
    ProxyPreserveHost On
    ProxyPass https://127.0.0.1:9124
    ProxyPassReverse https://127.0.0.1:9124
  </Location>

</VirtualHost>

Assuming you have a letsencrypt SSLCertificate you have to convert it so Kestrel can use it. On your Linux Server run in a terminal the following command. The path to the privkey, cert and chain file can be found in your apache config. This will create a certificate.pfx – you have to enter a valid password for Kestrel.

openssl pkcs12 -export -out certificate.pfx -inkey privkey.pem -in cert.pem -certfile chain.pem

Get the Dockerfile from hub.docker e.g. bionic/amd64/Dockerfile Add this line at the bottom of the Dockerfile:

COPY ./certificate.pfx /root/certificate.pfx

Copy the certificate.pfx file and the Dockerfile to an empty folder. Create a file docker-compose.yml:

version: '3.3'

services:
  helloapp:
    build: .
    #restart: always
    ports:
      - "9123:80"
      - "9124:443"
    environment:
      ASPNETCORE_BASEPATH: /helloapp
      ASPNETCORE_HTTPS_PORT: 9124
      ASPNETCORE_URLS: "https://+:443;http://+:80"
      Kestrel__Certificates__Default__Path: /root/certificate.pfx
      Kestrel__Certificates__Default__Password: mycertificatesecret
    volumes:
      - ./helloapp:/helloapp
    working_dir: /helloapp
    command: ["dotnet", "helloapp.dll"] 

Create a subfolder helloapp and copy your published WebApp to it.

Then you can start the container and it should be availabe in www.example.com/helloapp

docker-compose build
docker-compose up

Upvotes: 0

codevision
codevision

Reputation: 5560

You are hitting well known issue in Preview 4 release which affect Linux build See https://github.com/aspnet/AspNetCore/issues/9402 Asp.NET Team claim to fix that in Preview 5

To solve your issue, you should use daily build of Preview 5 as of 5 May 2019.

You can get builds of Preview5 from https://github.com/aspnet/AspNetCore/blob/master/docs/DailyBuilds.md

Upvotes: 2

Related Questions