Skip to content

bazel run for web sdk does not load appsettings.json #526

@chinwobble

Description

@chinwobble

I have modified this example slightly to print out the contents of the json file

Example:
https://github.com/bazel-contrib/rules_dotnet/blob/master/examples/aspnetcore/Program.cs

Modified code

// Program.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Data;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Reflection.Metadata;
using System.Runtime.InteropServices.Marshalling;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();

Console.WriteLine("print log level");
Console.WriteLine(builder.Configuration["Logging:LogLevel:Default"]);

var app = builder.Build();

app.UseAuthorization(); 

app.MapControllers();

app.Run("http://localhost:8000");

If you run this with bazel like this:

cd examples
bazel run //aspnetcore:aspnetcore

then the second console.writeline will print an empty string. i.e.

INFO: Running command line: bazel-bin/aspnetcore/aspnetcore/net8.0/aspnetcore.dll.sh
print log level

Note the last line is blank.

if instead you run

cd examples/aspnetcore
dotnet run

It will correctly print the contents of the json file

$ dotnet run
print log level
Error

I can see the appsettings.json is copied into the output dir.

Target //aspnetcore:aspnetcore up-to-date:
  bazel-bin/aspnetcore/aspnetcore/net8.0/aspnetcore.dll
  bazel-bin/aspnetcore/aspnetcore/net8.0/aspnetcore.xml
  bazel-bin/aspnetcore/aspnetcore/net8.0/appsettings.json
  bazel-bin/aspnetcore/aspnetcore/net8.0/appsettings.Development.json

perhaps the shell script has the wrong working directory?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions