我可以使用Microsoft Dotnet CLI构建ASP.NET Core Web应用程序吗?

时间:2016-02-20 18:16:43

标签: c# asp.net-core .net-core asp.net-core-1.0 dotnet-cli

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

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

Microsoft.CodeAnalysis.CSharp 1.1.0-rc1-20151109-01与DNXCore不兼容,版本= 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 app的任何细节?

3 个答案:

答案 0 :(得分:7)

我花了一个小时的时间来玩它,我得到了“欢迎页面”。

正如我所怀疑的那样,你尝试将dotnet-cli与你的dnx风格的项目一起使用,你使用了错误的nuget feed(官方的,而不是每晚的myget feed)。

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

Startup.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();
        }
    }
}

project.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名字对象。

最后但并非最不重要的是,在我的案例中有效的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”Feed后,所有包都已解析并且dotnet run正常工作。它将侦听“http://localhost:5000”端口并提供欢迎页面。

您可能会收到有关多个入口点的错误消息,这是因为MainProgram.cs都有Startup.cs方法。只需删除Program.cs即可。

这应该是一个切入点。

到目前为止,

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

答案 1 :(得分: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应用程序[s],还是仅限于控制台应用程序?

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

  • 不,您不能将dotnet CLI用于ASP.NET Core rc1 Web应用程序,因为dnx.exe加载了ASP.NET主机层。
  • 是的,您可以将它用于ASP.NET Core rc2 Web应用程序,因为您的控制台应用程序会加载ASP.NET托管层本身。

答案 2 :(得分: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 Code生成器生成.Net Web App

添加导入后,dotnet restore无问题地工作。