unoplatform / Uno.Wasm.Bootstrap

A simple nuget package to run C# code in a WASM-compatible browser
Other
367 stars 57 forks source link

Hosting in ASP.NET Core #191

Open bricelam opened 4 years ago

bricelam commented 4 years ago

I would like to host an Uno WASM project inside an ASP.NET Core project (similar to hosting Blazor WebAssembly). I dug a little into how Blazor does it, and it looks like they generate a StaticWebAssets.xml file based on referenced projects. This allows the ASP.NET Core app to serve static files from the WebAssembly project. Could you add similar targets to the Uno WASM build scripts to piggyback on this functionality?

Also, is there a sample showing how to do this? I'm making progress, but it's a slow going.

jeromelaban commented 4 years ago

Thanks for the suggestion. At this point, there is no sample on how to do that, but if StaticWebAsset is supported in the default Web SDK, then it should not be a big deal to add.

If you be added in the publish targets: https://github.com/unoplatform/Uno.Wasm.Bootstrap/blob/8add3fd92e26891863414b9fa407830081f7d732/src/Uno.Wasm.Bootstrap/build/Uno.Wasm.Bootstrap.Publish.targets#L5

or after the build dist target:

https://github.com/unoplatform/Uno.Wasm.Bootstrap/blob/8add3fd92e26891863414b9fa407830081f7d732/src/Uno.Wasm.Bootstrap/build/Uno.Wasm.Bootstrap.targets#L98

FrancoisM commented 2 years ago

Hello. Did you manage to do it? Any sample to share? Right now I think that in my CI I'll copy the wasm dist folder to the wwwroot of the aspnetcore.

FrancoisM commented 2 years ago
<Project>
    <Target Name="CopyingDistToAspNetWwwRoot" AfterTargets="BuildDist">
        <ItemGroup>
            <DistFiles Include="$(OutputPath)dist\**\*.*" />
        </ItemGroup>        
        <Copy SourceFiles="@(DistFiles)"
              DestinationFolder="..\..\MyApp.WebApp\wwwroot\wasm\%(RecursiveDir)" SkipUnchangedFiles="true"/>
    </Target>   
</Project>
mmarinchenko commented 2 years ago

@FrancoisM

  1. You may just set WasmShellDistPath to ..\..\MyApp.WebApp\wwwroot\wasm to achieve the same effect (unless you need to save a copy inside $(OutputPath)dist).

  2. You probably should change WebRootPath to \wwwroot\wasm in your ASP.NET Core hosting configuration or pass this path to UseStaticFiles()/UseSpaStaticFiles() to correctly serve http[s]:\\host:[port]\wasm\* requests.

mmarinchenko commented 2 years ago

Alternatively, you can put Uno wasm app directly in wwwroot folder. Here's what it might looks like in a Dockerfile:

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
ENV ASPNETCORE_URLS=https://+:443;http://+:80
EXPOSE 443 80
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
COPY . /src
RUN dotnet build "/src/Server/Host/Host.csproj" -c Release -o /app/build
RUN dotnet build "/src/Client/Wasm/Wasm.csproj" -c Release -p:WasmShellDistPath=/app/build/wwwroot

FROM build AS publish
RUN dotnet publish "/src/Server/Host/Host.csproj" -c Release -r linux-x64 -o /app/publish --self-contained=false
RUN dotnet publish "/src/Client/Wasm/Wasm.csproj" -c Release -r browser-wasm -p:WasmShellDistPath=/app/publish/wwwroot --self-contained=false
RUN find /app/publish -type f -name '*.pdb' -delete

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

P.S. I'm using .NET 5.0 in my project for now. I haven't tried 6.0 yet, so things might be different there.