aws / aws-lambda-dotnet

Libraries, samples and tools to help .NET Core developers develop AWS Lambda functions.
Apache License 2.0
1.57k stars 479 forks source link

Hosted Blazor client in AWS Serverless DotNet 6 failed to publish to lambda #1092

Closed genifycom closed 2 years ago

genifycom commented 2 years ago

The Question

I am trying out a Hosted Blazor app with a DotNet Lambda Serverless App after the release of the 6.0 Managed runtime.

The host Lambda Proj file is:

`

net6.0 true Lambda true

`

The Host project references the Client Blazor WebAssembly App and has to include the package Microsoft.AspNetCore.Components.WebAssembly.Server

BlazorApp2.csproj is a standard WebAssembly app with proj file

`

net6.0 enable enable

`

The host Startup.cs now includes:

//Required for Blazor client app.UseBlazorFrameworkFiles(); app.UseStaticFiles();

Using AWS Toolkit 1.26.0.0 I try to "Publish to AWS Lambda ..." and I get the following message on Failed to publish:

publish: C:\Program Files\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.2-mauipre.1.22102.15\Sdk\WasmApp.targets(161,5): error : $(MicrosoftNetCoreAppRuntimePackDir)='', and cannot find %(ResolvedRuntimePack.PackageDirectory)=. One of these need to be set to a valid path [C:\Dev\BlazorApp2\BlazorApp2.csproj]

I see that this includes a reference to mauipre which is curious since I created a standard Blazor Webassembly client in the non preview Visual Studio.

Anyone know what can be done to make hosted blazor in Lambda possible "out of the box"?

Thank you

Environment


This is a :question: general question

ashishdhingra commented 2 years ago

Hi @genifycom,

Good afternoon.

Thanks for posting guidance question.

Would it be possible to share the sample code solution and detailed reproduction steps? This would help us reproducing the issue on the exact configuration and determine root cause.

Thanks, Ashish

github-actions[bot] commented 2 years ago

This issue has not received a response in 5 days. If you want to keep this issue open, please just leave a comment below and auto-close will be canceled.

genifycom commented 2 years ago

A hosted Blazor application is one where the host server can serve up a Blazor Webassembly client.

This included project therefore has two parts.

  1. An AWS Lambda minimal API standard template sample
  2. A standard Blazor Webassembly client

On its own, 1 publishes to Lambda and runs. When a project dependency is created in 1 to 2. The Lambda project no longer publishes.

The log is

Processing CloudFormation resource AspNetCoreFunction Initiate packaging of . for resource AspNetCoreFunction Executing publish command Deleted previous publish folder ... invoking 'dotnet publish', working folder 'C:\Dev\AITC\LambdaBlazor\LambdaBlazor\AWSServerless1.\bin\Release\net6.0\publish' ... dotnet publish --output "C:\Dev\AITC\LambdaBlazor\LambdaBlazor\AWSServerless1.\bin\Release\net6.0\publish" --configuration "Release" --framework "net6.0" /p:GenerateRuntimeConfigurationFiles=true --runtime linux-x64 --self-contained false ... publish: Microsoft (R) Build Engine version 17.2.0-preview-22126-01+40f49cd8d for .NET ... publish: Copyright (C) Microsoft Corporation. All rights reserved. ... publish: Determining projects to restore... ... publish: Restored C:\Dev\AITC\LambdaBlazor\LambdaBlazor\BlazorApp1\BlazorApp1.csproj (in 291 ms). ... publish: Restored C:\Dev\AITC\LambdaBlazor\LambdaBlazor\AWSServerless1\AWSServerless1.csproj (in 291 ms). ... publish: You are using a preview version of .NET. See: https://aka.ms/dotnet-core-preview ... publish: C:\Program Files\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.3\Sdk\WasmApp.targets(161,5): error : $(MicrosoftNetCoreAppRuntimePackDir)='', and cannot find %(ResolvedRuntimePack.PackageDirectory)=. One of these need to be set to a valid path [C:\Dev\AITC\LambdaBlazor\LambdaBlazor\BlazorApp1\BlazorApp1.csproj] Failed to create application package Error packaging up project in C:\Dev\AITC\LambdaBlazor\LambdaBlazor\AWSServerless1. for CloudFormation resource AspNetCoreFunction LambdaBlazor.zip

This precludes us from any Hosted Blazor Lambda application using the Managed DotNet runtimes.

genifycom commented 2 years ago

Any update on being able to run hosted Blazor in AWS Lambda?

genifycom commented 2 years ago

Can a repo collaborator reopen this please. I really need hosted Blazor in Lambda and I provided a simple example. This is not the whole story but I have tried to get some traction on this in multiple issues.

Now that we have DotNet 6, this should work.

genifycom commented 2 years ago

Thank you :)

normj commented 2 years ago

I'm not able to reproduce your exact issue which I think has something to do with your environment. I think a bigger problem is once you get past the environment problem the lambda publish will fail because we pass in a runtime to the publish command of linux-x64 to reduce the dependency bundled. The .NET CLI fails for this in this situation because it pass that runtime to the WASM project which it doesn't support. Here is the error I got.

... publish: C:\Program Files\dotnet\sdk\6.0.300-preview.22154.4\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.ILLink.targets(213,5): error NETSDK1102: Optimizing assemblies for size is not supported for the selected publish configuration. Please ensure that you are publishing a self-contained app. [C:\Users\normj\Downloads\LambdaBlazor\BlazorApp1\BlazorApp1.csproj]

When I hacked the Lambda publishing tools to not include the runtime I was able to do a successful publish and render the WASM application. So if we were to support this scenario we need a way to turn off passing in the runtime.

Have you considered deploying the WASM application with our new deploy tool currently for non-lambda deployments, https://github.com/aws/aws-dotnet-deploy? What I would recommend doing is deploying the Blazor WASM project with the new deploy tool which will host the application in S3/CloudFront. That way you are not using Lambda functions for returning static content (Blazor Assemblies).

image

With that deployment tooling you can configure a backend REST API which can be your Lambda function via API Gateway.

image

github-actions[bot] commented 2 years ago

This issue has not received a response in 5 days. If you want to keep this issue open, please just leave a comment below and auto-close will be canceled.

genifycom commented 6 months ago

@normj Hi Norm,

I opened a .NET 8 question against the AWS toolkit for Visual Studio: https://github.com/aws/aws-toolkit-visual-studio/issues/413

And I closed a discussion question here: https://github.com/aws/aws-lambda-dotnet/discussions/1711

So in .NET 8, if we want a Lambda Application that can serve up server side pages (Razor pages or server-side Razor components) everything is fine. If we have a @rendermode InteractiveWebAssembly Razor components then we cannot currently use that.

It works in a Lambda Application locally but cannot package it up for Lambda distribution.

Can you offer any suggestions of how to package up the interactive WASM component?

I tried copying the published _framework (which has the component WASM file and all of the supporting WASM runtime elements) and deploying this with the Lambda publish.

Binary Media for the APIGateway is set to /

Should this work?

Thank you, Dave

genifycom commented 6 months ago

Made some progress with the following proj file

<Project Sdk="Microsoft.NET.Sdk.Web">
    <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>

        <AWSProjectType>Lambda</AWSProjectType>

        <!-- This property makes the build directory similar to a publish directory and helps the AWS .NET Lambda Mock Test Tool find project dependencies. -->
        <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>

        <!-- Generate ready to run images during publishing to improve cold start time. -->
        <PublishReadyToRun>false</PublishReadyToRun>
    </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="Amazon.Lambda.AspNetCoreServer" Version="9.0.0" />
        <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="8.0.3" />
        <ProjectReference Include="..\CSRInLambda\CSRInLambda.Client\CSRInLambda.Client.csproj" />
    </ItemGroup>
    <ItemGroup>
        <Folder Include="wwwroot\images\" />
    </ItemGroup>
</Project>

This allows me to publish locally to a folder but the publish to AWS still fails with:

... publish: C:\Users\yyy.nuget\packages\microsoft.net.sdk.webassembly.pack\8.0.3\build\Microsoft.NET.Sdk.WebAssembly.Browser.targets(231,5): error WASM0005: Unable to resolve WebAssembly runtime pack version [C:\Dev\xxx\CSRInLambda\CSRInLambda\CSRInLambda.Client\CSRInLambda.Client.csproj]