C# MongoDb Driver won't connect to Mongo instance when running inside AWS Lambda / pure .netcoreapp1.0

797 views
Skip to first unread message

Stewart Snow

unread,
Jul 20, 2017, 6:23:02 PM7/20/17
to mongodb-user
Hi There,
We're in the process of attempting to move a number of micro services with mongo backends to AWS Lambda. 

However having major issues getting the driver to run appropriately inside of Lambda. The code all runs fine when on Windows (it's all pure .net core - net standard 1.6) targeting netcoreapp1.0 (As this is as far as AWS Lambda supports for now).

All the non-mongo code in Lambda runs fine, but as soon as we try to make a connection to a mongo instance using the MongoClient we get the following error:

Unknown error responding to request: FileNotFoundException:
System.IO.FileNotFoundException: Could not load file or assembly 'System.Security.SecureString, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.

File name: 'System.Security.SecureString, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' ---> System.IO.FileNotFoundException: 'System.Security.SecureString, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' not found in the deployment package or in the installed Microsoft.NETCore.App.
at AWSLambda.Internal.Bootstrap.LambdaAssemblyLoadContext.Load(AssemblyName assemblyName)
at System.Runtime.Loader.AssemblyLoadContext.ResolveUsingLoad(AssemblyName assemblyName)
at System.Runtime.Loader.AssemblyLoadContext.Resolve(IntPtr gchManagedAssemblyLoadContext, AssemblyName assemblyName)
at MongoDB.Driver.PasswordEvidence..ctor(String password)
at MongoDB.Driver.MongoCredential.FromComponents(String mechanism, String source, String username, String password)
at MongoDB.Driver.MongoClientSettings.FromUrl(MongoUrl url)
at TestASPNetCoreServerWebApi.Controllers.ValuesController.Get()
at lambda_method(Closure , Object , Object[] )


Having dived into it a bit, we can see this relates to the MongoUrl & MongoClient's use of SecureString - but it's not clear at all why the driver doesn't have access to the assembly's that it needs. We've scoured online and can find no other references to this problem other than a brief mention here - https://jira.mongodb.org/browse/CSHARP-1727 

We've poked and prodded a lot, with no luck. Tried manually adding in the system.security.securestring dll and then the error moves onto this one:

Operation is not supported on this platform. (See inner exception for details.) ---> System.PlatformNotSupportedException: Operation is not supported on this platform.
at System.Runtime.InteropServices.OSPlatform.get_Windows()
at MongoDB.Driver.Core.Connections.ClientDocumentHelper.CreateOSDocument()
at System.Lazy`1.CreateValue()
--- End of stack trace from previous location where exception was thrown ---
at System.Lazy`1.get_Value()
at MongoDB.Driver.Core.Connections.ClientDocumentHelper.CreateClientDocument(String applicationName)
at MongoDB.Driver.Core.Connections.BinaryConnectionFactory..ctor(ConnectionSettings settings, IStreamFactory streamFactory, IEventSubscriber eventSubscriber)
at MongoDB.Driver.Core.Configuration.ClusterBuilder.BuildCluster()
at MongoDB.Driver.ClusterRegistry.CreateCluster(ClusterKey clusterKey)
at MongoDB.Driver.ClusterRegistry.GetOrCreateCluster(ClusterKey clusterKey)
at MongoDB.Driver.MongoClient..ctor(MongoClientSettings settings)
at Brafton.Alerts.Data.MongoDbContext..ctor(String connectionString)


A little googling found just this - http://transistordawn.com/2017/05/08/resolving-platformnotsupportedexception-using-mongodb-on-aws-lambda/ - clearly others have been where we have been, tried the addition of the assembly mentioned - but no change, same error.

It feels as though there is something in the .net core side of the Mongo Driver that's not quite right for pure .net core linux (non-windows) operation. 

Is anyone able to provide any advise / insight?

This is our test CSProj:
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup Label="Configuration" Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <OutputType>exe</OutputType>
  </PropertyGroup>

  <PropertyGroup>
    <TargetFramework>netcoreapp1.0</TargetFramework>
    <OutputTypeEx>exe</OutputTypeEx>
  </PropertyGroup>

  <ItemGroup>
    <Content Include="appsettings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
  </ItemGroup>  
  
  <ItemGroup>

    <PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.0.1" />

    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.Routing" Version="1.0.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.0.0" />
    <PackageReference Include="Microsoft.Extensions.Logging" Version="1.0.0" />
    <PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.0.0" />

    <PackageReference Include="Amazon.Lambda.Core" Version="1.0.0" />
    <PackageReference Include="Amazon.Lambda.Serialization.Json" Version="1.1.0" />
    <PackageReference Include="Amazon.Lambda.AspNetCoreServer" Version="0.10.1-preview1" />
    <PackageReference Include="Amazon.Lambda.Logging.AspNetCore" Version="1.1.0" />
    <PackageReference Include="MongoDB.Driver" Version="2.3.0" />
  </ItemGroup>

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

</Project>


And all we're doing to try and connect to mongo is as follows, in a quick test controller:

[HttpGet]
        public IActionResult Get()
        {

            var mongoUrl = new MongoUrl("mongodb://full-mongo-url-to-instance-and-database-name");
            var client = new MongoClient(mongoUrl);
            var db = client.GetDatabase(mongoUrl.DatabaseName);
            var cols = db.ListCollections();
            var first = cols.FirstOrDefault();
            return Ok(first);
        }

Cheers
Stewart

Wan Bachtiar

unread,
Oct 11, 2017, 3:33:42 AM10/11/17
to mongodb-user

Is anyone able to provide any advise / insight?

Hi Stewart,

It’s been a while since you posted this question, have you found the answer to your question yet ?

I’ve tried to replicate the issue using same csproj dependencies version as you’ve posted.

  • netcoreapp1.0
  • Amazon.Lambda.Core 1.0.0
  • Amazon.Lambda.Serialization.Json 1.1.0
  • Amazon.Lambda.AspNetCoreServer 0.10.1-preview1
  • Amazon.Lambda.Logging.AspNetCore 1.1.0
  • Amazon.Lambda.Tools 1.6.0 (Also tried with latest version of 1.8.0)
  • MongoDB.Driver 2.3.0

The local ASP NET Core WEB API worked without the SecureString error trace.
The test was a local execution (Kestrel) of lambda.AspNetCoreWebAPI AWS Blueprints, running on Ubuntu 16.04 (docker). 

Did you run this locally ? or only when published and execute on AWS Lambda ? If you run this locally, what's your OS ? 

Regards,
Wan.

Reply all
Reply to author
Forward
0 new messages