asp.net core WebAPI發布


介紹:

Asp.Net Core在Windows上可以采用兩種運行方式。一種是自托管運行,另一種是發布到IIS托管運行。

自托管

首先有一個完好的.Net Core WebAPI測試項目,然后進入根目錄運行   dotnet publish  ,來進行編譯:

 

然后在進入dll目錄,也就是程序集目錄:運行當前項目的主程序dll: dotnet  xxx.dll

出現上面情況就是完成了,發布在了5000端口;

驗證看一下:

 修改默認端口:

.NET Core WebAP默認的端口號是5000,但是我們可以通過配置來修改端口號。

第一步:創建hosting.json文件:

{
  "server.urls": "http://*:8001;http://*:8002;http://*:8003"
}

第二部讀取,並注冊:

復制代碼
     public static void Main(string[] args)
        {
            //BuildWebHost(args).Run();
            var config = new ConfigurationBuilder()
               .SetBasePath(Directory.GetCurrentDirectory())
               .AddJsonFile("hosting.json", optional: true)
               .Build();

            var host = new WebHostBuilder()
                .UseKestrel()
                .UseConfiguration(config)
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>()
                .UseApplicationInsights()
                .Build();

            host.Run();
        }
復制代碼

還有一個就是如果出現一些與:CoreApi.deps.json相關的錯誤,解決辦法:

 

找到這個文件然后在里面添加: <PublishWithAspNetCoreTargetManifest>false</PublishWithAspNetCoreTargetManifest>

復制代碼
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <PublishWithAspNetCoreTargetManifest>false</PublishWithAspNetCoreTargetManifest>
  </PropertyGroup>
</Project>
復制代碼

 注:如果只是修改一個端口不做多端口發布只需要這樣即可:

復制代碼
  public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
             .UseUrls("http://localhost:8002")
             .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .UseApplicationInsights()
            .Build();
復制代碼

 還有一種可以多次運行命令行來實現不同的端口:

首先Program.cs:

復制代碼
  public static IWebHost BuildWebHost(string[] args)
        {

            var config = new ConfigurationBuilder().AddCommandLine(args)
                .Build();
            string ip = config["ip"];
            string port= config["port"];
            return WebHost.CreateDefaultBuilder(args)
             .UseUrls($"http://{ip}:{port}")
             .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .UseApplicationInsights()
            .Build();

        }
復制代碼

然后在命令行輸入:

     dotnet CoreApi.dll  --ip 127.0.0.1 --port 8001


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM