如何在 Core 2.0 的 ConfigurationBuilder 中设置基本路径。
我用谷歌搜索并找到了 this 问题、来自 Microsoft 文档的 this 以及在线 2.0 文档,但他们似乎使用的是
1.0.0-beta8中的
Microsoft.Extension.Configuration
版本。
我想读书
appsettings.json
。 Core 2.0 中有新的方法可以做到这一点吗?
using System;
using System.IO;
using Microsoft.Extensions.Configuration;
namespace ConsoleApp2
{
class Program
{
public static IConfigurationRoot Configuration { get; set; }
static void Main(string[] args)
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory()) // <== compile failing here
.AddJsonFile("appsettings.json");
Configuration = builder.Build();
Console.WriteLine(Configuration.GetConnectionString("con"));
Console.WriteLine("Press a key...");
Console.ReadKey();
}
}
}
appsetting.json
{
"ConnectionStrings": {
"con": "connection string"
}
}
更新: 除了添加 Microsoft.Extensions.Configuration.FileExtensions(如下所示 Set 所示)之外,我还需要添加 Microsoft.Extensions.Configuration.Json 来获取
AddJsonFile
扩展。
SetBasePath
扩展方法在Config.FileExtensions中定义。
您需要添加对 Microsoft.Extensions.Configuration.FileExtensions 包的引用。
AddJsonFile
,请添加对 Microsoft.Extensions.Configuration.Json
包的引用。
我正在使用 Visual Studio 2017 v15.5 开发 .NET Core 2 控制台应用程序。正如其他人所指出的,添加 Microsoft.Extensions.Configuration 后,我需要添加 Microsoft.Extensions.Configuration.Json 才能使
AddJsonFile()
调用正常工作。这也使得 SetBasePath()
调用起作用;所以我不需要添加Configuration.FileExtensions。 (我的代码无论有没有它都可以编译和运行。)
我还调用了
AddEnvironmentVariables()
,为此我需要添加 Configuration.EnvironmentVariables。我的代码如下:
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory()) // requires Microsoft.Extensions.Configuration.Json
.AddJsonFile("appsettings.json") // requires Microsoft.Extensions.Configuration.Json
.AddEnvironmentVariables(); // requires Microsoft.Extensions.Configuration.EnvironmentVariables
Configuration = builder.Build();
有趣的是,我唯一需要的
using
声明是using Microsoft.Extensions.Configuration
。
同时使用“Microsoft.Extensions.Configuration”和“Microsoft.Extensions.Configuration.Json”这将解决问题。
https://www.nuget.org/packages/Microsoft.Extensions.Configuration/ https://www.nuget.org/packages/Microsoft.Extensions.Configuration.Json/
这是示例“ConnectionFactory”
using System.Data;
using System.Data.SqlClient;
using Microsoft.Extensions.Configuration;
using System.IO;
namespace DataAccess.Infrastructure
{
public class ConnectionFactory : IConnectionFactory
{
public ConnectionFactory()
{
var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json");
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
public IDbConnection GetConnection
{
get
{
var connectionString = Configuration.GetConnectionString("DefaultConnection");
var conn = new SqlConnection(connectionString);
conn.Open();
return conn;
}
}
#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
// TODO: dispose managed state (managed objects).
}
// TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
// TODO: set large fields to null.
disposedValue = true;
}
}
// TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
// ~ConnectionFactory() {
// // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
// Dispose(false);
// }
// This code added to correctly implement the disposable pattern.
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(true);
// TODO: uncomment the following line if the finalizer is overridden above.
// GC.SuppressFinalize(this);
}
#endregion
} }
对于那些在使用 .NET 8 时偶然发现这一点的人。
您需要以下软件包:
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
</ItemGroup>
以及以下使用语句:
using Microsoft.Extensions.Configuration;
现在在您的program.cs中您可以:
// Build the configuration
var configuration = new ConfigurationBuilder()
.SetBasePath(Environment.CurrentDirectory)
.AddJsonFile($"appsettings.{environment}.json", optional: false) // Use the specified environment's file
.Build();
更多信息见本文。