Launch IIS Express to run ASP.NET Core Apps
我希望通过使用命令行从IIS Express中启动ASP.NET Core App来运行它。
我偶然发现这篇文章
So in fact Visual Studio silently adds the two environment variables
when launching IIS Express, so that ASP.NET Core related bits can be
injected.LAUNCHER_ARGS: -debug -p"C:\\Program Files\\dotnet\\dotnet.exe" -a"exec
\\"C:\\Users\\lextm\\documents\\visual studio
2017\\Projects\\WebApplication2\\WebApplication2\\bin\\Debug\
etcoreapp1.0\\WebApplication2.dll\\""
-pidFile"C:\\Users\\lextm\\AppData\\Local\\Temp\\2\\tmpFD6D.tmp" -wd"C:\\Users\\lextm\\documents\\visual studio
2017\\Projects\\WebApplication2\\WebApplication2"
让我知道是否有更简便的方法来启动IIS Express,以使用命令行或Powershell脚本运行ASP.NET Core Apps。
您正在寻找
我目前正在使用以下PowerShell脚本运行.NET Core 2.0应用程序:
1 2 3 | $env:LAUNCHER_ARGS ="-p""<path to dotnet.exe>"" -a""exec \""<path to webapp main dll>\"""" -pidFile $([System.IO.Path]::GetTempFileName()) -wd""<path to webapp root folder>"" -pr <project name>" $env:LAUNCHER_PATH ="<path to VSIISExeLauncher.exe>" &"<path to iisexpress.exe>" /config:"<path to applicationhost.config>" /site:"<webapp name>" |
占位符(尖括号中的文本)必须填充相应的值。您可以通过从Visual Studio运行项目并使用Process Explorer检查iisexpress.exe进程的环境变量来找到它们,如上面提供的链接中所示。
在.NET Core 3中,此问题的解决方案已更改。跟着这些步骤。
1)现在,环境变量应为:
1 2 3 | LAUNCHER_ARGS=exec"C:\\YourWebApiProject\\bin\\Debug\ etcoreapp3.1\\YourWebApiProject.dll" LAUNCHER_PATH=C:\\Program Files\\dotnet\\dotnet.exe |
将第一个路径更改为您的dll路径,并确保该路径中的.NET版本正确。请注意,不再需要创建临时文件。
2)确保两个模块AspNetCoreModule和AspNetCoreModuleV2分别在文件
在
1 |
在
1 | <section name="aspNetCore" overrideModeDefault="Allow" /> |
在
1 |
对位于
3)请注意您使用的是32位还是64位IIS Express。 (如果您使用的是64位计算机,则32位IIS Express = C:\ Program Files(x86)\ IIS Express,64位= C:\ Program Files \ IIS Express)对于我而言,32位以前可以正常工作但是在迁移到.NET Core 3之后,我必须使用64位,否则上述模块将无法加载。
我需要一次轻松地运行多个.Net Core API端点,而不必为每个端点都打开打开的Visual Studio。我最终在这里使用答案来构建以下内容:
iisaspnet.bat:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | @echo off :: Args are like: :: MobileApi :: C:\\Users\\chris\\Dropbox\\Code\\2017\\VbaMeasureHcp\\src\\HCPMobileApi :: MobileApi\\bin\\Debug\ etcoreapp2.2\\MobileApi.dll :: .vs\\MobileApp\\config\\applicationhost.config setlocal set PATH=%PATH%;C:\\Program Files\\IIS Express set LAUNCHER_ARGS=exec %2\\%3 set LAUNCHER_PATH=C:\\Program Files\\dotnet\\dotnet.exe iisexpress /site:%1 /config:"%2\\%4" :: Comment out line below to check for errors exit |
第一个arg是项目的名称-Visual Studio中的名称(在某些情况下,人们会流氓并为其命名Project文件,而在项目本身则为另一件事-在这种情况下,您想要的是Project名称,而不是文件名)。
第二个arg是第三个和第四个args的根文件夹。
第三个参数是在哪里找到已编译的Project DLL
第四个参数是在哪里找到applicationhost.config,它解释了如何启动站点。正如您将在下面看到的那样,通常可以在.vs文件夹中找到该文件夹??,但是存在的地方可能会有些疯狂,这取决于人们如何组织他们的Solution和Project文件夹。通常,.vs文件夹与.sln文件位于同一文件夹中,因此它可能离Project文件夹/文件较远。
这将不太有用,但是这里是启动IIS Express窗口的批处理文件,因此您可以看到iisaspnet.bat的使用情况:
1 2 3 4 5 6 7 8 9 10 11 12 | start iisaspnet.bat MobileApi C:\\Users\\chris\\Dropbox\\Code\\2017\\VbaMeasureHcp\\src\\HCPMobileApi MobileApi\\bin\\Debug\ etcoreapp2.2\\MobileApi.dll .vs\\MobileApp\\config\\applicationhost.config start iisaspnet.bat HCP.MasterDataApi C:\\Users\\chris\\Dropbox\\Code\\2017\\VbaMeasureHcp\\src MasterData\\Api\\HCP.MasterDataApi\\bin\\Debug\ etcoreapp2.2\\HCP.MasterDataApi.dll Solutions\\.vs\\MasterData\\config\\applicationhost.config start iisaspnet.bat HCP.SecurityApi C:\\Users\\chris\\Dropbox\\Code\\2017\\VbaMeasureHcp\\src Security\\Api\\HCP.SecurityApi\\bin\\Debug\ etcoreapp2.2\\HCP.SecurityApi.dll Solutions\\.vs\\Security\\config\\applicationhost.config start iisaspnet.bat HCP.BillingApi C:\\Users\\chris\\Dropbox\\Code\\2017\\VbaMeasureHcp\\src Billing\\Api\\HCP.BillingApi\\bin\\Debug\ etcoreapp2.2\\HCP.BillingApi.dll Solutions\\.vs\\Billing\\config\\applicationhost.config start iisaspnet.bat HCP.ClientApi C:\\Users\\chris\\Dropbox\\Code\\2017\\VbaMeasureHcp\\src Client\\Api\\HCP.ClientApi\\bin\\Debug\ etcoreapp2.2\\HCP.ClientApi.dll Solutions\\.vs\\Client\\config\\applicationhost.config start iisaspnet.bat HCP.EmployeeApi C:\\Users\\chris\\Dropbox\\Code\\2017\\VbaMeasureHcp\\src Employee\\Api\\HCP.EmployeeApi\\bin\\Debug\ etcoreapp2.2\\HCP.EmployeeApi.dll Solutions\\.vs\\Employee\\config\\applicationhost.config |
如果项目,解决方案等的存储和命名方式更加一致,则参数的工作方式可能会更加简单,但是,这是我无法控制的现有解决方案集,它们的命名方式很分散,而且混乱无论如何,上面的内容对于理解如何调用这些命令可能会更有帮助。
结果是,运行一个命令为我启动了6个IIS Express命令窗口,请求被记录到每个窗口中,而我只需在每个窗口中键入Q即可杀死它们。