3

I understand that .net core has replaced the app.config file with appsetting.json. However this file seems to be added for ASP.net projects only. In fact it is not even available in the add items list. I found this post that list packages needed to be added:

  1. Microsoft.Extensions.Configuration
  2. Microsoft.Extensions.Configuration.FileExtensions
  3. Microsoft.Extensions.Configuration.Json

I added all these and it does give me the option of adding a json configuration file but still not the App Settings File which is only available under ASP.Net Core. I am trying to understand why, doesn't a non web project need configuration and what is the recommended way to configure a .net core console application. Thanks in advance.

1 Answer 1

8

Non-web project may or may not need configuration. But, as you noticed, Visual Studio doesn't scaffold console projects with appsettings.json. Obviously, you can add it to the project as json file. Once you have it, the challenge is to make use of it. I frequently use Configuration object and dependency injection in Entity Framework utilities.

For example,

public static class Program
{
    private static IConfigurationRoot Configuration { get; set; }

    public static void Main()
    {
        IConfigurationBuilder builder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .AddEnvironmentVariables();
        Configuration = builder.Build();

        IServiceCollection services = new ServiceCollection();
        services.AddDbContext<MyDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        services.AddScoped<IMyService, MyService>();
        IServiceProvider provider = services.BuildServiceProvider();

        IMyService myService = provider.GetService<IMyService>();
        myService.SomeMethod();
    }

    public class TemporaryDbContextFactory : IDesignTimeDbContextFactory<MyDbContext>
    {
        public MyDbContext CreateDbContext(string[] args)
        {
            IConfigurationBuilder configBuilder = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json")
                .AddEnvironmentVariables();
            IConfigurationRoot configuration = configBuilder.Build();
            DbContextOptionsBuilder<MyDbContext> builder = new DbContextOptionsBuilder<MyDbContext>();
            builder.UseSqlServer(configuration.GetConnectionString("DefaultConnection"));
            return new MyDbContext(builder.Options);
        }
    }
}

This allows me to both run migrations and console-based utilities against DbContext. You don't specify what kind of configuration you are going to need - so this is just one example. But hopefully, you can adjust it to your needs.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks Felix. So this is the way it is. I will go ahead and add the a file named appsettings.json and expect that DXN will treat it just as the framework appsetting.json file. You guessed correctly that I am writing a non UI, database based program and I am working with linq2DB library for which I have to initialize the LinqToDB.Data.DataConnection() object.
However the same problem as I was expecting. I can add a file with this name. But the runtime throws a FileNotFoundException because although the file exists in the project, the runtime did not consider it the official file and did not copy it into to the runtime path (bin/Debug/netcoreapp2.2) where it is now looking for it.
Also on my machine the AddEnvironmentVariables() method is not part of the IConfigurationBuilder interface. I had to comment this line out.
yes, you need to copy the file manually (at least I don't know how to put the copying into the pipeline, although there probably is a way). If you are using environment variables, then add Microsoft.Extensions.Configuration.EnvironmentVariables. If not - just remove the call
To avoid the FileNotFoundException make sure you change the 'Copy to Output Directory' property of the file to 'Copy always' or 'Copy if newer'. This will copy the file to the runtime path.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.