IConfiguration对象为null。

时间:2018-02-07 09:03:04

标签: asp.net-core asp.net-core-2.0

我正在尝试从appsettings.json读取连接字符串,我正在使用:

services.AddSingleton(Configuration);

启动时此行抛出null。我对core2.0很新。有人能说出我错过的东西吗?

我的创业公司:

public class Startup
{
    public static string ConnectionString { get; private set; }
    public IConfiguration Configuration { get; }
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddSingleton(Configuration);
        services.AddSingleton<IConfiguration>(Configuration);
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseMvc();
    }
}

我的控制器:

public class CreateController : Controller
{
    public IConfiguration _ConnectionString;
    public CreateController(IConfiguration configuration)
    {
        _ConnectionString = configuration;
    }

    public IEnumerable<string> Get()
    {
        Markets();
    }

    public string Markets()
    {
        using(SqlConnection con = new SqlConnection(_ConnectionString.GetSection("Data").GetSection("ConnectionString").Value))
        {
            return con.Database;
        }
    }
}

3 个答案:

答案 0 :(得分:4)

我注意到你的Startup缺少一个构造函数。在ASP.NET Core 2中,当它调用startup(基于vanilla WebHost.CreateDefaultBuilder(args).UseStartup<Startup>()中的典型Program.BuildWebHost)时会自动将配置传递给Startup构造函数:

public class Startup
{
    public IConfiguration Configuration { get; }

    // This configuration is automatic, if WebHost.CreateDefaultBuilder(args) is used in Program.cs
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

只需添加即可解决您的IConfiguration是空问题。

现在,您不应该将该配置添加到DI中,因为默认情况下它应该已经在那里,您可以按原样将其添加到控制器构造函数中。但是,这样做没有坏处。

最后,要加入合唱,直接在控制器中使用IConfiguration不是一个好主意。而是研究强类型配置设置。有一些教程可以提供帮助 - 这是我发现的the first link - 但要点是你的控制器最终会看起来像这样:

public class CreateController : Controller
{
    public ConnectionStrings _ConnectionStrings;
    public CreateController(IOptions<ConnectionStrings> connectionStrings)
    {
        _ConnectionStrings = connectionStrings.Value;
        ...

答案 1 :(得分:0)

您不应该在services.AddSingleton(Configuration)中致电ConfigureServices。它默认已经在DI容器中。

您只需在Controler

中引用它即可
public class CreateController : Controller
{

    public IConfiguration _configuration;
    public CreateController(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    public IEnumerable<string> Get()
    {
        Markets();
    }

    public string Markets()
    {
        var connectionString = _configuration.GetConnectionString("ConnectionStringName");
        using( SqlConnection con = new SqlConnection(connectionString) )
        {
            return con.Database;
        }

    }
}

答案 2 :(得分:0)

它为空,因为它尚未设置。您需要首先构建配置,最好在构造函数中完成。正如其他人所指出的那样,不建议这样做。

实施例

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

        if (env.IsDevelopment())
        {
            builder.AddUserSecrets<Startup>();
        }

        Configuration = builder.Build();
    }

    private IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton(Config);            
    }
}