首页 > 解决方案 > Automatically setup a Asp.net Core developer environment with IIS hosting/debugging

问题描述

I have a Powershell script that builds and sets up several web applications on a developer machine. It is used when first setting up a development environment or when a developer wants to simply get the latest version of everything and make it available on their machine. Usually to work on one particular app, but having all the others available since they provide services to each other.

For "old" Asp.net applications, this is as simple as building the .csproj and pointing a site on the local IIS to the appropriate folder.

For our new .Net Core applications, it looked just as simple for a while. The script built the appropriate .csproj using Msbuild, and created and configured an AppPool and IIS Site pointing to the project directory using Powershell. The web.config file contains <aspNetCore processPath="bin\IISSupport\VSIISExeLauncher.exe" arguments="-argFile IISExeLauncherArgs.txt" /> to enable IIS hosting and debugging.

This approach worked on computers that had already run the .net Core apps in question. However, it turns out that bin\IISSupport\VSIISExeLauncher.exe is not created during the build by MsBuild, but only when launching the app for the first time within Visual Studio. This means that, even though everything is properly setup, new developers would still have to manually open each solution and launch each one of the installed apps manually from Visual Studio for their environment to be properly and entirely setup.

Has anyone had any luck automating this process ?

I know that developing .Net Core apps directly on the local IIS server is not a "preferred" scenario, but automatically installing and running multiple web apps that form part of a SOA on demand seems reasonable.

Last, but not least, I'd like to point out that we're trying to set up a development environment, where a developer is supposed to be able to open a solution in Visual Studio and the app(s) that it contains are ready to be debugged on the local IIS. So simply publishing the apps to a local site is not an ideal solution.

标签: powershellasp.net-coreiis.net-core

解决方案


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

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

VSIISExeLauncher 垫片脚本:

# 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上找到。


推荐阅读