关于c#:如何在Core 2.0中的ConfigurationBuilder中设置SetBasePath
https://www.codenong.com/46843367/
How to SetBasePath in ConfigurationBuilder in Core 2.0
如何在Core 2.0的ConfigurationBuilder中设置基本路径。
我已经在Google上搜索并找到了这个问题,这个问题来自Microsoft文档以及在线的2.0文档,但是他们似乎正在使用1.0.0-beta8中的
我想阅读
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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
1 2 3 4 5 | { "ConnectionStrings": { "con":"connection string" } } |
更新:
除了按照Set指示的那样添加Microsoft.Extensions.Configuration.FileExtensions,我还需要添加Microsoft.Extensions.Configuration.Json来获取
- UPDATE完成了工作!
-
Microsoft.Extensions.Configuration.Json 依赖于Microsoft.Extensions.Configuration.FileExtensions ,因此您只需要前者。 -
朋友,请注意,
AddJsonFile("appsettings.json") 方法不知道您的托管环境! 使用.AddJsonFile($"appsettings.{_hostingEnvironment.EnvironmentN ame}.json") 代替。 :D
您需要添加对Microsoft.Extensions.Configuration.FileExtensions包的引用。
要解析
- 但是现在找不到" AddJsonFile"方法。 :D我还必须添加此程序包:" Microsoft.Extensions.Configuration.Json"。
-
Microsoft.Extensions.Configuration.Json 依赖于Microsoft.Extensions.Configuration.FileExtensions ,因此您只需要前者。
我正在使用Visual Studio 2017 v15.5开发一个.NET Core 2控制台应用程序。正如其他人指出的那样,在添加Microsoft.Extensions.Configuration之后,我需要添加Microsoft.Extensions.Configuration.Json来使
我还呼叫了
1 2 3 4 5 | 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语句应该是Microsoft.Extensions.Configuration。 但是,必须通过"管理NuGet包"项目选项显式添加NuGet依赖项" Microsoft.Extensions.Configuration.Json"。 其依赖项是" Microsoft.Extensions.Configuration.FileExtensions",因此将作为依赖项连接的一部分包括在内。 这两个依赖关系将解决" SetBasePath"和" AddJsonFile"编译错误。
- 值得指出的是,它不再可用作Mac预览版的VS设置当前目录为bin / netcoreapp2x
-
不是学究的,而是
Configuration.EnvironmentVariables ==Microsoft.Extensions.Configuration.EnvironmentVariables 。 有几件名字相似的东西。
同时使用" 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"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | 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 } } |
标签:AddEnvironmentVariables,Core,SetBasePath,Json,报错,AddJsonFile,Extensions,Configur From: https://www.cnblogs.com/sunny3158/p/17033874.html