簡體   English   中英

我可以使用Microsoft Dotnet CLI構建ASP.NET Core Web應用程序嗎?

[英]Can I build ASP.NET Core web application using Microsoft 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/clihttp://dotnet.github.io/getting-started/

但是沒有找到它支持ASP.NET Core web app的任何細節?

我花了一個小時的時間來玩它,我得到了“歡迎頁面”。

正如我所懷疑的那樣,你試圖將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是,在我的案例中工作的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 ”端口並提供歡迎頁面。

您可能會收到有關多個入口點的錯誤消息,這是因為您在Program.csStartup.cs中都有一個Main方法。 只需刪除Program.cs

這應該作為切入點。

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

簡答

對於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主機層本身。

現在我不知道你的確切錯誤是什么,但這可能會有所幫助:

基本上,您需要將適當的導入添加到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沒有問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM