我正在构建通过关系数据库中的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"
]
}
}
}有什么想法吗?
发布于 2017-01-26 06:55:43
我在lambda结果中看到了类似的问题:
Closure,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,流,流,ContextInfo )
在部署包或已安装的Microsoft.NETCore.App中找不到'System.Diagnostics.TraceSource,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a‘。:位于AWSLambda.Internal.Bootstrap.LambdaAssemblyLoadContext.Load(AssemblyName FileNotFoundException ) at System.Runtime.Loader.AssemblyLoadContext.Resolve(IntPtr assemblyName) at System.Runtime.Loader.AssemblyLoadContext.Resolve(IntPtr gchManagedAssemblyLoadContext,AssemblyName assemblyName)
对我来说起作用的是使用dotnet publish手动发布项目,然后在已发布的文件夹中导航到.\publish\runtimes\unix\lib\netstandard1.3,并将在那里发布的dlls复制到.\publish中。一旦这些文件放到该文件夹中,我压缩了发布文件夹的内容(不包括runtime子文件夹),并将其上传到lambda。
我的工作理论是,为什么会发生这种情况,是因为dotnet publish在发布时就好像智能系统正在使用结果一样,但是我认为lambda正在将程序集加载到另一个上下文中,而这个上下文不知道如何在子文件夹中查找依赖项。
发布于 2017-02-01 17:54:13
最简单的解决方案是编辑project.json文件。添加预发布事件的步骤
“脚本”:{ "prepublish":"xcopy AWS ./bin/Release/“,"postpublish":"dotnet publish-iis --publish-Folder%Publish:OutputPath% --framework %publish:FullTargetFramework%”}这会导致在./bin/Release/netcoreapp1.0/publish/runtimes/unix/libnetstandard1.3/System.Diagnostics.TraceSource.dll发布向导复制引用之前复制它。
问候
阿尔多·弗洛雷斯
发布于 2017-02-03 11:20:59
与@alduar类似,但我认为将其放在发布后更合适,因为dll在第一次构建时不会在那里。我认为它应该被复制到发布目录。请注意,这些文件路径是用于windows的。
"scripts": {
"postpublish": [ "xcopy bin\\Release\\netcoreapp1.0\\publish\\runtimes\\unix\\lib\\netstandard1.3\\System.Diagnostics.TraceSource.dll bin\\Release\\netcoreapp1.0\\publish\\" ]
},https://stackoverflow.com/questions/41794621
复制相似问题