【WebAPI No.2】如何WebAPI发布

介绍:

Asp.Net Core在Windows上能够采用两种运行方式。一种是自托管运行,另外一种是发布到IIS托管运行。html

自托管

首先有一个无缺的.Net Core WebAPI测试项目,而后进入根目录运行   dotnet publish  ,来进行编译:json

 

而后在进入dll目录,也就是程序集目录:运行当前项目的主程序dll: dotnet  xxx.dllide

出现上面状况就是完成了,发布在了5000端口;测试

验证看一下:ui

 修改默认端口:

.NET Core WebAP默认的端口号是5000,可是咱们能够经过配置来修改端口号。url

第一步:建立hosting.json文件:spa

{
  "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();
        }
View Code

还有一个就是若是出现一些与:CoreApi.deps.json相关的错误,解决办法:code

 

找到这个文件而后在里面添加: <PublishWithAspNetCoreTargetManifest>false</PublishWithAspNetCoreTargetManifest>server

<?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();

        }
View Code

而后在命令行输入:

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

 传送门

WebApi系列文章目录介绍

相关文章
相关标签/搜索