Windows server 2012 下 Asp.Net core 实现部署

  1. Windows Server的相关配置, 主要是iis等配置。

Windows server 2012 下 Asp.Net core 实现部署

Windows server 2012 下 Asp.Net core 实现部署

Windows server 2012 下 Asp.Net core 实现部署


Windows server 2012 下 Asp.Net core 实现部署


Windows server 2012 下 Asp.Net core 实现部署


Windows server 2012 下 Asp.Net core 实现部署




Windows server 2012 下 Asp.Net core 实现部署

  1. 网站部署, 下面采用的是文件夹部署的方式

Windows server 2012 下 Asp.Net core 实现部署

点击Advanced...
Windows server 2012 下 Asp.Net core 实现部署


Windows server 2012 下 Asp.Net core 实现部署


  1. web api有2中host方式: console 和IIS
因为部署时选择的是框架依赖型的部署, 即需要把.net core runtime提前安装到目标机器上, 然后只copy自己项目用到的dll即可。优点是包小,缺点是目标机器必须安装开发时用到的.net core 版本或更高。

        a. console host
        
把文件部署产生的dll 复制到目标机器,直接用dotnet命令行启动web工程的dll, 比如:

dotnet UIH.SCWebApi.Web.dll

因为默认.net core webapi 启动的端口用于是5000, 我们实际需要改动端口和ip。这个时候就需要改动web工程的program.cs
public static void Main(string[] args)
        {
            var config = new ConfigurationBuilder()
                .AddCommandLine(args)
                .Build();
            var host = new WebHostBuilder()
                .UseConfiguration(config)
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>()
                .Build();
            host.Run();
        }

上面高亮部分就是添加命令行方式指定ip和port。使用时如下:
dotnet UIH.SCWebApi.Web.dll server.urls=http://*:9999

启动效果
Windows server 2012 下 Asp.Net core 实现部署

        b. IIS Host

需要注意的是IIS部署需要依赖.net core web host 框架, 而这个框架在.net core sdk中没有, 只在.net core runtime 中包含。 参考:https://docs.microsoft.com/zh-cn/aspnet/core/host-and-deploy/iis/index?view=aspnetcore-2.1&tabs=aspnetcore2x#install-the-net-core-hosting-bundle

需要注意的是:在x64机器上装.net core runtime时, 管理员权限启动命令行传参  OPT_NO_X86=1 并运行安装程序。

接下来就是IIS网站的常规操作了
1. 打开IIS管理器
2. 点击“应用程序池”
3. 点击“添加应用程序池”, 更改.net clr 版本 为 “无托管代码”, 保存退出
4. 点击“网站”
5. 点击“添加网站", ”应用程序池” 选择第3步创建的, “物理路径”为deploy复制过来的文件夹, 其他输入框按照自己情况输入
6. 此时点击浏览, 应该就可以看到正确的网站了。
需要注意的是:必须确保.net core runtime已经安装,然后aspnetcoreModule就会存在,可以去“C:\windows\System32\inetsrv”目录下查看aspnetcore.dll是否存在。 

asp.net core webapi IIS托管的方式,背后还是转发到了console托管了。 IIS通过查看目录文件下的web.config, 比如:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <!--
    Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380
  -->
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
    </handlers>
    <aspNetCore processPath="dotnet" arguments=".\UIH.SCWebApi.Web.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false" />
  </system.webServer>
</configuration>
<!--ProjectGuid: a2213374-bb48-48fd-bbd4-81e6a961d866-->

可以看到iis还是i通过dotnet命令启动了dll

  1. 如果服务启动后无法看到nswagger 的ui, 关闭防火墙试试。

Windows server 2012 下 Asp.Net core 实现部署



  1. 如果需要远程调试代码, 需要安装Remote Debugger。
a. 去自己vs的安装目录里可以看到remote debugger文件夹, 然后复制到目标机器上。比如:
D:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\Remote Debugger
b.在目标机器上管理员权限启动上述文件夹下的msvmon.exe
c.点击”工具/选型“, 把”身份验证模式“改为”无身份验证“
Windows server 2012 下 Asp.Net core 实现部署
d. 在开放机上, vs打开源码, 按下”ctrl + alt + p" , 会打开Attach to Process窗口
选择Remote,输入目标机器的ip和调试端口, 回车后就会看到目标机器上的进程信息
选择dotnet.exe程序
Windows server 2012 下 Asp.Net core 实现部署

点击“Attach”后, 目标机器的调试程序会显示已连接
Windows server 2012 下 Asp.Net core 实现部署




参考: