Search code examples
c#asp.netmsbuild.net-6.0

Referencing a C# parameter name in an attribute, working on Windows, not on Linux


I am having an issue when running a pipeline to build for a .Net 6 Web API project. I have code that looks like this:

[HttpGet]
[MyAttribute(nameof(parameterName))]
public async Task<IActionResult> MyAction([FromQuery] MyParameter parameterName)
{
    //Action logic
}

This compiles and works on my local machine (Windows 11 64 bit) in Visual Studio 2022 (17.7.6) but when I try to build a docker (via an AzureDevops pipeline) container to run tests against I get the following errors:

/app/MyService/MyController.cs(xx, xx): error CS0103: The name 'parameterName' does not exist in the current context [/app/MyService/MyService.csproj]

The line referenced is [MyAttribute(nameof(parameterName))].

I don't know if this is a Linux thing or an MSBuild thing or how to fix it. Our build agent is a Linux Azure VM (part of an Azure scale set). In the pipeline logs I see this:

MSBuild version 17.3.2+561848881 for .NET


Solution

  • Thanks to the comments I was able to solve the issue by changing the Docker image I was using. I went from using:

    FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
    
    FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
    

    To using:

    FROM alpine:3.18.5 AS base
    
    FROM alpine:3.18.5 AS build
    

    And adding the .Net packages I required:

    RUN apk update && apk add dotnet7-sdk
    RUN apk update && apk add dotnet6-sdk
    
    RUN apk update && apk add dotnet6-runtime