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)

Most upvoted comments

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 publish or dotnet lambda package commands). I placed try…catch block with logging and rethrowing exception, and here is my log in CloudWatch:

Swagger ERROR: System.IO.FileNotFoundException: Could not find file '/var/task/SampleService.xml'. File name: '/var/task/SampleService.xml' at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirectory, Func2 errorRewriter) at Interop.CheckIo[TSafeHandle](TSafeHandle handle, String path, Boolean isDirectory, Func`2 errorRewriter) at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String path, OpenFlags flags, Int32 mode) at System.IO.UnixFileStream…ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent) at System.IO.UnixFileSystem.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) at System.Xml.XmlSystemPathResolver.GetEntity(Uri uri, String role, Type typeOfObjectToReturn) at System.Xml.XmlTextReaderImpl.FinishInitUriString() at System.Xml.XmlReaderSettings.CreateReader(String inputUri, XmlParserContext inputContext) at System.Xml.XPath.XPathDocument…ctor(String uri, XmlSpace space) at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenOptions.<>c__DisplayClass25_0.<IncludeXmlComments>b__0() at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenOptions.CreateSwaggerProvider(IServiceProvider serviceProvider)

START RequestId: 90871711-4dd3-11e7-853d-ab90391b490f Version: $LATEST An exception was thrown when the constructor for type ‘SampleService.LambdaEntryPoint’ 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) at Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddMvc(IServiceCollection services, Action`1 setupAction) at MobileGatewayService.Startup.ConfigureServices(IServiceCollection services)

END RequestId: 90871711-4dd3-11e7-853d-ab90391b490f REPORT RequestId: 90871711-4dd3-11e7-853d-ab90391b490f Duration: 1212.72 ms Billed Duration: 1300 ms Memory Size: 1024 MB Max Memory Used: 55 MB `

I did not change packages versions to be 1.0.* (for example swagger nuget package references NETStandard.Library >= 1.6.1), did not change project target to something other then default netcoreapp1.0 - it was just a simple funny error.

So my conclusion for now is: Native image cannot be loaded multiple times: FileLoadException means 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 release really 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:

@IgorFedchenko https://github.com/igorfedchenko Did you manage to solve your swagger problem? I’m having the exact same issue 😃

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/aws/aws-lambda-dotnet/issues/124#issuecomment-337257943, or mute the thread https://github.com/notifications/unsubscribe-auth/AA6JcC7L4c2jKn3lwOpOs-BohAVsUly_ks5stMB6gaJpZM4NoXpr .

And about the issue with loggerFactory.AddConsole(); - no need to remove this statement. Just change the version of the corresponding package (Microsoft.Extensions.Logging.Console in 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).