I'm building a C# AWS Lambda package. How do I properly include System.Data.SqlClient.dll?

2.3k views Asked by At

I'm attempting to build a C# AWS Lambda package using System.Data.SqlClient.dll using .NET Core CLI.

In my C# Lambda Function I'm attempting to connect to an external SQL Server instance. In doing so, I have the 'using System.Data.SqlClient' statement within my code and make use of some of the classes therewithin.

AWS provides some examples on using 'dotnet restore' and 'dotnet publish' to build the package. After some work, these are working flawlessly with my project. After running publish, the publish directory structure has the following directory:

*\bin\Debug\netcoreapp1.0\publish\runtimes*

Containing directories 'unix', 'win', 'win7-x64', and 'win7-x86'.

Deeper inside of these directories, I find the module 'System.Data.SqlClient.dll' inside, e.g.:

\bin\Debug\netcoreapp1.0\publish\runtimes\unix\lib\netstandard1.3 \bin\Debug\netcoreapp1.0\publish\runtimes\win\lib\netstandard1.3

The problem is that AWS Lambda can't seem to find the module inside of this directory structure. When attempting to test the code I get the following error:

"errorMessage": "Could not load file or assembly 'System.Data.SqlClient, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.",

I haven't found any reference to this issue but would assume that I need to modify the directory structure to help AWS find the proper file. I imagine this would mean choosing the correct runtime module.

Am I on the right track? Does anyone have a better understanding and some pointers? C# Lambda functions are so new that I'm finding this incredibly difficult to find more information about.

Thanks in advance!

2

There are 2 answers

3
Norm Johanson On BEST ANSWER

I would recommend using the Amazon.Lambda.Tools package which integrates with the dotnet CLI to perform the packaging. Basically you add the NuGet package as tool and execute:

dotnet lambda package

There are other commands as well which you can discover by executing

dotnet lambda help

The package command helps some of the rough spots we have found with using just the dotnet publish command. For example the package command does some extra copying of files for the case of System.Data.SqlClient.

For more information on it check out the GitHub repository https://github.com/aws/aws-lambda-dotnet/tree/master/Libraries/src/Amazon.Lambda.Tools

1
RusArt On

I had same problem with System.Data.SqlClient.dll. Don't know why, but it sometimes doesn't copy to output directory after build. You can add this dll in project as file and set copy to output property to true.