Uno.Wasm.Bootstrap icon indicating copy to clipboard operation
Uno.Wasm.Bootstrap copied to clipboard

Hosting in ASP.NET Core

Open bricelam opened this issue 5 years ago • 5 comments

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.

bricelam avatar Apr 09 '20 03:04 bricelam

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

jeromelaban avatar Apr 09 '20 11:04 jeromelaban

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 avatar Oct 29 '21 18:10 FrancoisM

<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>

FrancoisM avatar Oct 29 '21 20:10 FrancoisM

@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 avatar Oct 30 '21 08:10 mmarinchenko

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.

mmarinchenko avatar Oct 30 '21 09:10 mmarinchenko