Aws-lambda-dotnet: How to build in Visual Studio 2017

Created on 14 Apr 2017  路  8Comments  路  Source: aws/aws-lambda-dotnet

Can anyone at Amazon indicate the steps to get a Visual Studio 2017 ASP.NET Core Lambda application working.

The Visual Studio 2015 approach as per the talk "AWS re:Invent 2016: NEW LAUNCH! Developing Serverless C# Applications " no longer works in VS 2017 as there is no project.json (now csproj) and so Publish to Lambda does not show up.

Is there are new C# ASp.NET Core Lambda base (just api/values) writeup anywhere please.

Thank you,

Dave

guidance

Most helpful comment

Here are the steps I have done to get VS 2017 projects working in Lambda.

1) Add the Amazon.Lambda.AspNetCoreServer, LambdaEntryPoint.cs and serverless.template like you did in VS 2015 project
2) Right click and select "Edit .csproj
3) Add The following xml to the csproj to add the Amazon.Lambda.Tools

  <ItemGroup>
    <DotNetCliToolReference Include="Amazon.Lambda.Tools" Version="1.4.0" />
  </ItemGroup>

4) In the property group section add the following. Turning this off is related to the issues with Razor pages not working.

  <PreserveCompilationContext>false</PreserveCompilationContext>

You should be good to go now

All 8 comments

Here are the steps I have done to get VS 2017 projects working in Lambda.

1) Add the Amazon.Lambda.AspNetCoreServer, LambdaEntryPoint.cs and serverless.template like you did in VS 2015 project
2) Right click and select "Edit .csproj
3) Add The following xml to the csproj to add the Amazon.Lambda.Tools

  <ItemGroup>
    <DotNetCliToolReference Include="Amazon.Lambda.Tools" Version="1.4.0" />
  </ItemGroup>

4) In the property group section add the following. Turning this off is related to the issues with Razor pages not working.

  <PreserveCompilationContext>false</PreserveCompilationContext>

You should be good to go now

Thanks so much Norm :-)

Wait, number 4 is there a bit missing?

  1. In the property group section add the following. Turning this off is related to the issues with Razor pages not working.
    false

What is the following here?

Thanks,
Dave

Also if I try to install Amazon.Lambda.Tools via Nuget I get:

Package 'Amazon.Lambda.Tools 1.5.0' has a package type 'DotnetCliTool' that is not supported by project 'LambdaTest'.

That doesn't make much sense since there is already

<ItemGroup>
  <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0" />
</ItemGroup>

in the csproj.

Ok, VS2017 I created a New Project ASP.NET Core Web Application (.NET Core).
Made sure ASP.NET Core 1.0 was selected and picked Web API.

CSPROJ is:

  <PropertyGroup>
    <TargetFramework>netcoreapp1.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <Folder Include="wwwroot\" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore" Version="1.0.4" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.0.3" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.0.2" />
    <PackageReference Include="Amazon.Lambda.AspNetCoreServer" Version="0.10.1-preview1" />
  </ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="Amazon.Lambda.Tools" Version="1.5.0" />
  </ItemGroup>

Added the Lambda function:

using System.IO;
using Microsoft.AspNetCore.Hosting;
namespace LambdaTest
{
    public class LambdaFunction : Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction
    {
        protected override void Init(IWebHostBuilder builder)
        {
            builder
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseStartup<Startup>()
                .UseApiGateway();
        }
    }
}

Locally it works. Publish to AWS Lambda and test the function /api/values

I get the following:

{
    "errorType" : "LambdaException",
    "errorMessage" : "An exception was thrown when the constructor for type 'LambdaTest.LambdaFunction' was invoked. Check inner exception for more details.",
    "cause"        : {
        "errorType" : "TargetInvocationException",
        "errorMessage" : "Exception has been thrown by the target of an invocation.",
        "stackTrace"   : [
            "at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)",
            "at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)",
            "at System.Activator.CreateInstance(Type type, Boolean nonPublic)",
            "at System.Activator.CreateInstance(Type type)"
        ],
        "cause"        : {
            "errorType" : "TargetInvocationException",
            "errorMessage" : "Exception has been thrown by the target of an invocation.",
            "stackTrace"   : [
                "at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)",
                "at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)",
                "at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)",
                "at Microsoft.AspNetCore.Hosting.Internal.ConfigureServicesBuilder.Invoke(Object instance, IServiceCollection exportServices)",
                "at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()",
                "at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()",
                "at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()",
                "at Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction..ctor()"
            ],
            "cause"        : {
                "errorType" : "KeyNotFoundException",
                "errorMessage" : "The given key was not present in the dictionary.",
                "stackTrace"   : [
                    "at System.Collections.Generic.Dictionary`2.get_Item(TKey key)",
                    "at Microsoft.AspNetCore.Mvc.Internal.DefaultAssemblyPartDiscoveryProvider.CandidateResolver.ComputeClassification(String dependency)",
                    "at Microsoft.AspNetCore.Mvc.Internal.DefaultAssemblyPartDiscoveryProvider.CandidateResolver.ComputeClassification(String dependency)",
                    "at Microsoft.AspNetCore.Mvc.Internal.DefaultAssemblyPartDiscoveryProvider.CandidateResolver.ComputeClassification(String dependency)",
                    "at Microsoft.AspNetCore.Mvc.Internal.DefaultAssemblyPartDiscoveryProvider.CandidateResolver.ComputeClassification(String dependency)",
                    "at Microsoft.AspNetCore.Mvc.Internal.DefaultAssemblyPartDiscoveryProvider.CandidateResolver.<GetCandidates>d__4.MoveNext()",
                    "at System.Linq.Enumerable.<SelectManyIterator>d__157`2.MoveNext()",
                    "at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext()",
                    "at Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.GetApplicationPartManager(IServiceCollection services)",
                    "at Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.AddMvcCore(IServiceCollection services)",
                    "at Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddMvc(IServiceCollection services)"
                ]
            }
        }
    }
}

Ok I think I have it now.

Even if not there, you must explicitly add

    <PreserveCompilationContext>false\</PreserveCompilationContext>

In PropertyGroup.

I guess that is the missing bit Norm?

Thanks,
Dave

Yes sorry about that. The escaping of XML got me tricked up but you need to add the PreserveCompilationContext

About Amazon.Lambda.Tools, currently Visual Studio 2017 NuGet manager doesn't support DotnetCliTool which is why you have to manually add Amazon.Lambda.Tools. I hope they update Visual Studio 2017 soon to fix this. I know there is a Microsoft GitHub issue on this but I don't have the link right now.

Ok thanks Norm

Was this page helpful?
0 / 5 - 0 ratings