You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
.NET 9 SDK WPF build broken with AssemblyName in Directory.Build.props #10068
We have a pretty complex solution with a central Directory.Build.props file.
This Directory.Build.props file contains - among other stuff - a RootNamespace property that sets a default root namespace according to our conventions.
The goal of this is that our .csproj files have short names without a MyCompanyName.MyProductName. prefix, but the namespaces and generated assemblies should have this prefix.
This worked since years up to .NET SDK 8.0.403. But it's broken since 2024-11-13 when Visual Studio 17.12 (which brings .NET SDK 9.0.100) was installed. It breaks WPF builds if the Directory.Build.props file sets a RootNamespace and/or a custom AssemblyName MSBuild property. It works if the property is moved to the WPF project's .csproj file. But it really needs to be moved, i.e. removed from the central Directory.Build.props file.
Something seems to be broken in the WPF XAML compile process.
As a workaround, I would have to comment out the <AssemblyName> property in Directory.Build.props, but this would break (or at least change) the output of all our other projects.
Reproduction Steps
Please see that attached ZIP file for a minimal repro.
Uncomment the <AssemblyName> tag in Directory.Build.props and the (clean) build will fail.
The task GenerateTemporaryTargetAssembly generates assembly with name which comes from the parent Directory.Build.props and not the one that is defined in the WPF csproj file.
Also fixes the problem. Which means that something in ExecuteGenerateTemporaryTargetAssemblyWithPackageReferenceSupport method is different than ExecuteLegacyGenerateTemporaryTargetAssembly.
The only real difference I see is how AssemblyName is passed to BuildEngine.BuildProjectFile. In the legacy version it is passed as properties, whereas in the new version it is written to the temp csproj file, and as far as I can see it is written correctly, but the BuildEngine ignores it for some reason.
Instead of overriding the AssemblyName in the csproj file, create a Directory.Build.props inside the WPF project and set the AssemblyName there.
Thank you so much!
Just wanted to confirm that this workaround also works for me. I refined the solution a bit with an important of the ancestor Directory.Build.props file as shown below, so that I don't loose all my other global settings.
false
Also fixes the problem. Which means that something in ExecuteGenerateTemporaryTargetAssemblyWithPackageReferenceSupport method is different than ExecuteLegacyGenerateTemporaryTargetAssembly.
Unfortunately this workaround does not work for me. It breaks other things in the build process.
For example, it breaks the Compile-time logging source generation feature because the source generator seems to be effectively disabled.
public static partial class Log
{
[LoggerMessage(
EventId = 0,
Level = LogLevel.Critical,
Message = "Could not open socket to `{HostName}`")]
public static partial void CouldNotOpenSocket(
ILogger logger, string hostName);
}
Results in
error CS8795: Partial method 'Log.CouldNotOpenSocket(ILogger, string)' must have an implementation part because it has accessibility modifiers.
Unfortunately this workaround does not work for me. It breaks other things in the build process.
For example, it breaks the Compile-time logging source generation feature because the source generator seems to be effectively disabled.
Yeah, I know. I just noted that, so the code owners have a starting point when trying to find out the root cause.
@candritzky - I have been able to replicate the issue with .NET 8.0 as well. Upon investigation, I discovered that the problem stems from an incorrect resultant DLL name for the TemporaryTargetAssembly task. I confirmed that we are sending the correct assembly name for the temporaryTargetAssembly csproj, and found that the issue does not occur if we use RootNamespace and AssemblyName in the csproj file of the project instead of directory.build.props. In my opinion, the issue is related to MSBuild rather than WPF.
Additionally, regarding the use of IncludePackageReferencesDuringMarkupCompilation, this option sends globalProperties containing AssemblyName and other details to MSBuild instead of null. While I am unsure how MSBuild handles global properties, I suspect that the presence of global properties ensures the resultant DLL for TemporaryTargetAssembly has the correct name.
@Kuldeep-MS As described, the problem occurs for us only since the upgrade to Visual Studio 17.12 (which includes .NET 9 SDK). I could not see it with .NET 8 SDK. Of course, I can see it also when I target net8.0-windows, but use .NET 9 SDK to build.
You might be right that this is a MSBuild issue. I cannot tell.
What else do you need from me? Why did you tag this issue with "waiting-author-feedback"?
Maybe to make the problem more clear, here are the temporary projects that are created (left: .NET 8, right: .NET 9):
With #7557 only the first AssemblyName is preserved in the .csproj file and any others are removed.
My opinion on that, it's just luck that this worked in .NET 8. I don't really see a use case in setting the AssemblyName in Directory.Build.props and .csproj to different values.
The main problem seems to be again, that MSBuildProjectName cannot be safely used in a WPF projects due to its ugly markup compilation behavior using a temporary .csproj file. 😬
If there is really need to use MSBuildProjectName, then something like the following should better be used to drop the _wpftmp suffix when compiling the temporary .csproj file:
So when you change the RootNamespace assignment in the Directory.Build.props to <RootNamespace>MyCompanyName.MyProductName.$([System.Text.RegularExpressions.Regex]::Replace($(MSbuildProjectName), '_[a-z0-9]+_wpftmp', ''))</RootNamespace>
then it should work for .NET 8 and .NET 9.
@rainersigwald - I would like to draw your attention to this issue. To give you more context, when building a WPF application, if we define the AssemblyName in Directory.Build.props using the $(MSBuildProjectName) variable. During the build process for the GenerateTemporaryTarget Assembly, WPF provides MSBuild with a .csproj file that has a resolved AssemblyName, where $(MSBuildProjectName) has already been replaced with its value. However, MSBuild ends up using the AssemblyName from the Directory.Build.props file, which still contains the $(MSBuildProjectName) variable, leading to a DLL name that differs from what is expected for the TemporaryTarget Assembly.
GenerateTemporaryTarget is an intermediate step for XAML compilation in WPF where a temporary project is created. However, the output DLL is expected to retain the original AssemblyName rather than using the temporary project's name.
Could you please have a look at this and share your insights on whether this issue lies with MSBuild, or if there is something that can be adjusted on the WPF side?
@MichaeIDietrich Thanks for your suggestion for stripping off the nasty _wpftmp suffix. So far (up to .NET 8 SDK), it was not needed. And I still don't see why this broke in .NET 9 SDK. Obviously the first AssemblyName property (at the beginning of the .csproj) file gets overwritten with some default. In .NET 8 SDK this was then "fixed" by redefining the property later again. This 2nd AssemblyName is now missing.
Regarding this:
I don't really see a use case in setting the AssemblyName in Directory.Build.props and .csproj to different values.
Our use case is the following:
We have a large solution with a lot of projects and we want them to use namespace and assembly names that are prefixed with MyCompanyName.MyProductName. by default, without having to put this prefix onto each and every .csproj file. That's why we put this convention into the central Directory.Build.props file.
The few executables that our solution produces, should have a different, shorter assembly name so that they are easier to handle from the command line (e.g. using tab completion), or to locate in the process list (Task Manager, attach debugger etc.). For this reason we override the (convention based) AssemblyName in the EXEs' .csproj files.
Does this make sense? Is there a better way to achieve the same result (without renaming all .csproj files to contain the MyCompanyName.MyProductName. prefix)?
Reacted by Jan Kučera, Michael Dietrich and Jakub Bereżański
@candritzky In fact, I think your use case is actually reasonable, so I am wrong about this.
Then the actual problem stays with using MSBuildProjectName in WPF projects.
I think we cannot override the value of it as it is a built-in property and changing the semantics seems to be the wrong path here.
But as a simple workaround we could introduce another property to the Microsoft.NET.Sdk that contains the expected project name and so there would be a simple way to fix such issues by replacing MSBuildProjectName with BetterMSBuildProjectName. (maybe someone could come up with a better name :D)
The implementation could look something like this:
This would work for all project types not only WPF.
As a mid term / long term change we could think about no longer creating a customized temporary .csproj file, but instead do this with the current .csproj file and inject the logic using MSBuild, in a similar fashion like inner builds work when a project has multiple TargetFrameworks defined. That way MSBuildProjectName would stay the same for inner temporary builds and the issue would no longer occur.
Hello I have the same issue, the problem is that clr-namespaces does not work anymore for project if AssemblyName is defined in any of Directory.Build.props files. The problem occurs for both MSBuild in Visual Studio and dotnet build outside of Visual Studio.
Any .NET build with .NET 9 SDK breaks, it does not depend on target. I build for both .NET 8 and .NET 9.
I added to my csproj that false for now, I do not know if it is a best solution.
Minimal reproductive scenario:
create new solution with wpf project 'App1',
add user control to project 'UserControl1',
reference added user control in main window,
Create Directory.Build.props file with content :
<Project>
<PropertyGroup Label="CompanyName for all Assemblies">
<AssemblyName>MyCompany.$(MSBuildProjectName)</AssemblyName>
</PropertyGroup>
</Project>
You will get the error : error MC3074: The tag 'UserControl1' does not exist in XML namespace 'clr-namespace:App1'.
Problem does not occur on console and library type projects.
Unfortunately, xmlns defined using 'XmlnsDefinitionAttribute' also ends with the same error.
Activity
zlatanov commentedon Nov 14, 2024
I can reproduce the same issue as well. We use
AssemblyNameidentically to how @candritzky explained.WORKAROUND
Instead of overriding the AssemblyName in the csproj file, create a Directory.Build.props inside the WPF project and set the AssemblyName there.
zlatanov commentedon Nov 14, 2024
For the WPF team:
The task
GenerateTemporaryTargetAssemblygenerates assembly with name which comes from the parent Directory.Build.props and not the one that is defined in the WPF csproj file.zlatanov commentedon Nov 14, 2024
Also fixes the problem. Which means that something in
ExecuteGenerateTemporaryTargetAssemblyWithPackageReferenceSupportmethod is different thanExecuteLegacyGenerateTemporaryTargetAssembly.The only real difference I see is how AssemblyName is passed to
BuildEngine.BuildProjectFile. In the legacy version it is passed as properties, whereas in the new version it is written to the temp csproj file, and as far as I can see it is written correctly, but the BuildEngine ignores it for some reason.candritzky commentedon Nov 14, 2024
Thank you so much!
Just wanted to confirm that this workaround also works for me. I refined the solution a bit with an important of the ancestor
Directory.Build.propsfile as shown below, so that I don't loose all my other global settings.candritzky commentedon Nov 14, 2024
Unfortunately this workaround does not work for me. It breaks other things in the build process.
For example, it breaks the Compile-time logging source generation feature because the source generator seems to be effectively disabled.
Results in
zlatanov commentedon Nov 14, 2024
Yeah, I know. I just noted that, so the code owners have a starting point when trying to find out the root cause.
Kuldeep-MS commentedon Nov 19, 2024
@candritzky - I have been able to replicate the issue with .NET 8.0 as well. Upon investigation, I discovered that the problem stems from an incorrect resultant DLL name for the TemporaryTargetAssembly task. I confirmed that we are sending the correct assembly name for the temporaryTargetAssembly csproj, and found that the issue does not occur if we use
RootNamespaceandAssemblyNamein thecsprojfile of the project instead ofdirectory.build.props. In my opinion, the issue is related to MSBuild rather than WPF.Additionally, regarding the use of
IncludePackageReferencesDuringMarkupCompilation, this option sendsglobalPropertiescontainingAssemblyNameand other details toMSBuildinstead ofnull. While I am unsure howMSBuildhandles global properties, I suspect that the presence of global properties ensures the resultant DLL for TemporaryTargetAssembly has the correct name.candritzky commentedon Nov 19, 2024
@Kuldeep-MS As described, the problem occurs for us only since the upgrade to Visual Studio 17.12 (which includes .NET 9 SDK). I could not see it with .NET 8 SDK. Of course, I can see it also when I target net8.0-windows, but use .NET 9 SDK to build.
You might be right that this is a MSBuild issue. I cannot tell.
What else do you need from me? Why did you tag this issue with "waiting-author-feedback"?
MichaeIDietrich commentedon Nov 19, 2024
Maybe to make the problem more clear, here are the temporary projects that are created (left: .NET 8, right: .NET 9):
With #7557 only the first
AssemblyNameis preserved in the.csprojfile and any others are removed.My opinion on that, it's just luck that this worked in .NET 8. I don't really see a use case in setting the
AssemblyNameinDirectory.Build.propsand.csprojto different values.The main problem seems to be again, that
MSBuildProjectNamecannot be safely used in a WPF projects due to its ugly markup compilation behavior using a temporary .csproj file. 😬If there is really need to use
MSBuildProjectName, then something like the following should better be used to drop the_wpftmpsuffix when compiling the temporary .csproj file:So when you change the
RootNamespaceassignment in theDirectory.Build.propsto<RootNamespace>MyCompanyName.MyProductName.$([System.Text.RegularExpressions.Regex]::Replace($(MSbuildProjectName), '_[a-z0-9]+_wpftmp', ''))</RootNamespace>then it should work for .NET 8 and .NET 9.
Kuldeep-MS commentedon Nov 20, 2024
@rainersigwald - I would like to draw your attention to this issue. To give you more context, when building a WPF application, if we define the
AssemblyNameinDirectory.Build.propsusing the$(MSBuildProjectName)variable. During the build process for the GenerateTemporaryTarget Assembly, WPF provides MSBuild with a.csprojfile that has a resolvedAssemblyName, where$(MSBuildProjectName)has already been replaced with its value. However, MSBuild ends up using theAssemblyNamefrom theDirectory.Build.propsfile, which still contains the$(MSBuildProjectName)variable, leading to a DLL name that differs from what is expected for the TemporaryTarget Assembly.GenerateTemporaryTarget is an intermediate step for XAML compilation in WPF where a temporary project is created. However, the output DLL is expected to retain the original AssemblyName rather than using the temporary project's name.
Could you please have a look at this and share your insights on whether this issue lies with MSBuild, or if there is something that can be adjusted on the WPF side?
candritzky commentedon Nov 20, 2024
@MichaeIDietrich Thanks for your suggestion for stripping off the nasty
_wpftmpsuffix. So far (up to .NET 8 SDK), it was not needed. And I still don't see why this broke in .NET 9 SDK. Obviously the first AssemblyName property (at the beginning of the .csproj) file gets overwritten with some default. In .NET 8 SDK this was then "fixed" by redefining the property later again. This 2nd AssemblyName is now missing.Regarding this:
Our use case is the following:
MyCompanyName.MyProductName.by default, without having to put this prefix onto each and every.csprojfile. That's why we put this convention into the centralDirectory.Build.propsfile.AssemblyNamein the EXEs'.csprojfiles.Does this make sense? Is there a better way to achieve the same result (without renaming all .csproj files to contain the
MyCompanyName.MyProductName.prefix)?MichaeIDietrich commentedon Nov 20, 2024
@candritzky In fact, I think your use case is actually reasonable, so I am wrong about this.
Then the actual problem stays with using
MSBuildProjectNamein WPF projects.I think we cannot override the value of it as it is a built-in property and changing the semantics seems to be the wrong path here.
But as a simple workaround we could introduce another property to the
Microsoft.NET.Sdkthat contains the expected project name and so there would be a simple way to fix such issues by replacingMSBuildProjectNamewithBetterMSBuildProjectName. (maybe someone could come up with a better name :D)The implementation could look something like this:
This would work for all project types not only WPF.
As a mid term / long term change we could think about no longer creating a customized temporary .csproj file, but instead do this with the current .csproj file and inject the logic using MSBuild, in a similar fashion like inner builds work when a project has multiple
TargetFrameworksdefined. That wayMSBuildProjectNamewould stay the same for inner temporary builds and the issue would no longer occur.mklemarczyk commentedon Nov 29, 2024
Hello I have the same issue, the problem is that clr-namespaces does not work anymore for project if AssemblyName is defined in any of Directory.Build.props files. The problem occurs for both MSBuild in Visual Studio and dotnet build outside of Visual Studio.
Any .NET build with .NET 9 SDK breaks, it does not depend on target. I build for both .NET 8 and .NET 9.
I added to my csproj that false for now, I do not know if it is a best solution.
Minimal reproductive scenario:
You will get the error :
error MC3074: The tag 'UserControl1' does not exist in XML namespace 'clr-namespace:App1'.Problem does not occur on console and library type projects.
Unfortunately, xmlns defined using 'XmlnsDefinitionAttribute' also ends with the same error.
mklemarczyk commentedon Dec 14, 2024
Important,
IncludePackageReferencesDuringMarkupCompilationworkaround makes CommunityToolkit.MVVM source generators not working.