c# - AWS Lambda、.Net Core 和 MySql : Could not load file or assembly 'System. Diagnostics.TraceSource,版本 = 4.0.0.0

标签 c# mysql amazon-web-services .net-core aws-lambda

我正在构建通过 RDS 中的 MySql 访问数据的 lambda 微服务。我的本地单元测试工作正常,但是当我发布到 AWS 时,出现以下错误:

    {
  "TypeName": "MySql.Data.MySqlClient.MySqlTrace",
  "Message": "The type initializer for 'MySql.Data.MySqlClient.MySqlTrace' threw an exception.",
  "Data": {},
  "InnerException": {
    "Message": "Could not load file or assembly 'System.Diagnostics.TraceSource, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.\n",
    "FileName": "System.Diagnostics.TraceSource, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
    "Data": {},
    "InnerException": {
      "Message": "'System.Diagnostics.TraceSource, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' not found in the deployment package or in the installed Microsoft.NETCore.App.",
      "FileName": null,
      "Data": {},
      "InnerException": null,
      "StackTrace": "   at AWSLambda.Internal.Bootstrap.LambdaAssemblyLoadContext.Load(AssemblyName assemblyName)\n   at System.Runtime.Loader.AssemblyLoadContext.ResolveUsingLoad(AssemblyName assemblyName)\n   at System.Runtime.Loader.AssemblyLoadContext.Resolve(IntPtr gchManagedAssemblyLoadContext, AssemblyName assemblyName)",
      "HelpLink": null,
      "Source": "Bootstrap",
      "HResult": -2147024894
    },
    "StackTrace": null,
    "HelpLink": null,
    "Source": null,
    "HResult": -2147024894
  },
  "StackTrace": "   at MySql.Data.MySqlClient.MySqlTrace.LogError(Int32 id, String msg)\n   at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()\n   at MySql.Data.MySqlClient.MySqlPool.GetConnection()\n   at MySql.Data.MySqlClient.MySqlConnection.Open()\n   at HealthStats.Functions.GetLocationTypes(APIGatewayProxyRequest request, ILambdaContext context)",
  "HelpLink": null,
  "Source": "MySql.Data",
  "HResult": -2146233036
}

这是我的 project.json 文件。我尝试将 System.Diagnostics.TraceSource 库添加为标准项目依赖项(未显示)和框架依赖项(如下所示)。我的想法可能是在发布期间它没有添加程序集,因为我没有在我的代码中直接使用 TraceSource。但是,这两种尝试都没有解决问题:

{
  "version": "1.0.0-*",
  "buildOptions": {
    "emitEntryPoint": false
  },

  "dependencies": {
    "Amazon.Lambda.APIGatewayEvents": "1.0.1",
    "Amazon.Lambda.Core": "1.0.0",
    "Amazon.Lambda.Serialization.Json": "1.0.1",
    "Amazon.Lambda.Tools": {
      "type": "build",
      "version": "1.1.0-preview1"
    },
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.0.0"
    },
    "MySql.Data": "7.0.6-IR31"
  },

  "tools": {
    "Amazon.Lambda.Tools" : "1.1.0-preview1"
  },

  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "System.Diagnostics.TraceSource": "4.0.0"
      },
      "imports": [
        "dnxcore50",
        "portable-net45+win8"
      ]
    }
  }
}

有什么想法吗?

最佳答案

我在 lambda 结果中看到了类似的问题:

The type initializer for 'MySql.Data.MySqlClient.MySqlTrace' threw an exception.: TypeInitializationException at MySql.Data.MySqlClient.MySqlTrace.LogError(Int32 id, String msg) at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver() at MySql.Data.MySqlClient.MySqlPool.GetConnection() at MySql.Data.MySqlClient.MySqlConnection.Open() at AWSLambda2.Function.FunctionHandler(SNSEvent input, ILambdaContext context) at lambda_method(Closure , Stream , Stream , ContextInfo )

'System.Diagnostics.TraceSource, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' not found in the deployment package or in the installed Microsoft.NETCore.App.: FileNotFoundException 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)

对我有用的是使用 dotnet publish 手动发布项目,然后在发布的文件夹中导航到 .\publish\runtimes\unix\lib\netstandard1.3并将在那里发布的 dll 复制到 .\publish。将它们放入该文件夹后,我压缩了发布文件夹的内容(不包括运行时子文件夹),并将其上传到 lambda。

我对为什么会发生这种情况的工作理论是因为 dotnet publish 正在发布,就好像智能系统正在使用结果一样,但是我认为 lambda 正在将程序集加载到另一个不知道在子文件夹中查找依赖项的上下文。

关于c# - AWS Lambda、.Net Core 和 MySql : Could not load file or assembly 'System. Diagnostics.TraceSource,版本 = 4.0.0.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41794621/

相关文章:

c# - 为什么 wpf 复选框绑定(bind)可能无法工作?

c# - 读取ISO 8859 1编码,用webclient下载xml流

Mysql从连接结果中删除

java - 数据库在等待空闲可用连接时超时

mysql - MySql版本如何学习?

linux - amazon web service -EC2 linux vm 没有显卡或共享显卡

c# - 如何让 VS 假定所有内容默认都是公开的

c# - 数据绑定(bind) Entity Framework 导航属性 - 处理更改

java - 配置为 s3 通知处理程序时如何在 aws lambda 函数中给出错误响应

ssh - 为 AWS EC2 实例创建私钥