相关疑难解决方法(0)

如何在ASP.NET Core中的任何类中访问Configuration?

我已经完成了ASP.NET核心的配置文档.文档说您可以从应用程序的任何位置访问配置.

下面是模板创建的Startup.cs

public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

        if (env.IsEnvironment("Development"))
        {
            // This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
            builder.AddApplicationInsightsSettings(developerMode: true);
        }

        builder.AddEnvironmentVariables();
        Configuration = builder.Build();
    }

    public IConfigurationRoot Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container
    public void ConfigureServices(IServiceCollection …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core-mvc asp.net-core

102
推荐指数
7
解决办法
8万
查看次数

在C#类库中使用IConfiguration

我正在使用C#和Core .NET构建一个类库.我正在尝试使用config.json文件中的配置.以下是该文件的内容:

config.json

{
  "emailAddress":"someone@somewhere.com"
}
Run Code Online (Sandbox Code Playgroud)

为了尝试使用config.json我的配置,我Microsoft.Framework.ConfigurationModel.Json在我的project.json文件中引用.在我的代码中,我有以下内容:

MyClass.cs

using Microsoft.Framework.ConfigurationModel;
public class MyClass
{
  public string GetEmailAddress()
  {
//    return ConfigurationManager.AppSettings["emailAddress"];  This is the approach I had been using since .NET 2.0
    return ?;  // What goes here?
  }
}
Run Code Online (Sandbox Code Playgroud)

从.NET 2.0开始,我一直在使用ConfigurationManager.AppSettings["emailAddress"].但是,我现在正试图通过以下方式学习如何做到这一点IConfiguration.我的问题是,这是一个类库.因此,我不确定配置文件的加载方式,位置或时间.在传统的.NET中,我只需要为ASP.NET项目命名文件web.config,为其他项目命名app.config.现在,我不确定.我有一个ASP.NET MVC 6项目和一个XUnit项目.所以,我正在试图弄清楚如何使用config.json在这两种情况下使用.

谢谢!

c# .net-core asp.net-core

27
推荐指数
3
解决办法
4万
查看次数

.net core控制台应用程序强类型配置

在.NET Core Console应用程序上,我正在尝试将自定义appsettings.json文件中的设置映射到自定义配置类.

我在线查看了几个资源,但无法使.Bind扩展方法有效(我认为它适用于asp.net应用程序或以前版本的.Net Core,因为大多数示例都表明了这一点).

这是代码:

 static void Main(string[] args)
    {

        var builder = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);

        IConfigurationRoot configuration = builder.Build();

        //this is a custom configuration object
        Configuration settings = new Configuration();

        //Bind the result of GetSection to the Configuration object
        //unable to use .Bind extension
        configuration.GetSection("MySection");//.Bind(settings);

        //I can map each item from MySection manually like this
        settings.APIBaseUrl = configuration.GetSection("MySection")["APIBaseUrl"];

        //what I wish to accomplish is to map the section to my Configuration object
        //But …
Run Code Online (Sandbox Code Playgroud)

c# .net-core

7
推荐指数
2
解决办法
3481
查看次数

在独立的 .NET Core DLL 中,如何从 appsettings.json 配置文件中获取值?

编辑:如果 ASP.NET 控制器在控制台应用程序中运行,则从 ASP.NET 控制器传递配置将不起作用。

背景:
- DLL 将随不同的应用程序、一些控制台、一些 ASP.NET MVC 和一些 WebAPI 一起提供
- 我正在构建一个 .NET Core C# 处理 DLL,它需要 appsettings.json 中的多个配置条目。
- 为了简单起见,如果可能的话,我想让 DLL C# 方法全部静态。
- 示例配置条目为“SxProcFull”,值为 true 或 false。DLL 需要大约 10 个配置条目
- DLL 是更大代码库的一部分,从 Web 控制器方法或控制台应用程序的主方法向下调用多个级别

如何从 DLL 内部获取配置条目?

Web 上的大多数示例都是针对 ASP.NET 的,并且通过将配置入口代码放入控制器方法中并将配置服务传递到控制器方法中来过度简化。

.NET Core 文档详细介绍了存在的提供程序类型,但缺少实际示例。

相关链接:

了解控制台应用程序中的 .net Core 依赖注入
https://espressocoder.com/2018/12/03/build-a-console-app-in-net-core-like-a-pro/
https://blog。 bitscry.com/2017/05/30/appsettings-json-in-net-core-console-app/
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/?view=aspnetcore-3.0 &tabs=windows
还有更多...

编辑:将多应用程序类型项目移动到背景列表的顶部。

回答:

using System;
using System.IO;
using System.Reflection;
using Microsoft.Extensions.Configuration;

namespace AspNetCoreTestProject2
{
public static class MyConfiguration …
Run Code Online (Sandbox Code Playgroud)

c# console-application .net-core

6
推荐指数
1
解决办法
4680
查看次数