在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
答案 0 :(得分:3)
我认为问题在于您在Microsoft.NETCore.App
和.csproj
文件中引用了project.json
。查看我的一个.NET Core Web应用程序的.csproj
文件,我看不到对Microsoft.NETCore.App
的任何引用。因此,我建议您首先备份原始文件的副本,然后删除引用.csproj
的{{1}}和project.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
就好了。
答案 1 :(得分:0)
kimbaudi是对的。
在与地狱(Nuget)的paketmanager进行一些调整之后,我的程序会抛出一些常见的异常。我已经通过Nuget-Manager安装了“Microsoft.AspNetCore.Mvc”,但是已经删除了“Microsoft.AspNetCore”。重新安装后,一切正常。