Asp.net core 2.0 .NET Core 2.0控制台应用程序未编译
我想配置一个.NET Core 2.0控制台应用程序。有一行无法编译(请参阅代码中的注释) 我查看了很多博客、网站等。这似乎是一种被认可的配置.NET核心应用程序的方法。我是否缺少程序集引用Asp.net core 2.0 .NET Core 2.0控制台应用程序未编译,asp.net-core-2.0,Asp.net Core 2.0,我想配置一个.NET Core 2.0控制台应用程序。有一行无法编译(请参阅代码中的注释) 我查看了很多博客、网站等。这似乎是一种被认可的配置.NET核心应用程序的方法。我是否缺少程序集引用 using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using System; using System.I
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.IO;
namespace fbs.Console
{
class Program
{
public static IConfiguration configuration;
public static IServiceProvider serviceProvider;
static void Main(string[] args)
{
// Create service collection
ConfigureServices();
var dbService = serviceProvider.GetRequiredService<IDbContext>();
}
private static void ConfigureServices()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
configuration = builder.Build();
var appSettings = new AppSettings();
var section = configuration.GetSection("AppSettings");
IServiceCollection services = new ServiceCollection();
services.AddOptions();
// The following statement does NOT compile???
services.Configure<AppSettings>(section);
services.AddSingleton<IDbContext, DbContext>();
serviceProvider = services.BuildServiceProvider();
}
}
public class AppSettings
{
public string Database { get; set; }
}
public interface IDbContext
{
string Database { get; set; }
}
public class DbContext : IDbContext
{
public string Database { get; set; }
public DbContext(IOptions<AppSettings> appSettings)
{
this.Database = appSettings.Value.Database;
}
}
}
使用Microsoft.Extensions.Configuration;
使用Microsoft.Extensions.DependencyInjection;
使用Microsoft.Extensions.Options;
使用制度;
使用System.IO;
命名空间fbs.Console
{
班级计划
{
公共静态i配置;
公共静态IServiceProvider服务提供商;
静态void Main(字符串[]参数)
{
//创建服务集合
配置服务();
var dbService=serviceProvider.GetRequiredService();
}
私有静态void配置服务()
{
var builder=new ConfigurationBuilder()
.SetBasePath(目录.GetCurrentDirectory())
.AddJsonFile(“appsettings.json”,可选:true,reloadOnChange:true);
configuration=builder.Build();
var appSettings=新的appSettings();
var section=configuration.GetSection(“AppSettings”);
IServiceCollection服务=新的ServiceCollection();
services.AddOptions();
//下面的语句不编译???
服务。配置(部分);
services.AddSingleton();
serviceProvider=services.BuildServiceProvider();
}
}
公共类应用程序设置
{
公共字符串数据库{get;set;}
}
公共接口上下文
{
字符串数据库{get;set;}
}
公共类DbContext:IDbContext
{
公共字符串数据库{get;set;}
公共数据库上下文(IOptions appSettings)
{
this.Database=appSettings.Value.Database;
}
}
}
编辑:
编译器错误显示:
Error CS1503 Argument 2: cannot convert from 'Microsoft.Extensions.Configuration.IConfigurationSection' to 'System.Action<fbs.Console.AppSettings'.
错误CS1503参数2:无法从“Microsoft.Extensions.Configuration.IConfigurationSection”转换为“System.Action”已解决:
事实上,我一直没有在项目中添加Microsoft.Extensions.Options.ConfigurationExtensions
。我不知道这个程序集是必要的,因为似乎没有必要将它添加为“using”子句。至少,我的代码现在只是通过添加带有NuGet的包进行编译(但不更改using语句)。我有点困惑 请更新您的问题以向我们提供更多信息:标题不描述问题,提供错误消息,插入一些指向您访问的博客和网站的链接。@FIL:您是对的。很抱歉。不管怎样,问题解决了(见我的编辑)。谢谢你的回复!在我看来,这是微软在核心部分滥用过多的扩展的缺点之一IServiceCollection.Configure
存在于该命名空间之外,但不执行扩展方法所执行的操作。除非您显式地包含扩展名称空间,否则它基本上没有使用正确的方法,并且您会得到一个异常。问题是扩展有点太神奇了,VisualStudio并没有让您知道需要添加一个using名称空间,甚至需要添加哪个名称空间。