Azure Devops can an ASP.NET Core 8 Web API be built with .NET Framework references?

  Kiến thức lập trình

I have an ASP.NET Core 8 Web API that I am building/deploying through Azure Devops. The API is being hosted in IIS on a server, so I was using the DotNetCoreCLI@2 task to perform a dotnet publish which would build my project and generate a web.config.

I have some legacy .NET 4.8 projects that are now included in the project references and this has broken my build process. When trying to do the same build I would get an error:

The task “SGen” is not supported on the .NET Core version of MSBuild

It looked like this had to do with XML serialization in the legacy projects, although none of them explicitly had:

<GenerateSerializationAssemblies>On</GenerateSerializationAssemblies>

I think that one solution would be to add to all legacy projects:

<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>

but I would like to avoid that. In order to successfully build my ASP.NET Core 8 Web API + legacy references I had to swap to the VSBuild@1 task which looks like:

- task: VSBuild@1
    inputs:
      solution: ./MyApi/MyApi.csproj
      msbuildArgs: '/p:OutputPath=$(publishDirectory)'
      configuration: 'Release'
      platform: Any CPU  

This works fine, except we no longer would have the web.config being generated. My thought was that I could add a step following this that would still utilize the the DotNetCoreCLI@2 task and pass it the argument

--no-build

since I have already built the project. Now I get this error:

Microsoft.NET.Sdk.StaticWebAssets.References.targets: error : Manifest files at ‘objReleasenet8.0staticwebassets.build.json’ not found

So I don’t think I can go with a no build route. My last hope was to build only my ASP.NET Core 8 Web API since I have built all of the legacy dependencies and that would generate the web.config. In my DotNetCoreCLI@2 task I tried passing

/p:BuildProjectReferences=false

as an argument, but it cannot find any of the legacy files and errors out. It appears that the dotnet publish step is meant to be a standalone, do-it-all process?

The only solutions I can think of are:

  1. Manually add web.config to my solution and maintain it (probably the easiest?)
  2. Explicitly set serialization off in all legacy .csproj

Is there any possible option in my build process to achieve what was happening before?

6

I attempted with a sample as below, and everything can work fine:

  1. The projects within the same solution (.sln).

    • The .NET 8 Web API project. It has a project reference.
    • The .NET Framework 4.8 project referenced by the .NET 8 Web API project.
  2. The sample of the pipeline.

    steps:
    # Restore the required NuGet packages for all projects.
    - task: NuGetCommand@2
      displayName: 'NuGet restore'
    
    # Run 'dotnet publish' for the Web API project.
    # Set output directory as '$(Build.ArtifactStagingDirectory)'.
    - task: DotNetCoreCLI@2
      displayName: 'dotnet publish'
      inputs:
        command: publish
        publishWebProjects: false
        projects: myWebAPI/myWebAPI.csproj
        arguments: '--no-restore -c Release -o $(Build.ArtifactStagingDirectory)'
        zipAfterPublish: false
    
    # Publish the output artifact files from the directory '$(Build.ArtifactStagingDirectory)' to Azure Pipelines.
    - task: PublishPipelineArtifact@1
      displayName: 'Publish Pipeline Artifact'
      inputs:
        targetPath: '$(Build.ArtifactStagingDirectory)'
        artifact: drop
    
  3. Result.


EDIT:

I can reproduce the same error if I add the property “<GenerateSerializationAssemblies>On</GenerateSerializationAssemblies>” to the .csproj file of the project reference, and only use the DotNetCoreCLI@2 task to build the project.

When building the .NET 8 project, by default it also will build all of its project references together. The error occurs when using the dotnet command to build the .NET Framework 4.8 project references that the “GenerateSerializationAssemblies” is set to “On“.

To avoid the error, you can do like as below:

  1. Use the VSBuild@1 task to build the .NET Framework 4.8 project references.

  2. Then use the DotNetCoreCLI@2 task to build .NET 8 project. On this task, add the MSBuild project property “-p:BuildProjectReferences=false” as an argument to skip building the project references as they have been built by previous VSBuild@1 task. See “Common MSBuild project properties“.

Below is a sample of the pipeline as reference.

steps:
- task: NuGetCommand@2
  displayName: 'NuGet restore'

- task: VSBuild@1
  displayName: 'Build - myConsoleApp'
  inputs:
    solution: myConsoleApp/myConsoleApp.csproj
    configuration: Release
    msbuildArchitecture: x64

- task: DotNetCoreCLI@2
  displayName: 'dotnet publish - myWebAPI'
  inputs:
    command: publish
    publishWebProjects: false
    projects: myWebAPI/myWebAPI.csproj
    arguments: '--no-restore -c Release -o $(Build.ArtifactStagingDirectory) -p:BuildProjectReferences=false'
    zipAfterPublish: false

- task: PublishPipelineArtifact@1
  displayName: 'Publish Pipeline Artifact'
  inputs:
    targetPath: '$(Build.ArtifactStagingDirectory)'
    artifact: drop


2

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website Kho Theme wordpress Kho Theme WP Theme WP

LEAVE A COMMENT