ASP.NET Core 6 MVC application with Mudblazor not working once deployed to Azure app service using Azure Devops

huangapple go评论70阅读模式
英文:

ASP.NET Core 6 MVC application with Mudblazor not working once deployed to Azure app service using Azure Devops

问题

抱歉,以下是您提供的内容的翻译:

问题描述

我有一个解决方案,其中包含一些 Azure 函数形式的微服务,以及一些共享项目和一个 MVC6 Mudblazor Web 应用程序。

问题

当我通过 Visual Studio 2022 逐个部署所有这些组件时,一切正常。

然而,当我通过 Azure DevOps 发布管道进行部署时,一切开始变得混乱。函数部署正常。但 Web 应用程序开始表现异常。Web 应用程序正常加载,但当我尝试登录时,出现以下错误:

严重: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
处理组件时发生未处理的异常:ExpectedStartOfValueNotFound,< 路径:$ | 行号:0 | 行中的字节位置:0。

System.Text.Json.JsonException: ExpectedStartOfValueNotFound,< 路径:$ | 行号:0 | 行中的字节位置:0。
---> System.Text.Json.JsonReaderException: ExpectedStartOfValueNotFound,< 行号:0 | 行中的字节位置:0。
at System.Text.Json.ThrowHelper.ThrowJsonReaderException(Utf8JsonReader&,ExceptionResource,Byte,ReadOnlySpan1 ) at System.Text.Json.Utf8JsonReader.ConsumeValue(Byte ) at System.Text.Json.Utf8JsonReader.ReadFirstToken(Byte ) at System.Text.Json.Utf8JsonReader.ReadSingleSegment() at System.Text.Json.Utf8JsonReader.Read() at System.Text.Json.Serialization.JsonConverter1[[MyProject.Platform.Shared.Wrapper.Result1[[MyProject.Platform.Application.Features.Accounts.Queries.GetById.GetProjectByIdResponse, PublicKeyToken=null]].ReadCore(Utf8JsonReader&amp;,JsonSerializerOptions,ReadStack&amp; ) 异常的内部异常堆栈结束处 at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack&amp;,JsonReaderException ) at System.Text.Json.Serialization.JsonConverter1[[MyProject.Platform.Shared.Wrapper.Result`1[[MyProject.Platform.Application.Features.Accounts.Queries.GetById.GetProjectByIdResponse,
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task,ComponentState)

blazor.webassembly.js:1 严重: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
处理组件时发生未处理的异常:在解析值时遇到意外字符:T。路径 '',行号 0,位置 0。
Newtonsoft.Json.JsonReaderException: 在解析值时遇到意外字符:T。路径 '',行号 0,位置 0。
在 Newtonsoft.Json.JsonTextReader.ParseValue()
在 Newtonsoft.Json.JsonTextReader.Read()
在 Newtonsoft.Json.JsonReader.ReadAndMoveToContent()
在 Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract,Boolean hasConverter)
在 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader,Type objectType,Boolean checkAdditionalContent)
在 Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader,Type objectType)
在 Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader,Type objectType)
在 Newtonsoft.Json.JsonConvert.DeserializeObject(String value,Type type,JsonSerializerSettings settings)
在 Newtonsoft.Json.JsonConvert.DeserializeObject[Object](String value,JsonSerializerSettings settings)
在 Newtonsoft.Json.JsonConvert.DeserializeObject[Object](String value)

在 Toolbelt.Blazor.HttpClientInterceptor.InvokeAsync(HttpClientInterceptorEventHandler asyncEventHandler,HttpClientInterceptorEventArgs args)
在 Toolbelt.Blazor.HttpClientInterceptor.InvokeAfterSendAsync(HttpClientInterceptorEventArgs args)
在 Toolbelt.Blazor.HttpClientInterceptorHandler.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)
在 System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage,HttpCompletionOption,CancellationTokenSource,Boolean,CancellationTokenSource,CancellationToken )

在 Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
在 Microsoft.AspNetCore.Components.Forms.EditForm.HandleSubmitAsync()
在 Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
在 Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task,ComponentState)

资源加载失败:服务器以状态码 405(不允许的方法)响应

这是我使用的管道 YAML:

trigger:
- development
- master

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Debug'
  publishPath: '$(Build.ArtifactStagingDirectory)/publish'

stages:

- stage: BuildApp
  displayName: Build Apps
  jobs:
  - job: BuildApp
    displayName: Build App
    steps:
    - task: DotNetCoreInstaller@2
      displayName: 'Install .NET 6 SDK'
      inputs:
        version: '6.x'

    - task: DotNetCoreInstaller@2
      displayName: 'Install .NET 7 SDK'
      inputs:
        version: '7.x'

    - task: DotNetCoreCLI@2
      displayName: 'Install WebAssembly tools for .NET 6'
      inputs:
        command: 'custom'
        custom: 'workload'
        arguments: 'install wasm-tools-net6'
        workingDirectory: '$(Agent.ToolsDirectory)/dotnet'
    - task: DotNetCoreInstaller@2
      displayName: 'Install .NET 3 SDK'
      inputs:
        version: '3.x'
    - task: NuGetToolInstaller@1
      inputs:
        versionSpec: '6.5'

    - task: NuGetCommand@2
      inputs:
        command: 'restore'
        restoreSolution: '$(System.DefaultWorkingDirectory)/WebApp/MyProject.Platform.sln'
        feedsToUse: 'config'
        nugetConfigPath: '$(System.DefaultWorkingDirectory)/nuget.config'
        arguments: '-Parallel'
    - task: MSBuild@1
      displayName: 'BUILD!!'
      inputs:
        solution: '$(System.DefaultWorkingDirectory)/WebApp/MyProject.Platform.sln'
        msbuildArchitecture: 'x64'
        platform: 'any cpu'
        configuration: $(buildConfiguration)
    - task: DotNetCoreCLI@2
      inputs:
        command: 'build'
        projects: '$(System.DefaultWorkingDirectory)/WebApp/MyProject.Platform.WebApp/Server/MyProject.Platform.Server.csproj'
        arguments: '--configuration Debug --output $(Build.ArtifactStagingDirectory)'
    - task: Dot

<details>
<summary>英文:</summary>

**The setup**

I have solution with quite few micro services in the form of Azure functions and a few shared projects and one MVC6 Mudblazor web app.

**The Problem**

When I deploy all of these components individually through Visual Studio 2022, everything works fine.

However, when I deploy these through an Azure Devops release pipeline, things start going crazy. Functions deploy fine. But the Webapp starts misbehaving. The webapp loads up fine but when i try to login, I get the following errors:

&gt; crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]  
&gt; Unhandled exception rendering component: ExpectedStartOfValueNotFound, &lt; Path: $ | LineNumber: 0 | BytePositionInLine: 0.  
&gt; 
&gt; System.Text.Json.JsonException: ExpectedStartOfValueNotFound, &lt;  Path: $ | LineNumber: 0 | BytePositionInLine: 0.  
&gt; ---&gt; System.Text.Json.JsonReaderException: ExpectedStartOfValueNotFound, &lt; LineNumber: 0 | BytePositionInLine: 0.
&gt; at  System.Text.Json.ThrowHelper.ThrowJsonReaderException(Utf8JsonReader&amp; , ExceptionResource , Byte , ReadOnlySpan`1 )  
&gt; at System.Text.Json.Utf8JsonReader.ConsumeValue(Byte )  
&gt; at System.Text.Json.Utf8JsonReader.ReadFirstToken(Byte )  
&gt; at System.Text.Json.Utf8JsonReader.ReadSingleSegment()  
&gt; at System.Text.Json.Utf8JsonReader.Read()  
&gt; at System.Text.Json.Serialization.JsonConverter`1[[MyProject.Platform.Shared.Wrapper.Result`1[[MyProject.Platform.Application.Features.Accounts.Queries.GetById.GetProjectByIdResponse, PublicKeyToken=null]].ReadCore(Utf8JsonReader&amp; , JsonSerializerOptions , ReadStack&amp; )
       Exception_EndOfInnerExceptionStack
       at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack&amp; , JsonReaderException )
       at System.Text.Json.Serialization.JsonConverter`1[[MyProject.Platform.Shared.Wrapper.Result`1[[MyProject.Platform.Application.Features.Accounts.Queries.GetById.GetProjectByIdResponse, 
       at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
       at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task , ComponentState )

    blazor.webassembly.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
          Unhandled exception rendering component: Unexpected character encountered while parsing value: T. Path &#39;&#39;, line 0, position 0.
    Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: T. Path &#39;&#39;, line 0, position 0.
       at Newtonsoft.Json.JsonTextReader.ParseValue()
       at Newtonsoft.Json.JsonTextReader.Read()
       at Newtonsoft.Json.JsonReader.ReadAndMoveToContent()
       at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
       at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
       at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)
       at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
       at Newtonsoft.Json.JsonConvert.DeserializeObject[Object](String value, JsonSerializerSettings settings)
       at Newtonsoft.Json.JsonConvert.DeserializeObject[Object](String value)
       at 
       at Toolbelt.Blazor.HttpClientInterceptor.InvokeAsync(HttpClientInterceptorEventHandler asyncEventHandler, HttpClientInterceptorEventArgs args)
       at Toolbelt.Blazor.HttpClientInterceptor.InvokeAfterSendAsync(HttpClientInterceptorEventArgs args)
       at Toolbelt.Blazor.HttpClientInterceptorHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
       at System.Net.Http.HttpClient.&lt;SendAsync&gt;g__Core|83_0(HttpRequestMessage , HttpCompletionOption , CancellationTokenSource , Boolean , CancellationTokenSource , CancellationToken )
       at 
       at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
       at Microsoft.AspNetCore.Components.Forms.EditForm.HandleSubmitAsync()
       at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
       at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task , ComponentState )

     Failed to load resource: the server responded with a status of 405 (Method Not Allowed)

This is the pipeline YAML I used:

    trigger:
    - development
    - master
    
    pool:
      vmImage: &#39;windows-latest&#39;
    
    variables:
      solution: &#39;**/*.sln&#39;
      buildPlatform: &#39;Any CPU&#39;
      buildConfiguration: &#39;Debug&#39;
      publishPath: &#39;$(Build.ArtifactStagingDirectory)/publish&#39;
    
    stages:
    
    - stage: BuildApp
      displayName: Build Apps
      jobs:
      - job: BuildApp
        displayName: Build App
        steps:
        - task: DotNetCoreInstaller@2
          displayName: &#39;Install .NET 6 SDK&#39;
          inputs:
            version: &#39;6.x&#39;
    
        - task: DotNetCoreInstaller@2
          displayName: &#39;Install .NET 7 SDK&#39;
          inputs:
            version: &#39;7.x&#39;
    
        - task: DotNetCoreCLI@2
          displayName: &#39;Install WebAssembly tools for .NET 6&#39;
          inputs:
            command: &#39;custom&#39;
            custom: &#39;workload&#39;
            arguments: &#39;install wasm-tools-net6&#39;
            workingDirectory: &#39;$(Agent.ToolsDirectory)/dotnet&#39;
        - task: DotNetCoreInstaller@2
          displayName: &#39;Install .NET 3 SDK&#39;
          inputs:
            version: &#39;3.x&#39;
        - task: NuGetToolInstaller@1
          inputs:
            versionSpec: &#39;6.5&#39;
    
        - task: NuGetCommand@2
          inputs:
            command: &#39;restore&#39;
            restoreSolution: &#39;$(System.DefaultWorkingDirectory)/WebApp/MyProject.Platform.sln&#39;
            feedsToUse: &#39;config&#39;
            nugetConfigPath: &#39;$(System.DefaultWorkingDirectory)/nuget.config&#39;
            arguments: &#39;-Parallel&#39;
        - task: MSBuild@1
          displayName: &#39;BUILD!!&#39;
          inputs:
            solution: &#39;$(System.DefaultWorkingDirectory)/WebApp/MyProject.Platform.sln&#39;
            msbuildArchitecture: &#39;x64&#39;
            platform: &#39;any cpu&#39;
            configuration: $(buildConfiguration)
        - task: DotNetCoreCLI@2
          inputs:
            command: &#39;build&#39;
            projects: &#39;$(System.DefaultWorkingDirectory)/WebApp/MyProject.Platform.WebApp/Server/MyProject.Platform.Server.csproj&#39;
            arguments: &#39;--configuration Debug --output $(Build.ArtifactStagingDirectory)&#39;
        - task: DotNetCoreCLI@2
          displayName: &#39;Publish&#39;
          inputs:
            command: &#39;publish&#39;
            publishWebProjects: True
            projects: &#39;$(System.DefaultWorkingDirectory)/WebApp/MyProject.Platform.WebApp/Server/MyProject.Platform.Server.csproj&#39;
            arguments: &#39;--configuration Debug --output $(Build.ArtifactStagingDirectory)&#39;
            zipAfterPublish: true
        - task: CopyFiles@2
          inputs:
            targetFolder: &#39;$(Build.ArtifactStagingDirectory)&#39;
    
        - task: PublishBuildArtifacts@1    
          displayName: &#39;Publish Artifact: drop&#39;

**The question**

What am I doing wrong here?

</details>


# 答案1
**得分**: 0

成功解决了问题。
问题出在这里:

- 任务: DotNetCoreCLI@2
  显示名称: '发布'
  输入:
    命令: 'publish'
    发布Web项目: True
    项目: '$(System.DefaultWorkingDirectory)/WebApp/MyProject.Platform.WebApp/Server/MyProject.Platform.Server.csproj'
    参数: '--configuration Debug --output $(Build.ArtifactStagingDirectory)'
    发布后进行压缩: true 

当您设置`发布Web项目: True`时,"如果为真,则该任务将尝试在存储库中查找Web项目并对它们运行发布命令。Web项目可通过目录中存在web.config文件或wwwroot文件夹来识别。" - Azure DevOps信息按钮

由于最初为真,它每次都选择了Client,因为CLIENT项目有一个wwwroot文件夹。将`发布Web项目`设置为false后,您可以指定一个特定的SERVER项目。

所以,SERVER项目从未被部署。这就引起了所有这些混乱。

这是最终修复的更改:

- 任务: DotNetCoreCLI@2
  显示名称: '发布'
  输入:
    命令: 'publish'
    发布Web项目: False
    项目: '$(System.DefaultWorkingDirectory)/WebApp/MyProject.Platform.WebApp/Server/MyProject.Platform.Server.csproj'
    参数: '--configuration Debug --output $(Build.ArtifactStagingDirectory)'
    发布后进行压缩: true

<details>
<summary>英文:</summary>

I was finally able to get it working.
The issue was here:

    - task: DotNetCoreCLI@2
      displayName: &#39;Publish&#39;
      inputs:
        command: &#39;publish&#39;
        publishWebProjects: True
        projects: &#39;$(System.DefaultWorkingDirectory)/WebApp/MyProject.Platform.WebApp/Server/MyProject.Platform.Server.csproj&#39;
        arguments: &#39;--configuration Debug --output $(Build.ArtifactStagingDirectory)&#39;
        zipAfterPublish: true 


When you give `publishWebProjects: True` &quot;If true, the task will try to find the web projects in the repository and run the publish command on them. Web projects are identified by presence of either a web.config file or wwwroot folder in the directory.&quot; - Azure devops info button

Due to this being true originally, it was selecting Client every time because the CLIENT project has a wwwroot folder. With publishWebProjects set to false, you can specify a particular SERVER project.

So, the SERVER project never got deployed.And that caused all of this havoc.

This is the change that finally fixed it:

    - task: DotNetCoreCLI@2
      displayName: &#39;Publish&#39;
      inputs:
        command: &#39;publish&#39;
        publishWebProjects: False
        projects: &#39;$(System.DefaultWorkingDirectory)/WebApp/MyProject.Platform.WebApp/Server/MyProject.Platform.Server.csproj&#39;
        arguments: &#39;--configuration Debug --output $(Build.ArtifactStagingDirectory)&#39;
        zipAfterPublish: true 

</details>



huangapple
  • 本文由 发表于 2023年4月7日 03:41:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75953205.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定