I'm using Visual Studio 2022 to create a simple C# class library targeting .NET6 and .NET Framework 4.8.... and I've been tearing my hair out on this one all day.
I want to name my assembly differently depending on the framework. Specifically, I want to append ".Core" to my assembly name if it's a .NET build.
But it seems I can't utilize the $(TargetFramework) macro within a .csproj file to change the value of the AssemblyName property.
For example, this works when using the $(Configuration) macro:
<PropertyGroup>
<TargetFrameworks>net6.0;net48</TargetFrameworks>
...
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
<AssemblyName>$(MSBuildProjectName).Debug</AssemblyName>
</PropertyGroup>
But this doesn't:
<PropertyGroup>
<TargetFrameworks>net6.0;net48</TargetFrameworks>
....
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0'">
<AssemblyName>$(MSBuildProjectName).Core</AssemblyName>
</PropertyGroup>
I've been reading similar questions, specifically this one but I'm not sure it applies to CSPROJ files. Moreover the answer doesn't provide an example; it's unclear to me how it could be implemented in a CSPROJ.
Can AssemblyName be overridden by placing a condition on the target framework?
The errors I'm receiving are:
NuGet package restore failed. Please see Error List window for detailed warnings and errors.
Failed to restore D:\src2\csharp\dummy1\dummy1.csproj (in 0.6 ms).
1>C:\Program Files\dotnet\sdk\7.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(267,5): error NETSDK1005: Assets file 'D:\src2\csharp\dummy1\obj\project.assets.json' doesn't have a target for 'net6.0'. Ensure that restore has run and that you have included 'net6.0' in the TargetFrameworks for your project.
1>Done building project "dummy1.csproj" -- FAILED.
1>C:\Program Files\dotnet\sdk\7.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(267,5): error NETSDK1005: Assets file 'D:\src2\csharp\dummy1\obj\project.assets.json' doesn't have a target for 'net48'. Ensure that restore has run and that you have included 'net48' in the TargetFrameworks for your project.
1>Done building project "dummy1.csproj" -- FAILED.
using command line tools "dotnet build" and "msbuild", the project succeeds (thanks @Jimmy!). It's only in Visual Studio that it doesn't work. I'm starting to suspect my problem isn't renaming the assembly name, but something else in Visual Studio that needs to be done.
With @Jimmy pointing me to the build tool, I'm suspecting that this really is a Nuget problem, and NOT an assembly renaming issue, though the error only appears when trying to rename the assembly...