aws-lambda-dotnet: AspNetCoreServer - Fails to bootstrap with "Native image cannot be loaded" exception
Hi, I’m having some trouble with trying to do a simple hello world deployment of Web API to Lambda and keep hitting Native image cannot be loaded multiple times exception when attempting to deploy my application.
Looking in cloud watch I see the following:
An exception was thrown when the constructor for type 'Cram.Lambda.LambdaFunction' was invoked. Check inner exception for more details.: LambdaException
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()
Native image cannot be loaded multiple times: FileLoadException
at Microsoft.Extensions.DependencyInjection.MvcRazorMvcCoreBuilderExtensions.AddRazorViewEngineServices(IServiceCollection services)
at Microsoft.Extensions.DependencyInjection.MvcRazorMvcCoreBuilderExtensions.AddRazorViewEngine(IMvcCoreBuilder builder)
at Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddMvc(IServiceCollection services)
My .csproj file (using dotnet 1.0.4) is as follows:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp1.0</TargetFramework>
<PreserveCompilationContext>false</PreserveCompilationContext>
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Amazon.Lambda.AspNetCoreServer" Version="0.10.1-preview1" />
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Routing" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Amazon.Lambda.Tools" Version="1.4.0" />
</ItemGroup>
</Project>
I’m unclear as to what the issue might be and did not find anything in prior issues. I was able to work around an earlier issue by adding the <PreserveCompilationContext>false</PreserveCompilationContext> which I uncovered in prior issues here. Has anyone encountered this before and might know how I’ve misconfigured something in my project setup?
In case it is relevant, I’m using serverless 1.14.0 to package the application which compiles as follows:
dotnet restore
dotnet publish -c release
Any help is appreciated. Thanks!
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 18 (3 by maintainers)
Ok, now I have completely sorted out my issue. I am using Swagger (Swashbuckle.AspNetCore package) to display online documentation for my lambda-hosted service. On my local machine all worked, but after deploying it was crashing with already mentioned exception (Native image could not be loaded…). After getting package sources (OpenSource is cool) and debugging with “Console.WriteLine” technique I have found that there was a real exception about missing XML doc file (it did not copy with
dotnet publishordotnet lambda packagecommands). I placed try…catch block with logging and rethrowing exception, and here is my log in CloudWatch:So my conclusion for now is:
Native image cannot be loaded multiple times: FileLoadExceptionmeans some other exception in your application during startup, and it looks like it is not related to your target framework version.P.S. Anyway I would recommend to set nuget project versions to be compatible with .NET Code 1.0 (not depend from .NETStandard.Library 1.6.1) - and
dotnet lambda package -c releasereally helps. It is running on lambda without this checks anyway, but just to be safe from runtime craches.P.P.S. But it would be much better if this exception was in CloudWatch log without placing debug output lines into specially downloaded package sources (but I am not sure this is an AWS Lambda problem)
OK, I found that the problem was the loggerFactory.AddConsole(); in the Startup class. Removing it resolved the issue.
I did manage to fix my problem. Let me know if you’re having issues. I’ll b willing to do a team viewer session to hep out.
On Oct 17, 2017 7:58 AM, “Frederik Nygaard Svendsen” < notifications@github.com> wrote:
And about the issue with
loggerFactory.AddConsole();- no need to remove this statement. Just change the version of the corresponding package (Microsoft.Extensions.Logging.Consolein this case) from 1.1.* to 1.0.* (from 1.1.2 to 1.0.2 in my case) - so that is was not referencing NETStandard.Library 1.6.1 (I am usually checking dependencies on the package’s NuGet page).