【问题标题】:VS2015 ASP.NET 5 Empty project doesn't work, can't find ConfigurationBuilder.BuildVS2015 ASP.NET 5 空项目不工作,找不到ConfigurationBuilder.Build
【发布时间】:2015-09-25 19:58:40
【问题描述】:

试用新的 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"
  ]
}

更新:在基于 cmets 将 beta5s 更改为 beta7s 后,我克服了这个 ConfigurationBuilder 错误并转到“无法加载文件或程序集” Microsoft.Dnx.Host.Clr' 或其依赖项之一”错误,我怀疑这是另一个版本问题,我现在正在调查......但我想这将是一个不同的问题。

【问题讨论】:

  • 你用的是什么版本? Beta7?
  • 看起来 OP 正在使用 beta3,可能有重大的 API 更改。
  • 对不起,我应该发布 project.json。我现在就这样做。
  • @Henk,基于该评论,我将“beta5”行更改为“beta7”,保存,此错误消失了。现在我有另一个错误嘿。我想这就是进步。
  • 如果某处有冲突/依赖性检查以提醒这样的组合可能会导致问题,那就太好了......尽管管理该图表可能会很困难。

标签: c# asp.net-core dnx


【解决方案1】:

正如 Henk Mollema 在 cmets 中所指出的,没有找到 ConfigurationBuilder.Build 并且我的 Configuration 行是红色的原因是我有混合包版本(一些 beta5 和一些 beta7)。此功能是在 beta5 之后的版本中添加的,或者由于库不匹配而无法找到。在project.json 中升级到 beta7 修复了它。

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

【讨论】:

    【解决方案2】:

    你在这里遗漏了一些东西。假设您使用的是 beta7 并且您想要一个 config.json 文件:

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

    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.Json (1.0.0-beta7)。

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

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

    【讨论】:

    • 您知道 Startup() 的可能签名是否记录在任何地方?我在网上找到的几乎每个文档都使用不同的文档。到目前为止,这种方式确实令人沮丧。
    • @Josh 构造函数可以包含一些可以注入的运行时组件。 ConfigureServices() 只能有一个 IServiceCollection 参数。 Configure() 应该包含一个 IApplicationBuilder 参数和任何其他可注入类型。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-02
    • 2016-07-21
    • 1970-01-01
    • 1970-01-01
    • 2016-01-06
    • 2021-05-28
    • 1970-01-01
    相关资源
    最近更新 更多