【问题标题】:Automatically setup a Asp.net Core developer environment with IIS hosting/debugging使用 IIS 托管/调试自动设置 Asp.net Core 开发人员环境
【发布时间】:2019-07-09 13:55:47
【问题描述】:

我有一个 Powershell 脚本,可以在开发人员机器上构建和设置多个 Web 应用程序。当首次设置开发环境或开发人员想要简单地获取所有内容的最新版本并使其在他们的机器上可用时使用它。通常在一个特定的应用程序上工作,但其他所有应用程序都可用,因为它们相互提供服务。

对于“旧”的 Asp.net 应用程序,这就像构建 .csproj 并将本地 IIS 上的站点指向适当的文件夹一样简单。

对于我们的新 .Net Core 应用程序,它在一段时间内看起来很简单。该脚本使用 Msbuild 构建了适当的 .csproj,并使用 Powershell 创建并配置了指向项目目录的 AppPool 和 IIS 站点。 web.config 文件包含 <aspNetCore processPath="bin\IISSupport\VSIISExeLauncher.exe" arguments="-argFile IISExeLauncherArgs.txt" /> 以启用 IIS 托管和调试。

这种方法适用于已经运行相关 .net Core 应用程序的计算机。但是,事实证明bin\IISSupport\VSIISExeLauncher.exe 不是在 MsBuild 构建期间创建的,而是仅在首次在 Visual Studio 中启动应用程序时创建的。这意味着,即使一切都已正确设置,新开发人员仍必须手动打开每个解决方案并从 Visual Studio 手动启动每个已安装的应用程序,才能正确且完整地设置他们的环境。

有没有人幸运地自动化了这个过程?

我知道直接在本地 IIS 服务器上开发 .Net Core 应用程序不是“首选”方案,但自动安装和运行构成 SOA 一部分的多个 Web 应用程序按需似乎是合理的。

最后但并非最不重要的一点是,我想指出,我们正在尝试建立一个 开发 环境,开发人员应该能够在 Visual Studio 中打开解决方案并且它包含的应用程序已准备好在本地 IIS 上进行调试。因此,简单地将应用程序发布到本地站点并不是一个理想的解决方案。

【问题讨论】:

    标签: powershell asp.net-core iis .net-core


    【解决方案1】:

    我一直在寻找类似的东西,并最终编写了一个脚本(如下)来生成所需的文件。它具有非常基本的 .csproj 文件支持,因此如果它不起作用,您可以提供 -WorkingDirectory-AssemblyPath 参数的绝对路径。它设置了运行站点所需的最低限度参数,而无需先打开 Visual Studio。一旦有人在 Visual Studio 中构建/开始调试,文件将被覆盖,并且调试将按预期进行。

    这使用VSSetup PowerShell 模块自动检测您安装的 Visual Studio 版本(适用于 2017 或更高版本)。

    VSIISExeLauncher shim 脚本:

    # This utilizes https://github.com/microsoft/vssetup.powershell
    # Quick setup:  Install-Module VSSetup -Scope CurrentUser
    
    param(
        [string] $WorkingDirectory = "",
        [string] $AssemblyPath = "",
        [string] $ProjectPath = "",
        [string] $Build = "Debug"
    )
    $ErrorActionPreference = "Stop"
    
    # Check that at parameter requirements are met
    if ((-not $WorkingDirectory -and -not $ProjectPath) -or (-not $AssemblyPath -and -not $ProjectPath)) {
        throw "Please specify the working directory and assembly path, or the project file name"
    }
    
    # Get assembly path and working directory information using .csproj file
    if ($ProjectPath) {
        $project = [xml] (Get-Content $ProjectPath -Raw)
        if ($project.Project.Sdk -ine "Microsoft.NET.Sdk.Web") {
            throw "$ProjectPath is not a Microsoft.NET.Sdk.Web project"
        }
    
        $TargetFramework = "$($project.Project.PropertyGroup.TargetFramework)".Trim()
        $RootNamespace = "$($project.Project.PropertyGroup.RootNamespace)".Trim()
        $WorkingDirectory = (Get-Item $ProjectPath).Directory.FullName
        $AssemblyPath = "$WorkingDirectory\bin\$Build\$TargetFramework\$RootNamespace.dll"
        if (-not (Test-Path $AssemblyPath)) {
            $AssemblyPath = "$WorkingDirectory\bin\$TargetFramework\$RootNamespace.dll"
    
            if (-not (Test-Path $AssemblyPath)) {
                throw "Could not find $AssemblyPath"
            }
        }
    }
    
    # Get the newest version of Visual Studio installed on this machine
    Import-Module VSSetup
    $VSSetup = (Get-VSSetupInstance | Sort-Object { $_.InstallationVersion } -Descending | Select-Object -First 1)
    
    # Build some path strings
    $VSIISExePath = "$($VSSetup.InstallationPath)\Common7\IDE\Extensions\Microsoft\Web Tools\ProjectSystem\VSIISExeLauncher.exe"
    $DotNetPath = (Get-Command dotnet).Source
    $IISSupportPath = "bin\IISSupport"
    
    # For debugging purposes
    Write-Host "Working Directory: $WorkingDirectory"
    Write-Host "Assembly Path:     $AssemblyPath"
    Write-Host "VSIISEXELauncher:  $VSIISExePath"
    Write-Host "DotNet:            $DotNetPath"
    
    # Create the support folder and copy needed files
    Push-Location $WorkingDirectory
    
    mkdir -Force $IISSupportPath | Out-Null
    Copy-Item $VSIISExePath $IISSupportPath
    
    @"
    -p $DotNetPath
    -pidFile $WorkingDirectory\$IISSupportPath\pidfile.txt
    -wd $WorkingDirectory
    -a exec `"$AssemblyPath`"
    -env ASPNETCORE_CONTENTROOT=$WorkingDirectory`0COMPLUS_ForceENC=1`0ASPNETCORE_ENVIRONMENT=Development`0
    "@ | Set-Content "$IISSupportPath\IISExeLauncherArgs.txt"
    
    # Show the list of files as proof the command completed
    Get-ChildItem $IISSupportPath
    
    Pop-Location
    

    IIS 站点创建:

    与上一段相关的旁注,如果您正在寻找设置 IIS 站点的方法,请查看WebAdministration PowerShell 模块。您可以轻松创建站点、设置其绑定、添加/配置应用程序池以及设置 web.config 选项。 IIS 设置还将在 PowerShell (IIS:\) 中显示为驱动器,以便使用 New-ItemGet-Item 等轻松访问设置。

    另外,请查看Carbon PowerShell 模块,该模块可用于轻松设置主机条目,而无需手动修改主机文件。它也可以在Chocolatey 上找到。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-03
      • 2018-03-18
      • 2018-11-22
      • 1970-01-01
      • 2022-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多