Aws-lambda-dotnet: Deploy with Visual Studio 2017 fails

Created on 3 Apr 2017  路  10Comments  路  Source: aws/aws-lambda-dotnet

I just took a VS 2015 project and compiled and deployed to Lambda with VS 2017.

This is the result:

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)
Exception has been thrown by the target of an invocation.: TargetInvocationException
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()
The given key was not present in the dictionary.: KeyNotFoundException
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)
guidance

Most helpful comment

My Solution: Open the csproj file and set PreserveCompilationContext to false.
<PreserveCompilationContext>false</PreserveCompilationContext>

Further Explanation:
The VS 2017 projects set PreserveCompilationContext to true. This means ,when you publish/deploy, VS emits a "ref" folder with a lot of the same assemblies as the root publish folder. I suspect that something is recursively scanning all assemblies and screwing that up, but i'm not sure at all. All I know so far is that I changed that setting and it now works, and thought I would share this to see if it helps others.

All 10 comments

Hi @genifycom , could you share the csproj file for your project?

I get this same error from AddMvc() when I deploy from VS2017.

Can you provide the csproj file for your project?

I might be able to work up a repro one that I could share, preference on sharing method?

A GitHub repo with the reproducible code in it would be good. The less extraneous code in there, the easier it will be for us to identify the issue.

I just generated it using yeoman on mac. Then converted the project to csproj using dotnet migrate. When it is published and invoked, I get the following exception:

{
  "errorType": "LambdaException",
  "errorMessage": "An exception was thrown when the constructor for type 'PreviewAspNetCoreWebAPI.LambdaEntryPoint' 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)",
          "at PreviewAspNetCoreWebAPI.Startup.ConfigureServices(IServiceCollection services) in /PreviewAspNetCoreWebAPI/src/PreviewAspNetCoreWebAPI/Startup.cs:line 34"
        ]
      }
    }
  }
}

Thanks for reproducing @KraigM

This is a blocking issue for me currently.

My Solution: Open the csproj file and set PreserveCompilationContext to false.
<PreserveCompilationContext>false</PreserveCompilationContext>

Further Explanation:
The VS 2017 projects set PreserveCompilationContext to true. This means ,when you publish/deploy, VS emits a "ref" folder with a lot of the same assemblies as the root publish folder. I suspect that something is recursively scanning all assemblies and screwing that up, but i'm not sure at all. All I know so far is that I changed that setting and it now works, and thought I would share this to see if it helps others.

@DohMoose that unblocked me as well. It seems this is likely an issue in the dotnet core migrate not adding the compilation context false to project files?

@DohMoose Awesome, that resolved the issue for me too. Thank you.

For context, I switched from a VS2015 deployment to a VS2017 and was getting that error on launch.

Was this page helpful?
0 / 5 - 0 ratings