-
Notifications
You must be signed in to change notification settings - Fork 494
Description
Describe the bug
When using Microsoft.Extensions.Configuration.Json version 7.0.0 and specifying a path for a JSON file, like so
var logConfig = new ConfigurationBuilder()
.AddJsonFile("logging.json")
.Build();logging.json is not found, even when present in the output directory.
Instead of searching ..\bin\Debug\net6.0\logging.json it uses %USERPROFILE%\.dotnet\tools\.store\amazon.lambda.testtool-6.0\0.13.0\amazon.lambda.testtool-6.0\0.13.0\tools\net6.0\any\logging.json.
If specifying appsettings.json in the AddJsonFile() method, the appsettings.json file from the Mock Lambda Test tool is injected into logConfig instead of the one supplied with the Lambda project.
Expected Behavior
I expect logging.json to be parsed from ..\bin\Debug\net6.0\logging.json and become part of the logConfig configuration as mentioned above.
Current Behavior
logging.json is serached for in %USERPROFILE%\.dotnet\tools\.store\amazon.lambda.testtool-6.0\0.13.0\amazon.lambda.testtool-6.0\0.13.0\tools\net6.0\any\logging.json, is not found, and logConfig remains empty.
Reproduction Steps
- Have the AWS Toolkit installed in Visual Studio
- Create a new solution using the
AWS Lambda Project (.NET Core - C#)template - Choose the
Empty FunctionBlueprint - Add the
Microsoft.Extensions.Configuration.Jsonversion7.0.0NuGet package - Add
using Microsoft.Extensions.Configuration;to the top ofFunction.cs - Copy and paste this line of code into the
FunctionHandler
var logConfig = new ConfigurationBuilder()
.AddJsonFile("logging.json", false)
.Build();- Execute the project using the Mock Lambda Test Tool - You can use
""for the function input.
This will throw an exception saying it can't find logging.json, which is correct since we didn't add the file. However, the error message will show the path it tried to search for the file. Instead of searching ..\AWSLambda1\AWSLambda1\bin\Debug\net6.0\logging.json it uses %USERPROFILE%\.dotnet\tools\.store\amazon.lambda.testtool-6.0\0.13.0\amazon.lambda.testtool-6.0\0.13.0\tools\net6.0\any\logging.json.
Possible Solution
I believe the issue might be in this area. Even though AppBaseCompilationAssemblyResolver(Path.GetDirectoryName(rootAssemblyPath)) uses the correct directory, debugging into .NET source shows that DependencyContext of the assemblyResolver points to the wrong location. This is still a educated guess though, I wasn't able to dig too much further as of yet.
Lines 124 to 130 in 630d1d3
| this.assemblyResolver = new CompositeCompilationAssemblyResolver | |
| (new ICompilationAssemblyResolver[] | |
| { | |
| new AppBaseCompilationAssemblyResolver(Path.GetDirectoryName(rootAssemblyPath)), | |
| new ReferenceAssemblyPathResolver(), | |
| new PackageCompilationAssemblyResolver() | |
| }); |
Additional Information/Context
A workaround does exist. Adding .SetBasePath(Environment.CurrentDirectory) to the ConfigurationBuilder will cause the configuration system to use the correct path.
AWS .NET SDK and/or Package version used
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
Targeted .NET Platform
.NET 6
Operating System and version
Windows 10