appsettings.json 文件不在 .net 核心控制台项目中 [英] appsettings.json file not in .net core console project

查看:42
本文介绍了appsettings.json 文件不在 .net 核心控制台项目中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 .net core 已经用 appsetting.json 替换了 app.config 文件.然而,这个文件似乎只为 ASP.net 项目添加.事实上,它甚至在添加项目列表中都不可用.我找到了这个发布需要添加的列表包:

<块引用>

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

我添加了所有这些,它确实为我提供了添加 json 配置文件的选项,但仍然没有添加仅在 ASP.Net Core 下可用的 App Settings File.我试图了解为什么非 Web 项目不需要配置以及配置 .net 核心控制台应用程序的推荐方法是什么.提前致谢.

解决方案

非网络项目可能可能不需要需要配置.但是,正如您所注意到的,Visual Studio 不会使用 appsettings.json 来构建控制台项目.显然,您可以将其作为 json 文件添加到项目中.一旦你拥有它,挑战就是如何使用它.我经常在实体框架实用程序中使用配置对象和依赖项注入.

例如

公共静态类程序{私有静态 IConfigurationRoot 配置 { 获取;放;}public static void Main(){IConfigurationBuilder builder = new ConfigurationBuilder().AddJsonFile("appsettings.json").AddEnvironmentVariables();配置 = builder.Build();IServiceCollection 服务 = new ServiceCollection();services.AddDbContext(options =>options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));services.AddScoped();IServiceProvider provider = services.BuildServiceProvider();IMyService myService = provider.GetService();myService.SomeMethod();}公共类 TemporaryDbContextFactory : IDesignTimeDbContextFactory{公共 MyDbContext CreateDbContext(string[] args){IConfigurationBuilder configBuilder = new ConfigurationBuilder().AddJsonFile("appsettings.json").AddEnvironmentVariables();IConfigurationRoot 配置 = configBuilder.Build();DbContextOptionsBuilderbuilder = new DbContextOptionsBuilder();builder.UseSqlServer(configuration.GetConnectionString("DefaultConnection"));返回新的 MyDbContext(builder.Options);}}}

这允许我针对 DbContext 运行迁移基于控制台的实用程序.您没有指定您需要什么样的配置 - 所以这只是一个例子.但希望您可以根据自己的需要进行调整.

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.

解决方案

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.

这篇关于appsettings.json 文件不在 .net 核心控制台项目中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆