VS2015 ASP.NET 5空项目不起作用,找不到ConfigurationBuilder.Build

时间:2015-09-04 11:03:15

标签: c# asp.net-core dnx

试用新的VS2015项目。我关注this tutorial,但它遗漏了一些东西。当我运行它时,我得到500错误:

System.MissingMethodException: Method not found: 'Microsoft.Framework.Configuration.IConfiguration Microsoft.Framework.Configuration.ConfigurationBuilder.Build()'.
at Microsoft.AspNet.Loader.IIS.RuntimeHttpApplication.ApplicationStart(IHttpApplication application)
at Microsoft.AspNet.Loader.IIS.HttpApplicationBase.InvokeApplicationStart(IHttpApplication application)

.NET Framework version 4.0.30319.42000   |   Microsoft.AspNet.Loader.IIS version 1.0.0-beta5-11951   |   IIS version 10.0.10117.0 (fbl_srv2_iis_dev(sujitn).150512-1839)   |   Need help?

经过一些阅读后,我发现你需要一个配置实例,虽然我找到的例子对我不起作用。它们都将实例设置为“配置”字段,但似乎不存在。这些配置行都是红色的,因为名称不存在:

public class Startup
{
    public Startup() {
        Configuration = new ConfigurationBuilder();
    }

    public void ConfigureServices(IServiceCollection services)
    {
        Configuration = new ConfigurationBuilder();
        services.AddMvc();
    }

    public void Configure(IApplicationBuilder app)
    {
        Configuration = new ConfigurationBuilder();

        app.Run(async (context) =>
        {
            await context.Response.WriteAsync("Hello World!");
        });

        app.UseMvc(m => {
            m.MapRoute(
                name: "default",
                template: "{controller}/{action}/{id?}",
                defaults: new { controller = "Home", action = "Index" });
        });
    }
}

这是使用空项目中的默认启动类。我手动添加了gulp和angular Node模块。我一定错过了什么,但我不知道是什么......

更新:这些是我在Startup.cs中的用法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Configuration;

更新:这是我当前的project.json文件(我尚未手动修改此文件):

{
    "webroot": "public",
    "version": "1.0.0-*",

    "dependencies": {
        "Microsoft.AspNet.Mvc": "6.0.0-beta7",
        "Microsoft.AspNet.Server.IIS": "1.0.0-beta5",
        "Microsoft.AspNet.Server.WebListener": "1.0.0-beta5"
    },

  "commands": {
    "web": "Microsoft.AspNet.Hosting --config hosting.ini"
  },

  "frameworks": {
    "dnx451": { },
    "dnxcore50": { }
  },

  "publishExclude": [
    "node_modules",
    "bower_components",
    "**.xproj",
    "**.user",
    "**.vspscc"
  ],
  "exclude": [
    "public",
    "node_modules",
    "bower_components"
  ]
}

更新:根据评论将beta5更改为beta7后,我收到了此ConfigurationBuilder错误并转到了“可能”没有加载文件或程序集'Microsoft.Dnx.Host.Clr'或其依赖项之一“错误,我怀疑是另一个版本问题,我现在正在研究......但我认为这将是一个不同的问题。< / p>

2 个答案:

答案 0 :(得分:2)

正如Henk Mollema的评论中指出的那样,找不到date而我的ConfigurationBuilder.Build行是红色的原因是我有混合包版本(一些beta5和一些beta7)。此功能要么在beta5之后的版本中添加,要么由于库不匹配而无法找到。在Configuration升级到beta7修复了它。

我提到的后续错误是fixed as well

答案 1 :(得分:1)

你在这里缺少一些东西。假设您使用的是beta7且需要config.json文件:

首先在IConfiguration构造函数中构建一个Startup实例:

private IConfiguration Configuration { get; }

public Startup(IApplicationEnvironment appEnv)
{
    var configurationBuilder = new ConfigurationBuilder(appEnv.ApplicationBasePath)
        .AddJsonFile("config.json")
        .AddEnvironmentVariables();

    Configuration = configurationBuilder.Build();
}

不要忘记将Configuration属性添加到您的班级。此外,您需要Microsoft.Framework.Configuration.Json1.0.0-beta7)。

现在,在ConfigureServices方法中,您可以绑定设置。例如:

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<AppSettings>(Config.GetSection("AppSettings"));

    // ...
}
相关问题