VS2015 ASP.NET 5 Empty project doesn't work, can't find ConfigurationBuilder.Build

不问归期 提交于 2019-12-12 02:27:55

问题


Trying out the new VS2015 projects. I was following this tutorial but it's missing something. When I run it I get a 500 error:

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?

After some reading I discovered that you need a Configuration instance, though the examples I found don't work for me. They all set the instance to a Configuration field but that doesn't seem to exist. These Configuration lines are all red because the name doesn't exist:

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" });
        });
    }
}

This is using the default Startup class from the empty project. I added the gulp and angular Node modules manually. I must be missing something but I have no idea what...

Update: These are my usings in 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;

Update: This is my current project.json file (I haven't modified this manually yet):

{
    "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"
  ]
}

Update: After changing the beta5s to beta7s, based on comments, I got past this ConfigurationBuilder error and moved on to a "Could not load file or assembly 'Microsoft.Dnx.Host.Clr' or one of its dependencies" error, which I suspect is another version issue and I'm looking into now...but that would be a different question I suppose.


回答1:


As noted in comments by Henk Mollema, the reason ConfigurationBuilder.Build was not found and my Configuration lines were red is that I had mixed package versions (some beta5 and some beta7). This functionality was either added in a version later than beta5, or it couldn't be found due to the mismatched libraries. Upgrading to beta7 in project.json fixed it.

The subsequent error I mentioned was fixed as well.




回答2:


You are missing a few things here. Assuming you are on beta7 and you want a config.json file:

First build an IConfiguration instance in the Startup constructor:

private IConfiguration Configuration { get; }

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

    Configuration = configurationBuilder.Build();
}

Don't forget to add the Configuration property to your class. Also, you need Microsoft.Framework.Configuration.Json (1.0.0-beta7) for this.

Now in the ConfigureServices method you can bind settings from the. For example:

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

    // ...
}


来源:https://stackoverflow.com/questions/32396520/vs2015-asp-net-5-empty-project-doesnt-work-cant-find-configurationbuilder-bui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!