【问题标题】:Troubles deploying my API to Azure App Service将我的 API 部署到 Azure 应用服务时遇到问题
【发布时间】:2022-01-25 13:16:15
【问题描述】:

我正在 ASP.NET Core 中开发一个非常简单的 API 用于测试目的。

我在https://localhost:5678/api/Values/addition/{a}/{b} 有一个端点,它只是将数字 A 和 B 相加并将结果存储在数据库中。

数据库存储在 Azure 上。在本地,应用程序运行良好并成功连接到我的 Azure SQL Server 数据库。我已经对连接字符串进行了硬编码,以避免任何可能导致错误的复杂化。稍后我会更改它。

我还在 Azure 上创建了一个发布管道。首先,这里是azure-pipelines.yml

# ASP.NET Core (.NET Framework)
# Build and test ASP.NET Core projects targeting the full .NET Framework.
# Add steps that publish symbols, save build artifacts, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core

trigger:
- master

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    restoreSolution: '$(solution)'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:DesktopBuildPackageLocation="$(build.artifactStagingDirectory)\WebApp.zip" /p:DeployIisAppPath="Default Web Site"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

- task: VSTest@2
  inputs:
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'drop'
    publishLocation: 'Container'

只要在master 分支上检测到新推送,就会触发发布管道:

触发的工作非常简单,它有一个Run On Agent,它触发我的应用程序部署到Azure App Service,就像这样:

steps:
- task: AzureRmWebAppDeployment@4
  displayName: 'Deploy To Development'
  inputs:
    azureSubscription: '{MY SUBSCRIPTION ID}'
    appType: webAppLinux
    WebAppName: '{MY APP SERVICE NAME}'
    deployToSlotOrASE: true
    ResourceGroupName: '{MY RESOURCEGROUP NAME, SAME AS THE APP SERVICE}'

构建成功,所有作业都运行成功。

我现在可以导航到我的应用服务 url https://{MY APP SERVICE NAME}.azurewebsites.net 并获得 Azure 的欢迎页面:

我假设此时我应该能够导航到 https://{MY APP SERVICE NAME}.azurewebsites.net/api/Values/addition/{a}/{b} 并从我的 API 获得响应,但我得到的是错误 404,而不是它。API 端点不存在。

我已经对所有内容进行了硬编码,以避免无法访问外部服务等系统错误,我的日志流也是空的,最后我使用 kudu 来验证我的应用程序中是否有正确的web.config。我认为,所有内容都已发表得很好。

我在这里错过了什么?

编辑 1:

我已将我的管道修改如下:

# ASP.NET Core
# Build and test ASP.NET Core projects targeting .NET Core.
# Add steps that run tests, create a NuGet package, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core

trigger:
- master

pool:
  vmImage: ubuntu-latest

variables:
  buildConfiguration: 'Release'

steps:

- task: DotNetCoreCLI@2
  inputs:
    command: 'test'
    projects: '**/*Tests.csproj'

- script: dotnet build --configuration $(buildConfiguration)
  displayName: 'dotnet build $(buildConfiguration)'

- task: DotNetCoreCLI@2
  inputs:
    command: 'restore'
    projects: '**/*sln'
    feedsToUse: 'select'
    vstsFeed: '{MY FEED}'

- task: DotNetCoreCLI@2
  inputs:
    command: 'publish'
    publishWebProjects: false
    arguments: '--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)'
    zipAfterPublish: true
    modifyOutputPath: true

- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'drop'
    publishLocation: 'Container'

修改了我的应用服务wwwroot/,还是不行。

【问题讨论】:

  • 在 Azure 门户中的 Web 应用管理刀片上,转到诊断日志菜单项。打开应用程序日志记录、Web 服务器日志记录、详细错误消息和失败请求跟踪,您将能够进入诊断和解决问题菜单选项并查看失败请求跟踪日志。您还可以浏览 Web 服务器日志和应用程序日志以了解详细错误。
  • app.UseEndpoints(endpoints => { endpoints.MapControllers(); } ); ``` 另外,尝试在startup.cs的配置部分添加这些行
  • 我已经修改了我的管道,但它仍然无法正常工作。我在我的第一篇文章中创建了一个编辑。关于 app.UseEndpoints(),我已经有了。关于日志,我无法重现您描述的步骤。可以给我一些截图吗?
  • 尝试在 Azure Web 应用级别创建虚拟目录:social.msdn.microsoft.com/Forums/azure/en-US/…
  • 是 Linux 还是 Windows 操作系统,如何部署?

标签: azure asp.net-core azure-web-app-service


【解决方案1】:

我认为您实际上并未将 zip 推送到应用服务(这可以解释 404)。如果您使用 webAppLinux 类型(从 here 向下滚动一点),您正在使用的 AzureRmWebAppDeployment@4 任务需要 PackagepackageForLinux 参数,以便它知道要发布到您的应用服务的内容。

【讨论】:

  • 在您链接的文档中,我们可以阅读 Package 变量:“(如果 ConnectionType = PublishProfile 或 WebAppKind = webApp、apiApp、functionApp、mobileApp、webAppLinux 或 functionAppLinux,则为必需)文件路径到包,或包含由 MSBuild 生成的应用服务内容的文件夹,或压缩的 zip 或 war 文件。”另外,当我用 kudu 浏览服务器时,包解压得很好
  • 如果你用 kudu 浏览时它就在那里,那我的理论就大错特错了。您是否有任何机会尝试通过 zipdeploy 手动压缩和推送本地 bin/ 文件夹的内容?这将有助于消除发布/部署方面的问题,并为您提供与您在本地拥有的内容进行更多类似的比较
猜你喜欢
  • 2021-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 2018-10-31
  • 2021-06-10
  • 1970-01-01
相关资源
最近更新 更多