3

我浏览了一些网站,发现 .NET Core 正在使用.NET CLI(.NET 命令行界面)。我已经使用 .NET CLI 创建了一个控制台应用程序,它工作正常

但是,当我使用 Yeoman 创建 ASP.NET Core Web 应用程序并运行命令“dotnet restore”时,我收到以下错误:

Microsoft.CodeAnalysis.CSharp 1.1.0-rc1-20151109-01 与 DNXCore 不兼容,Version=v5.0。

我也试过

dotnet restore -s https://api.nuget.org/v3/index.json

但得到同样的错误。

如果我运行“dnu restore”那么它工作正常,所以我的问题是我可以将 .NET CLI 也用于 Web 应用程序还是仅限于控制台应用程序?

我已经浏览了链接

https://github.com/dotnet/cli & http://dotnet.github.io/getting-started/

但没有发现它是否支持 ASP.NET Core Web 应用程序的任何细节?

4

3 回答 3

6

我花了一个小时来玩弄它,并让“欢迎页面”运行起来。

正如我所怀疑的那样,您尝试将 dotnet-cli 与您的 dnx 样式项目一起使用,并且您使用了错误的 nuget 提要(官方提要,而不是每晚的 myget 提要)。

对于这个演示项目,只需创建一个新文件夹并运行dotnet new. 这将创建三个文件NuGet.Configproject.jsonProgram.cs。您可以删除后一个,然后Startup.cs从下面创建一个。

启动.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

namespace AspNetCoreCliDemo
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app)
        {
            app.UseIISPlatformHandler();
            app.UseWelcomePage();
        }

        // This doesn't work right now, as it can't resolve WebApplication type
        //public static void Main(string[] args) => WebApplication.Run<Startup>(args);

        // Entry point for the application.
        public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                        .UseDefaultConfiguration(args)
                        .UseServer("Microsoft.AspNetCore.Server.Kestrel")
                        .UseIISPlatformHandlerUrl()
                        .UseStartup<Startup>()
                        .Build();

            host.Run();
        }
    }
}

项目.json:

{
  "version": "1.0.0-*",
  "compilationOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "NETStandard.Library": "1.0.0-rc2-*",
    "Microsoft.AspNetCore.Diagnostics": "1.0.0-rc2-*",
    "Microsoft.AspNetCore.IISPlatformHandler": "1.0.0-rc2-*",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-*"
  },

  "frameworks": {
    "dnxcore50": { }
  },

  "exclude": [
    "wwwroot",
    "node_modules"
  ],
  "publishExclude": [
    "**.user",
    "**.vspscc"
  ]
}

注意:目前仅dnxcore支持 moniker。

最后但并非最不重要的NuGet.Config一点是,在我的情况下有效:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <!--To inherit the global NuGet package sources remove the <clear/> line below -->
    <clear />
    <add key="cli-deps" value="https://dotnet.myget.org/F/cli-deps/api/v3/index.json" />
    <add key="dotnet-core" value="https://dotnet.myget.org/F/dotnet-core/api/v3/index.json" />
    <add key="api.nuget.org" value="https://api.nuget.org/v3/index.json" />
  </packageSources>
</configuration>

我没有成功使用两个默认提要(api.nuget.org 和 dotnet-core),因为它无法解决一些依赖关系。添加“cli-deps”提要后,所有包都已解决并可以正常dotnet run工作。它将侦听“ http://localhost:5000 ”端口并提供欢迎页面。

您可能会收到有关拥有多个入口点的错误消息,这是因为您在和Main中都有一个方法。只需删除.Program.csStartup.csProgram.cs

这应该作为一个入口点。

dotnet-cli截至目前还不支持命令(以前在project.json文件中定义的命令)。

于 2016-02-20T20:51:13.243 回答
3

简答

对于 ASP.NET Core rc1,我们使用dnxdnu.

对于 ASP.NET Core rc2,我们使用dotnet.

更长的答案

我浏览了一些网站,发现 .NET Core 正在使用 .NET CLI(.NET 命令行界面)。我已经使用 .NET CLI 创建了一个控制台应用程序,它工作正常。

ASP.NET Core rc2 也是一个控制台应用程序。也就是说,它是一个启动 ASP.NET 宿主层的控制台应用程序。

但是,当我使用 Yeoman 创建 ASP.NET Core Web 应用程序并运行命令“dotnet restore”时,我收到以下错误...

我的感觉是您正在运行这些命令:

yo aspnet
? What type of application do you want to create? Web Application
? What's the name of your ASP.NET application? MyApp

这将创建一个 ASP.NET Core rc1 项目。您可以通过查看生成的 project.json 文件依赖项来了解这一点。他们都说rc1-final。结果:您无法使用dotnet命令行构建这些。您仍然必须使用dnu builddnx <command>公式。

...我也可以将 .NET CLI 用于 Web 应用程序,还是仅限于控制台应用程序?

在 ASP.NET Core rc1 中,dnx.exe调用了new WebHostBuilder(),因此我们需要使用它dnx来启动我们的 Web 应用程序。在 ASP.NET Core rc2 中,包含我们的 Web 应用程序的控制台应用程序自己调用new WebHostBuilder(),因此我们可以使用dotnet命令行界面来启动应用程序。所以...

  • 不,您不能将dotnetCLI 用于 ASP.NET Core rc1 Web 应用程序,因为dnx.exe会加载 ASP.NET 托管层。
  • 是的,您可以将它用于 ASP.NET Core rc2 Web 应用程序,因为您的控制台应用程序本身会加载 ASP.NET 托管层。
于 2016-02-21T00:53:07.767 回答
0

现在我不知道您的确切错误是什么,但这可能会有所帮助:

基本上,您需要将适当的导入添加到您的 project.json:

    "frameworks": {
        "dnxcore50": { 
            "imports": "portable-net451+win8"
        }
    }

(此处发布:https ://github.com/aspnet/Home/issues/1265 )

这些是我的错误:

Errors in /webserver/project.json
    Package Microsoft.CodeAnalysis.CSharp 1.1.0-rc1-20151109-01 is not compatible with dnxcore50 (DNXCore,Version=v5.0). Package Microsoft.CodeAnalysis.CSharp 1.1.0-rc1-20151109-01 supports:
      - net45 (.NETFramework,Version=v4.5)
      - portable-net45+win8 (.NETPortable,Version=v0.0,Profile=Profile7)
    Package Microsoft.CodeAnalysis.Common 1.1.0-rc1-20151109-01 is not compatible with dnxcore50 (DNXCore,Version=v5.0). Package Microsoft.CodeAnalysis.Common 1.1.0-rc1-20151109-01 supports:
      - net45 (.NETFramework,Version=v4.5)
      - portable-net45+win8 (.NETPortable,Version=v0.0,Profile=Profile7)
    One or more packages are incompatible with DNXCore,Version=v5.0.

我使用来自http://editor.swagger.io/#/的 Swagger 代码生成器生成了 .Net Web App

添加导入后,dotnet restore工作没有问题。

于 2016-07-11T13:07:28.310 回答