2017-05-27 90 views
0

linux,採用了最新的dotnetcore第一次嘗試在dotnetcore ASP.Net:WebHostBuilder錯誤

[[email protected] dot-net-core-centos-7-master]$ dotnet --version 
1.0.4 
[[email protected] dot-net-core-centos-7-master]$ 

我得到這個簡單的應用程序的erron。我錯過了什麼?

的.csproj文件

[[email protected] 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> 
[[email protected] dot-net-core-centos-7-master]$ 

project.json

[[email protected] 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的

[[email protected] 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

[[email protected] 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!"); 
      }); 
     } 
    } 
} 

[[email protected] dot-net-core-centos-7-master]$ 

當我嘗試運行它,我得到一個運行時錯誤:

[[email protected] 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. 
[[email protected] dot-net-core-centos-7-master]$ ls 
+0

你運行的是什麼版本的dotnet? 'dotnet --version' – kimbaudi

+0

@kimbaudi 1.0.4 – Ivan

+0

好的只是想知道。我想知道爲什麼你有'.csproj'文件和'project.json'文件。也許你從'project.json'遷移到'.csproj'。我認爲你應該先將'project.json'備份到某個地方,然後從你的web應用程序中刪除它。我會發佈一個答案,我希望能幫助你。 – kimbaudi

回答

2

我認爲問題在於這樣一個事實:你在你的.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就好。

+0

謝謝你的工作。 – Ivan