WebView2Feedback icon indicating copy to clipboard operation
WebView2Feedback copied to clipboard

[Problem/Bug]: [NET 9] Dependency conflict

Open hi-xiaoxin opened this issue 1 year ago • 28 comments

What happened?

This phenomenon occurred after we upgraded to .NET 9.0, and a warning is generated during compilation:

File: Microsoft.Common.CurrentVersion.targets


发现无法解析的“WindowsBase”的不同版本之间存在冲突。
“WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”与“WindowsBase, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”之间存在冲突。
    已选择“WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”,因为它是主版本而“WindowsBase, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”不是。
    依赖于“WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”[C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\9.0.0\ref\net9.0\WindowsBase.dll]的引用。
        C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\9.0.0\ref\net9.0\WindowsBase.dll
          导致引用“C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\9.0.0\ref\net9.0\WindowsBase.dll”的项目文件项 Include 特性。
            C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\9.0.0\ref/net9.0/WindowsBase.dll
    依赖于或已统一到“WindowsBase, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”[] 的引用。
        C:\Users\user1\.nuget\packages\microsoft.web.webview2\1.0.2895-prerelease\lib_manual\net5.0-windows10.0.17763.0\Microsoft.Web.WebView2.Wpf.dll
          导致引用“C:\Users\user1\.nuget\packages\microsoft.web.webview2\1.0.2895-prerelease\lib_manual\net5.0-windows10.0.17763.0\Microsoft.Web.WebView2.Wpf.dll”的项目文件项 Include 特性。
            C:\Users\user1\.nuget\packages\microsoft.web.webview2\1.0.2895-prerelease\buildTransitive\..\\lib_manual\net5.0-windows10.0.17763.0\Microsoft.Web.WebView2.Wpf.dll

The project does not use WPF or WinForms; it controls windows through the Win32 API (Set Handle). Therefore, there is actually no dependency on WindowBase at all. Previously, everything was normal in the .NET 8.0 environment.

Although the project is still running normally, we would also like to find the cause and eliminate this warning.

Thanks

Importance

Moderate. My app's user experience is affected, but still usable.

Runtime Channel

Stable release (WebView2 Runtime), Prerelease (Edge Canary/Dev/Beta)

Runtime Version

130.0.2849.80

SDK Version

1.0.2895-prerelease

Framework

Other

Operating System

Windows 11

OS Version

22631.4460

Repro steps

Upgrade to .NET 9.0

Repros in Edge Browser

No, issue does not reproduce in the corresponding Edge version

Regression

No, this never worked

Last working version (if regression)

No response

hi-xiaoxin avatar Nov 14 '24 09:11 hi-xiaoxin

The warning also occurs under .NET 8 and only since WebView2 version 1.0.2849.39. The message does not occur in WebView2 version 1.0.2792.45.

RalphLAtGitHub avatar Nov 14 '24 09:11 RalphLAtGitHub

I am wondering whether the issue you're encountering is a version conflict between different versions of the WindowsBase assembly after upgrading to .NET 9.0.

The warning states that WindowsBase, Version=4.0.0.0 was chosen because it is the primary version, while WindowsBase, Version=5.0.0.0 is not. This conflict arises because different parts of your project or its dependencies are referencing different versions of the WindowsBase assembly.

I suspect when upgrading to a new .NET version, some dependencies might still reference older versions of assemblies, leading to conflicts.

Possible Solutions

  1. Binding Redirects:

    • You can add binding redirects in your application's configuration file to resolve the version conflict. This ensures that all references to WindowsBase use the same version.
  2. Update Dependencies:

    • Ensure that all your project's dependencies are compatible with .NET 9.0 and reference the same version of WindowsBase.
  3. Exclude Unnecessary Dependencies:

    • If your project does not use WPF or WinForms, you can try excluding the Microsoft.Web.WebView2.Wpf.dll dependency if it's not needed.

If you need further assistance or have any specific questions, feel free to ask!

sandeepchads avatar Nov 15 '24 06:11 sandeepchads

The problem goes away if you add this to your '.proj' file:

<ItemGroup>
    <FrameworkReference Include="Microsoft.WindowsDesktop.App" />
</ItemGroup>

PoulBak avatar Nov 20 '24 08:11 PoulBak

Thanks for the solution! Is there an explanation for this? Thanks!

RalphLAtGitHub avatar Nov 20 '24 08:11 RalphLAtGitHub

Thanks for the solution! Is there an explanation for this? Thanks!

I don't call this a solution but a temporary workaround until they have fixed their dependency mess. I don't have an explanation (I found this on StackOverflow, don't have the link anymore, sorry).

PoulBak avatar Nov 20 '24 12:11 PoulBak

The problem goes away if you add this to your '.proj' file:

<ItemGroup>
    <FrameworkReference Include="Microsoft.WindowsDesktop.App" />
</ItemGroup>

https://github.com/dotnet/msbuild/issues/8289

Downgrading also helps. In the case of a non desktop app (a class library) I'm not sure if it's a sufficient solution.

UltraWelfare avatar Nov 22 '24 20:11 UltraWelfare

<ItemGroup>
    <FrameworkReference Include="Microsoft.WindowsDesktop.App" />
</ItemGroup>

I confirm, the warning disappears. Unfortunately, the error still persists. More: https://github.com/dotnet/msbuild/issues/8289#issuecomment-2452637397

sefinek avatar Nov 23 '24 01:11 sefinek

#4959

hi-xiaoxin avatar Jan 01 '25 03:01 hi-xiaoxin

I believe WindowsBase is only used by WPF? Why are we then getting this conflict when using Webview2 in a WindowsForms project? I.e. with a .csproj containing:

    <UseWindowsForms>true</UseWindowsForms>
    <UseWPF>false</UseWPF>

As others have stated, downgrading to 1.0.2792.45 makes the warning go away.

otykier avatar Mar 07 '25 09:03 otykier

Why hasn't this been fixed yet? Image

Microsoft.Web.WebView2 v1.0.3124.44

sefinek avatar Mar 14 '25 01:03 sefinek

Any fix on the way? MAUI just updated to v1.0.2903.40 and now I get MSB3277 for WindowsBase: https://github.com/dotnet/maui/pull/24329

Unfortunately, I have a dependency that is shared between a WinForms project and a MAUI project. When included in a WinForms project, it also bring in the the Wpf reference through Common.targets:

        <Reference Include="$(NugetRoot)\lib_manual\netcoreapp3.0\Microsoft.Web.WebView2.Core.dll" />
        <Reference Include="$(NugetRoot)\lib_manual\netcoreapp3.0\Microsoft.Web.WebView2.WinForms.dll" />
        <Reference Include="$(NugetRoot)\lib_manual\net5.0-windows10.0.17763.0\Microsoft.Web.WebView2.Wpf.dll" />

I'm not quite sure why both are being referenced when one of them likely isn't needed? The following works fine for me since it avoids bringing in the unnecessary/problematic reference:

        <Reference Include="$(NugetRoot)\lib_manual\netcoreapp3.0\Microsoft.Web.WebView2.Core.dll" />
	<Reference Include="$(NugetRoot)\lib_manual\netcoreapp3.0\Microsoft.Web.WebView2.WinForms.dll" Condition=" '$(UseWindowsForms)' == 'true'" />
	<Reference Include="$(NugetRoot)\lib_manual\net5.0-windows10.0.17763.0\Microsoft.Web.WebView2.Wpf.dll" Condition=" '$(UseWPF)' == 'true'" />

jeremy-visionaid avatar Mar 17 '25 03:03 jeremy-visionaid

@jeremy-visionaid, there is no official solution.

One solution is to install an older version of WebView2, as I mentioned here: https://github.com/dotnet/msbuild/issues/8289#issuecomment-2452637397 (you would have to fork MAUI in this case)

The second solution is here: https://github.com/MicrosoftEdge/WebView2Feedback/issues/4924#issuecomment-2487896598

I doubt this will be fixed anytime soon. The first issues appeared in version 1.0.2792.45, released on 2024-09-23T18:54:20Z. The next version is even worse, as it now triggers an error. Due to this error, packaging the application (via MSIX Packaging Tools) for submission to the Microsoft Store is impossible.

sefinek avatar Mar 17 '25 03:03 sefinek

@sefinek Thanks for the suggestions. I did try <FrameworkReference Include="Microsoft.WindowsDesktop.App" /> already but it doesn't work for my situation (I get conflict warnings between Aero and Aero2).

It should have occured to me sooner, but what worked for me was to just remove the reference you don't need/want. e.g. for a WinForms project:

  <ItemGroup>
    <Reference Remove="Microsoft.Web.WebView2.Wpf.dll" />
  </ItemGroup>

That addresses the root cause since the Wpf assembly won't try and bring in the conflicting WindowsBase

jeremy-visionaid avatar Mar 17 '25 07:03 jeremy-visionaid

@jeremy-visionaid: Unfortunately, in my case, this doesn't work - the compiler did not exclude Microsoft.Web.WebView2.Wpf and still generates the DLL file. I remember that someone mentioned earlier that this issue is caused by Microsoft.Web.WebView2.Wpf, but I'm not sure

sefinek avatar Mar 17 '25 07:03 sefinek

@sefinek Oh darn - you're right. I was flitting between NuGet versions and forgot to revert my changes/fixes to Common.targets for the one I was using. Maybe it's still possible but through a target since that would get evaluated later. I'll have a crack at it, but it's getting late here and I obviously need some sleep 😅

jeremy-visionaid avatar Mar 17 '25 07:03 jeremy-visionaid

@sefinek Might need some refinement, but try this instead:

  <Target Name="RemoveWebView2Reference" AfterTargets="ResolvePackageDependenciesForBuild">
    <ItemGroup>
      <Reference Remove="$(NugetRoot)\lib_manual\net5.0-windows10.0.17763.0\Microsoft.Web.WebView2.Wpf.dll" />
    </ItemGroup>
  </Target>

jeremy-visionaid avatar Mar 17 '25 08:03 jeremy-visionaid

@jeremy-visionaid wo, the warning disappears, but the error still occurs - because of it, MSIX Packaging Tool doesn't work.

Image Image

sefinek avatar Mar 17 '25 08:03 sefinek

Oh darn, I'm afraid I can't help with MSIX stuff - I don't/can't use it for our app

jeremy-visionaid avatar Mar 17 '25 08:03 jeremy-visionaid

I don't use it in my main application either, so.. Well, it is what it is. My second app will run on the outdated WebView2. Microsoft will probably fix it in 10 years. Good luck!

sefinek avatar Mar 17 '25 08:03 sefinek

If it helps others, I finally settled on this for use/import through Directory.Build.targets

  <Target Name="RemoveUnnecessaryWebView2References" AfterTargets="ResolvePackageDependenciesForBuild">
    <ItemGroup>
      <Reference Remove="$(NugetRoot)\lib_manual\netcoreapp3.0\Microsoft.Web.WebView2.WinForms.dll" Condition="'$(UseWindowsForms)' != 'true'" />
      <Reference Remove="$(NugetRoot)\lib_manual\net5.0-windows10.0.17763.0\Microsoft.Web.WebView2.Wpf.dll" Condition="'$(UseWpf)' != 'true'" />
    </ItemGroup>
  </Target>

jeremy-visionaid avatar Mar 17 '25 21:03 jeremy-visionaid

    <Target Name="RemoveUnnecessaryWebView2References" AfterTargets="ResolvePackageDependenciesForBuild">
        <ItemGroup>
            <ReferenceToBeRemoved Include="@(Reference)" Condition="'%(Reference.FileName)' == 'Microsoft.Web.WebView2.WinForms' And '$(UseWindowsForms)' != 'true'"/>
            <ReferenceToBeRemoved Include="@(Reference)" Condition="'%(Reference.FileName)' == 'Microsoft.Web.WebView2.Wpf' And '$(UseWpf)' != 'true'"/>
            <Reference Remove="@(ReferenceToBeRemoved)"/>
        </ItemGroup>
    </Target>

shatyuka avatar Apr 25 '25 08:04 shatyuka

    <Target Name="RemoveUnnecessaryWebView2References" AfterTargets="ResolvePackageDependenciesForBuild">
        <ItemGroup>
            <ReferenceToBeRemoved Include="@(Reference)" Condition="'%(Reference.FileName)' == 'Microsoft.Web.WebView2.WinForms'"/>
            <ReferenceToBeRemoved Include="@(Reference)" Condition="'%(Reference.FileName)' == 'Microsoft.Web.WebView2.Wpf'"/>
            <Reference Remove="@(ReferenceToBeRemoved)"/>
        </ItemGroup>
    </Target>

Looks like it's missing the UseWindowsForms/UseWpf properties in the conditions? Otherwise it looks like it would be a good improvement to be more robust over time. I was kind of hoping that someone on the WebView2 team would notice the thread and fix the conditions.

jeremy-visionaid avatar Apr 25 '25 08:04 jeremy-visionaid

@jeremy-visionaid Yes it does not contain UseWindowsForms/UseWpf in condition attribute, for I'm using Avalonia :). I'll edit my answer.

shatyuka avatar Apr 25 '25 09:04 shatyuka

Hi folks, chiming in from the .NET team. I had a look at https://github.com/dotnet/winforms/issues/13358 and shared a workaround there. The right fix here is for WebView2 to add conditions in their targets. @soumamazu let me know if you need any help getting this done.

ericstj avatar May 12 '25 17:05 ericstj

@ericstj Thanks for weighing in too. Yeah, that's essentially what I was suggesting back in March. It's 6 months since the issue was reported for what looks like a reasonably trivial 2 line fix upstream. Pretty frustrating!

jeremy-visionaid avatar May 12 '25 21:05 jeremy-visionaid

@sefinek Might need some refinement, but try this instead:

  <Target Name="RemoveWebView2Reference" AfterTargets="ResolvePackageDependenciesForBuild">
    <ItemGroup>
      <Reference Remove="$(NugetRoot)\lib_manual\net5.0-windows10.0.17763.0\Microsoft.Web.WebView2.Wpf.dll" />
    </ItemGroup>
  </Target>

Thanks. This worked!!!!

I added a message:

  <Target Name="RemoveWebView2Reference" AfterTargets="ResolvePackageDependenciesForBuild">
    <ItemGroup>
      <Reference Remove="$(NugetRoot)\lib_manual\net5.0-windows10.0.17763.0\Microsoft.Web.WebView2.Wpf.dll" />
    </ItemGroup>
    <Message Importance="High" Text="Removed WebView2 WPF DLL reference." />
  </Target>

mikemeinz avatar Jul 15 '25 18:07 mikemeinz

Still not fixed :D

sefinek avatar Sep 18 '25 01:09 sefinek