9

Today I have a question which is one of those questions, that you always did want to know but you were always afraid to ask;) But this question is also correlated with BlazorWebAssembly app docker support. So maybe I will describe what I did.

SO I did want to play a little bit with cutting edge blazor and of course .NET 5.0 (my last "cutting edge technology" was ASP.NET MVC - so yeah, it have been a while :) )

In the beginning i didn't quite know what those option correlated with Blazor was for. So i've chosen:

enter image description here

You can spot, that the option "Enable Docker Support" is greyed out. So it was like "docker support problem preview" :)

I didn't know what option: "ASP.NET Core hosted" stand for, so I didn't use it (and it was a mistake) Once you check this option it will create one server app, one client (Blazor) one and one shared (which is for model purposes between them)

I didn't know that, so I did have to add on my own WebApi project and some project for the model :)

Now, the next step was to add docker support (which surprisingly is possible)

enter image description here

And this created a docker file:

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
WORKDIR /src
COPY ["BlazorApp/BlazorApp.csproj", "BlazorApp/"]
COPY ["../SearchMovies.Model/SearchMovies.Model.csproj", "../SearchMovies.Model/"]
RUN dotnet restore "BlazorApp/BlazorApp.csproj"
COPY . .
WORKDIR "/src/BlazorApp"
RUN dotnet build "BlazorApp.csproj" -c Release -o /app/build

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

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

Starting such app will end with something like that:

docker exec -i -e ASPNETCORE_HTTPS_PORT="32776" -w "/app" 28d444a42ceefdf124b29f9f542f423aa931e521d077bbfeb19e5da7644a5e25 sh -c ""dotnet" --additionalProbingPath /root/.nuget/fallbackpackages2 --additionalProbingPath /root/.nuget/fallbackpackages --additionalProbingPath /root/.nuget/fallbackpackages3  \"/app/bin/Debug/net5.0/BlazorApp.dll\" | tee /dev/console"
Cannot use file stream for [/app/bin/Debug/net5.0/BlazorApp.deps.json]: No such file or directory
A fatal error was encountered. The library 'libhostpolicy.so' required to execute the application was not found in '/app/bin/Debug/net5.0/'.
Failed to run as a self-contained app.
  - The application was run as a self-contained app because '/app/bin/Debug/net5.0/BlazorApp.runtimeconfig.json' was not found.
  - If this should be a framework-dependent app, add the '/app/bin/Debug/net5.0/BlazorApp.runtimeconfig.json' file and specify the appropriate framework.

I've already googled it, and the answer was: yeah, it is normal behaviour. Blazor is just a piece of static files (whicha are only "smartly" produced) So that is why it doesn't work. Tbh, I hear them, but still... Why o why?

I've googled it a little bit. I found some nice article about Blazor Webassembly here:

https://medium.com/@waelkdouh/hosting-containerized-client-side-blazor-applications-on-azure-e090af738619

Some fun fact at the begining (the quote from the article)

Note: I imagine this checkbox will be enabled sometime in the future

So guys, just to calm you. We are definively not in the futrue :)

So i've changed the docker file. And if i remember correctly - runing it "manually" as in the article by using "docker build" and "docker run" it started successfuly.

But when trying to hit "Debug" (or start without debuging) the error was the same as previously. Here is the docker file proposed there (i've only changed the version to 5.0 and changed publish to run only on the project):

FROM mcr.microsoft.com/dotnet/core/sdk:5.0-buster-slim AS build-env
WORKDIR /app
COPY . ./
RUN dotnet publish "BlazorApp/BlazorApp.csproj" -c Release -o output

FROM nginx:alpine
WORKDIR /var/www/web
COPY --from=build-env /app/output/wwwroot .
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80

So two attempts. I've checked also another cutting edge orchestration thingy called tye:

https://devblogs.microsoft.com/aspnet/introducing-project-tye/

But it cannot handle the Blazor as well.

I've even quickly created blazor app with ASP.NET Core hosted option. After tye init, tye build and tye run there are no bindings for the client project:

enter image description here

So maybe someone have some idea, what can i try?

And the question mentioned at the very begining was regarding orchestration i suppose. Let's assume i have four projects in solution - for example, Blazor and three webapi`s. How to run everything so that it will automagically bind the endpoints. Because normally i have to add the url for endpoints like: "https://localhost:5123/api/SomeService"

And docker at least when starting can assign different port numbers for those API's Changing in the code manually seems like the dummy thing to do.

What is the best way to tackle such cases - where there is a lot o communications between endpoints and i want to start all of them and they will be automatically "connected" (meaning, they will know on which port the "other" service is)?

Thanks in advance for anwsers on those two questions

Piotr
  • 875
  • 6
  • 24
  • Question for clarification: why do you want to use Docker for a static website like Blazor Wasm? Which problem is this supposed to resolve? – Akinzekeel Nov 14 '20 at 01:36
  • The problem of local testing changes (as everything you are putting into docker) You have some piece of code that you are developing and you want to test that. In order to test you need some url (no matter if dynamic or static website) – Piotr Nov 14 '20 at 01:41
  • "In order to test you need some url" Correct me if I'm wrong but I think Docker is not going to magically start serving static files via HTTP. What you need is a web server (which you get with the option Asp.Net Core hosted) – Akinzekeel Nov 14 '20 at 13:38
  • Docker hardly do anything magically at all. It does what you command him to do in dockerfile :) What i am complaining about is: that the "Add docker support" is not doing it right for Blazor (because as described in the article i've linked - you can just build it and copy output to folder which will be hosted in NGINX) But as i've said, even if i define it as so - it still won't build (start without debuging) And on top of that, i've choosen option "Asp.net core hosted" this time and still, the docker support is not working (won't start) – Piotr Nov 14 '20 at 14:21

0 Answers0