首先尝试 dotnetcore ASP.Net: WebHostBuilder 错误

First try at a dotnetcore ASP.Net: WebHostBuilder error

linux下,使用最新的dotnetcore

[idf@localhost dot-net-core-centos-7-master]$ dotnet --version
1.0.4
[idf@localhost dot-net-core-centos-7-master]$ 

我在这个简单的应用程序上遇到错误。我错过了什么?

.csproj 文件

[idf@localhost dot-net-core-centos-7-master]$ more dot-net-core-centos-7-master.csproj 
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Sdk">
      <Version>1.0.0-alpha-20161104-2</Version>
      <PrivateAssets>All</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.NET.Sdk.Web">
      <Version>1.0.0-alpha-20161104-2</Version>
      <PrivateAssets>All</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.NETCore.App">
      <Version>1.1.0</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.AspNetCore.Mvc">
      <Version>1.1.0</Version>
    </PackageReference>
  </ItemGroup>
</Project>
[idf@localhost dot-net-core-centos-7-master]$

project.json:

[idf@localhost dot-net-core-centos-7-master]$ more project.json
{
  "version": "1.0.0-*",
  "buildOptions": {
    "emitEntryPoint": true
  },
  "dependencies": {
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.0.0-rc2-3002702"
    },
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final"
  },
  "frameworks": {
    "netcoreapp1.0": {
      "imports": "dnxcore50"
    }
  },
}

Program.cs

[idf@localhost dot-net-core-centos-7-master]$ more Program.cs 
using System;
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;


namespace aspnetcoreapp
{
    public class Program
    {
        public static void Main(string[] args)
        {                
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseStartup<Startup>()
                .UseUrls("http://*:80/")
                .Build();

            host.Run();
        }
    }
}

Startup.cs

[idf@localhost dot-net-core-centos-7-master]$ more Startup.cs 

using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;

namespace aspnetcoreapp
{
    public class Startup
    {
        public void Configure(IApplicationBuilder app)
        {
            app.Run(context =>
            {
                return context.Response.WriteAsync("Hello from ASP.NET Core!");
            });
        }
    }
}

[idf@localhost dot-net-core-centos-7-master]$

当我尝试 运行 时,我收到 运行 时间错误:

[idf@localhost dot-net-core-centos-7-master]$ dotnet run
/home/idf/dotnet/sdk/1.0.4/Sdks/Microsoft.NET.Sdk/build/Microsoft.NET.Sdk.DefaultItems.targets(101,5): warning : A PackageReference for 'Microsoft.NETCore.App' was included in your project. This package is implicitly referenced by the .NET SDK and you do not typically need to reference it from your project. For more information, see https://aka.ms/sdkimplicitrefs [/home/idf/dotnet_progs/dot-net-core-centos-7-master/dot-net-core-centos-7-master.csproj]
Program.cs(14,28): error CS0246: The type or namespace name 'WebHostBuilder' could not be found (are you missing a using directive or an assembly reference?) [/home/idf/dotnet_progs/dot-net-core-centos-7-master/dot-net-core-centos-7-master.csproj]
Program.cs(20,18): error CS7036: There is no argument given that corresponds to the required formal parameter 'handler' of 'RunExtensions.Run(IApplicationBuilder, RequestDelegate)' [/home/idf/dotnet_progs/dot-net-core-centos-7-master/dot-net-core-centos-7-master.csproj]

The build failed. Please fix the build errors and run again.
[idf@localhost dot-net-core-centos-7-master]$ ls

我认为问题在于您在 .csprojproject.json 文件中引用了 Microsoft.NETCore.App。查看我的一个 .NET Core Web 应用程序的 .csproj 文件,我没有看到任何对 Microsoft.NETCore.App 的引用。所以我的建议是您首先在某处备份原始副本,然后删除 .csprojproject.json 文件中引用 Microsoft.NETCore.App 的行。我觉得很奇怪,你有一个 .csproj 文件和 project.json 文件。

我认为您的 .csproj 文件应该如下所示:

.csproj

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore" Version="1.1.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.0" />
  </ItemGroup>
</Project>

更新 .csproj 文件并安全删除 project.json 后,我会 dotnet clean 清理构建输出,然后 dotnet build 查看是否一切正常.如果是这样,你应该能够 dotnet run 就好了。

kimbaudi 是对的。

在对来自地狱 (Nuget) 的 paketmanager 进行一些摆弄之后,我的程序抛出了一些奇怪的异常。我通过 Nuget-Manager 安装了 "Microsoft.AspNetCore.Mvc",但奇怪的是 "Microsoft.AspNetCore" 被删除了。重新安装后一切正常。