VS2015 ASP.NET 5 空项目不起作用,找不到 ConfigurationBuilder.Build
VS2015 ASP.NET 5 Empty project doesn't work, can't find ConfigurationBuilder.Build
试用新的 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?
经过一些阅读,我发现您需要一个 Configuration 实例,尽管我找到的示例对我不起作用。他们都将实例设置为 Configuration 字段,但似乎不存在。这些配置行都是红色的,因为名称不存在:
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" });
});
}
}
这是使用空项目中的默认启动 class。我手动添加了 gulp 和 angular 节点模块。我一定是遗漏了什么,但我不知道是什么...
更新:这些是我在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
s 更改为 beta7
s 后,根据评论,我克服了这个 ConfigurationBuilder
错误并移动关于 "Could not load file or assembly 'Microsoft.Dnx.Host.Clr' or one of its dependencies" 错误,我怀疑这是另一个版本问题,我现在正在调查...但我想那将是一个不同的问题。
您在这里遗漏了一些东西。假设您使用的是 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
属性 添加到您的 class。此外,为此你需要 Microsoft.Framework.Configuration.Json
(1.0.0-beta7
)。
现在您可以在 ConfigureServices
方法中绑定设置。例如:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<AppSettings>(Config.GetSection("AppSettings"));
// ...
}
正如 Henk Mollema 在评论中指出的那样,未找到 ConfigurationBuilder.Build
并且我的 Configuration
行是红色的原因是我有混合包版本(一些 beta5 和一些 beta7)。此功能要么是在 beta5 之后的版本中添加的,要么由于库不匹配而找不到。在 project.json
中升级到 beta7 修复了它。
我提到的后续错误是。
试用新的 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?
经过一些阅读,我发现您需要一个 Configuration 实例,尽管我找到的示例对我不起作用。他们都将实例设置为 Configuration 字段,但似乎不存在。这些配置行都是红色的,因为名称不存在:
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" });
});
}
}
这是使用空项目中的默认启动 class。我手动添加了 gulp 和 angular 节点模块。我一定是遗漏了什么,但我不知道是什么...
更新:这些是我在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
s 更改为 beta7
s 后,根据评论,我克服了这个 ConfigurationBuilder
错误并移动关于 "Could not load file or assembly 'Microsoft.Dnx.Host.Clr' or one of its dependencies" 错误,我怀疑这是另一个版本问题,我现在正在调查...但我想那将是一个不同的问题。
您在这里遗漏了一些东西。假设您使用的是 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
属性 添加到您的 class。此外,为此你需要 Microsoft.Framework.Configuration.Json
(1.0.0-beta7
)。
现在您可以在 ConfigureServices
方法中绑定设置。例如:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<AppSettings>(Config.GetSection("AppSettings"));
// ...
}
正如 Henk Mollema 在评论中指出的那样,未找到 ConfigurationBuilder.Build
并且我的 Configuration
行是红色的原因是我有混合包版本(一些 beta5 和一些 beta7)。此功能要么是在 beta5 之后的版本中添加的,要么由于库不匹配而找不到。在 project.json
中升级到 beta7 修复了它。
我提到的后续错误是