Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WPF: Build fails in wpftmp.csproj when ThisAssembly is used #404

Closed
hypervtechnics opened this issue Nov 13, 2019 · 11 comments
Closed

WPF: Build fails in wpftmp.csproj when ThisAssembly is used #404

hypervtechnics opened this issue Nov 13, 2019 · 11 comments
Milestone

Comments

@hypervtechnics
Copy link

hypervtechnics commented Nov 13, 2019

I am trying to embed the library within a WPF app targeting netcoreapp3.0.

Repro

  1. Create WPF (.NET Core) via Visual Studio 2019.
  2. Create Git repository via Git UI in VS.
  3. Install NerdBank.GitVersioning via NuGet UI.
  4. Add Title = ThisAssembly.GitCommitId; to the MainWindow constructor.
  5. Try to build the application.

The IntelliSense suggests ThisAssembly and also shows the right constant values in the tooltip (the class is generated somewhere), but when it comes the build:

1>------ Build started: Project: VersioningTest, Configuration: Debug Any CPU ------
1>MainWindow.xaml.cs(13,21,13,33): error CS0103: The name 'ThisAssembly' does not exist in the current context
1>Done building project "VersioningTest_deh445kx_wpftmp.csproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

What I tried then

  1. If you remove the line added in 3 the app successfully builds & runs. When you add the line Title = typeof(MainWindow).Assembly.GetName().Version.ToString(); instead the version is 0.0.0.0.
  2. During installation no version.json was created. I added one on project level and as this did not work in the git root which didn't work either.
  3. I executed nbgv install via packet manager console on solution(= git repo root) level, after installing it as a global tool. Still did not work. On project level did not work either (created files at same location).
  4. Used Title = typeof(MainWindow).Assembly.GetName().Version.ToString(); and nbgv install. Version displayed is 1.0.0.0.

Context

My project file:

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Nerdbank.GitVersioning" Version="3.0.26">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>

</Project>

I did not find any issues related to this issue.

@AArnott
Copy link
Collaborator

AArnott commented Nov 15, 2019

The issue with WPF is it has this horky "inner build" which builds a temporary project file that doesn't properly import the nuget imported files. This is tracked by NuGet/Home#5894.

As for version numbers being 0.0.0.0, that may be because you didn't commit your version.json file. Did you try that?

@AArnott AArnott changed the title WPF netcoreapp3.0: Build fails WPF: Build fails in wpftmp.csproj when ThisAssembly is used Nov 15, 2019
AArnott added a commit that referenced this issue Nov 15, 2019
Toward a better workaround for #404
AArnott added a commit that referenced this issue Nov 15, 2019
Workaround for #404 till the dependent bugs we have open against NuGet are resolved.

This workaround is only complete with two xml snippets added to a WPF project:

Add this just under the opening `<Project>` tag:

```xml
  <Import Project="$(BaseIntermediateOutputPath)$(_TargetAssemblyProjectName)$(MSBuildProjectExtension).nuget.g.props"
          Condition=" '$(_TargetAssemblyProjectName)' != '' and '$(ImportProjectExtensionProps)' != 'false' and exists('$(BaseIntermediateOutputPath)$(_TargetAssemblyProjectName)$(MSBuildProjectExtension).nuget.g.props')" />
```

Add this just above the closing `</Project>` tag:

```xml
  <Import Project="$(BaseIntermediateOutputPath)$(_TargetAssemblyProjectName)$(MSBuildProjectExtension).nuget.g.targets"
          Condition=" '$(_TargetAssemblyProjectName)' != '' and '$(ImportProjectExtensionProps)' != 'false' and exists('$(BaseIntermediateOutputPath)$(_TargetAssemblyProjectName)$(MSBuildProjectExtension).nuget.g.targets')" />
```

This is tested to work both for .NET Framework WPF projects and .NET Core WPF projects.
AArnott added a commit that referenced this issue Nov 15, 2019
Workaround for #404 till the dependent bugs we have open against NuGet are resolved.

This workaround is only complete with two xml snippets added to a WPF project:

Add this just under the opening `<Project>` tag:

```xml
  <Import Project="$(BaseIntermediateOutputPath)$(_TargetAssemblyProjectName)$(MSBuildProjectExtension).nuget.g.props"
          Condition=" '$(_TargetAssemblyProjectName)' != '' and '$(ImportProjectExtensionProps)' != 'false' and exists('$(BaseIntermediateOutputPath)$(_TargetAssemblyProjectName)$(MSBuildProjectExtension).nuget.g.props')" />
```

Add this just above the closing `</Project>` tag:

```xml
  <Import Project="$(BaseIntermediateOutputPath)$(_TargetAssemblyProjectName)$(MSBuildProjectExtension).nuget.g.targets"
          Condition=" '$(_TargetAssemblyProjectName)' != '' and '$(ImportProjectExtensionProps)' != 'false' and exists('$(BaseIntermediateOutputPath)$(_TargetAssemblyProjectName)$(MSBuildProjectExtension).nuget.g.targets')" />
```

This is tested to work both for .NET Framework WPF projects and .NET Core WPF projects.
@AArnott AArnott added this to the 3.0 milestone Nov 15, 2019
@hypervtechnics
Copy link
Author

hypervtechnics commented Nov 15, 2019

you didn't commit your version.json file

Yeah. I forgot that. 🤦‍♂ Nevertheless I assumed that the library is working when this happened.

@AArnott
Copy link
Collaborator

AArnott commented Nov 15, 2019

The best I can do given current NuGet/WPF bugs is now released in nerdbank.gitversioning v3.0.28. Upgrade to that and apply the following workaround:

Add this just under the opening <Project> tag:

  <Import Project="$(BaseIntermediateOutputPath)$(_TargetAssemblyProjectName)$(MSBuildProjectExtension).nuget.g.props"
          Condition=" '$(_TargetAssemblyProjectName)' != '' and '$(ImportProjectExtensionProps)' != 'false' and exists('$(BaseIntermediateOutputPath)$(_TargetAssemblyProjectName)$(MSBuildProjectExtension).nuget.g.props')" />

Add this just above the closing </Project> tag:

  <Import Project="$(BaseIntermediateOutputPath)$(_TargetAssemblyProjectName)$(MSBuildProjectExtension).nuget.g.targets"
          Condition=" '$(_TargetAssemblyProjectName)' != '' and '$(ImportProjectExtensionProps)' != 'false' and exists('$(BaseIntermediateOutputPath)$(_TargetAssemblyProjectName)$(MSBuildProjectExtension).nuget.g.targets')" />

This is tested to work both for .NET Framework WPF projects and .NET Core WPF projects.

@hypervtechnics
Copy link
Author

Now it is working. Thank you for the quick workaround 😄 !

@wjk
Copy link

wjk commented Apr 4, 2020

@AArnott Unfortunately, this issue is happening with one of my projects, and the workaround you proposed in #404 (comment) isn’t working for me. Could you please reopen this issue so we can look into this? I’m using NBGV 3.1.74, which is larger than the version that you specified (3.0.28). Thanks!

@wjk
Copy link

wjk commented Apr 5, 2020

Please disregard my earlier comment; I have since fixed the issue. Sorry to bother you!

@inthemedium
Copy link

To aid others, I found another instance where the workaround above fixes the issue below. You can get in this situation with usage of InternalsVisibleTo in dependencies of a WPF project.

SomeFileUnderAWpfProject.cs(42,42): error CS0433: The type 'ThisAssembly' exists in both 'AssemblyA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' and 'AssemblyB, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' [wpfproject_x3jmmw55_wpftmp.csproj]

@AArnott
Copy link
Collaborator

AArnott commented Feb 5, 2021

It is also worth noting that the WPF team finally fixed this bug, although you need a recent SDK to get the fix. Details are here: microsoft/CsWin32#7

@lordscarlet
Copy link

lordscarlet commented Jan 10, 2023

I am having this problem on a project that is not WPF (<Project Sdk="Microsoft.NET.Sdk">), but is net standard (<TargetFrameworks>net7.0;net48;</TargetFrameworks>). I don't know if that may be relevant. I have tried to scour through all of the relevant issues here and tried the workarounds, but no solution yet.

I get the 'ThisAssembly' is inaccessible due to its protection level error, such as in #449

EDIT: I removed NDGV from some projects in the solution that didn't need it, cleared out obj/ and bin/ everywhere and I no longer get a compilation error.

@lordscarlet
Copy link

@AArnott ok, I am back. I am trying to apply this to an ASP.NET full framework application. I can build locally, but in the pipeline on ADO I still get the error above. I was able to solve it locally by adding the Directory.build.targets mentioned above, but that doesn't seem to help it when it gets to the build server.

@AArnott
Copy link
Collaborator

AArnott commented Nov 17, 2023

What OS is your build server running? Directory.Build.targets is the proper capitalization, so if the build server is non-Windows, it wouldn't work with your Directory.build.targets file.
I know you said 'full framework', but there's mono, so... just considering possibilities here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants