首页 新闻 会员 周边 捐助

升级至ASP.NET 5 RC1后遇到的读取config.json问题

0
悬赏园豆:20 [已解决问题] 解决于 2015-11-19 10:10

之前在Startup.cs是这样读取config.json的:

public Startup(IApplicationEnvironment appEnv)
{
    Configuration = new Configuration(appEnv.ApplicationBasePath)
        .AddJsonFile("config.json");
}

升级至ASP.NET 5 RC1后,运行dnx kestrel出现下面的错误:

cannot convert from 'string' to 'Microsoft.Framework.ConfigurationModel.IConfigurationSource'
   at Microsoft.Dnx.Compilation.CSharp.RoslynProjectReference.Load(AssemblyName assemblyName, IAssemblyLoadContext loadContext)
   at Microsoft.Dnx.Compilation.CompilationEngine.LoadProject(Project project, FrameworkName targetFramework, String aspect, IAssemblyLoadContext loadContext, AssemblyName assemblyName)
   at Microsoft.Dnx.Runtime.Loader.ProjectAssemblyLoader.Load(AssemblyName assemblyName, IAssemblyLoadContext loadContext)
   at Microsoft.Dnx.Runtime.Loader.ProjectAssemblyLoader.Load(AssemblyName assemblyName)
   at Microsoft.Dnx.Host.LoaderContainer.Load(AssemblyName assemblyName)
   at Microsoft.Dnx.Host.DefaultLoadContext.LoadAssembly(AssemblyName assemblyName)
   at Microsoft.Dnx.Runtime.Loader.AssemblyLoaderCache.GetOrAdd(AssemblyName name, Func`2 factory)
   at Microsoft.Dnx.Runtime.Loader.LoadContext.Load(AssemblyName assemblyName)
dudu的主页 dudu | 高人七级 | 园豆:30925
提问于:2015-11-19 09:45
< >
分享
最佳答案
0

换用下面这行代码试试。

public IConfiguration Configuration { get; set; }
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
  var builder = new ConfigurationBuilder()
                  .SetBasePath(appEnv.ApplicationBasePath)
                  .AddJsonFile("config.json");
   Configuration = builder.Build();
}

 在新的ASP.net5中,读取配置文件的过程是:

1、在Setup类中,通过ConfigurationBuilder(配置相关的唯一入口)创建ConfigurationProvider(此例中被封装在了扩展方法“AddJsonFile中”)

2、通过“Build()方法对外提供IConfiguration接口”,该接口使业务逻辑不依赖于具体的ConfigurationProvider。

收获园豆:20
虾。 | 菜鸟二级 |园豆:257 | 2015-11-19 09:59

正解。还需要将命名空间由 using Microsoft.Framework.ConfigurationModel 改为 using Microsoft.Extensions.Configuration;

dudu | 园豆:30925 (高人七级) | 2015-11-19 10:09
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册