SQL files do not load when using ASP.NET Core
Closed this issue · 0 comments
MrDave1999 commented
Example code to reproduce the problem
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
var result = new YeSqlLoader().LoadFromDirectories("./yesql");
var app = builder.Build();
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
The following exception is thrown when the application is executed:
System.AggregateException: 'One or more errors occurred. (./yesql: error: No such directory exists.)
Given the following project structure
├── SqlFiles/
│ ├── bin/
│ │ └── Debug/
│ │ └── netstandard2.0/
│ │ └── yesql/
│ │ └── Reports/
│ │ ├── order.sql
│ │ └── orderDetails.sql
│ ├── Reports/
│ │ ├── order.sql
│ │ └── orderDetails.sql
│ └── SqlFiles.csproj
└── WebApi/
├── bin/
│ └── Debug/
│ └── net8.0/
│ └── yesql/
│ ├── product.sql
│ └── Reports/
│ ├── order.sql
│ └── orderDetails.sql
├── product.sql
├── WebApi.csproj
└── Program.cs
For a strange reason, the current directory is the root directory (where the WebApi.csproj project file is located). Of course, in that directory there is no yesql folder, that's why it doesn't find it.
The current directory should be WebApi/bin/Debug/net8.0
. This should be the expected behavior.
Instead, I change the code to:
var path = Path.Combine(AppContext.BaseDirectory, "yesql");
var result = new YeSqlLoader().LoadFromDirectories(path);
There if the error disappears, since AppContext.BaseDirectory
returns WebApi/bin/Debug/net8.0
.