💡

System.ComponentModel.Annotations の FileNotFoundException 問題の解消

に公開

.NET Framework 4.7.2 における System.ComponentModel.Annotations の FileNotFoundException 問題の対応(CommunityToolkit.Mvvm 使用時)

問題の概要

.NET Framework 4.7.2 のプロジェクトで CommunityToolkit.Mvvm (v8.2.0) を使用する際、以下のエラーが発生することがあります:

System.IO.FileNotFoundException
Message=Could not load file or assembly 'System.ComponentModel.Annotations, Version=4.2.0.0...'

この問題は、CommunityToolkit.Mvvm が内部的に依存している System.ComponentModel.Annotations アセンブリのバージョン不一致に起因します。具体的には、NuGet パッケージのバージョンが 5.0.0 であっても、アセンブリ自体のバージョンが 4.2.1.0 であるため、ランタイムが 4.2.0.0 を見つけられず、エラーが発生します。

解決方法

この問題を解決するためには、プロジェクトの app.config にバインディングリダイレクトを追加します。

手順:

  1. app.config の編集:プロジェクトのルートにある app.config ファイルを開き、以下のセクションを <configuration> タグ内に追加または編集します。

    <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
          <assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
          <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.1.0" />
        </dependentAssembly>
      </assemblyBinding>
    </runtime>
    

    これにより、バージョン 4.2.0.0 への参照が実際に存在する 4.2.1.0 にリダイレクトされ、エラーが解消されます。

  2. NuGet パッケージの確認System.ComponentModel.Annotations パッケージのバージョンが 4.2.1.0 以上であることを確認してください。これにより、最新の修正と機能が適用されます。

参考資料


Solution: Fixing System.ComponentModel.Annotations FileNotFoundException in .NET Framework 4.7.2 with CommunityToolkit.Mvvm

Problem Overview

When using CommunityToolkit.Mvvm (v8.2.0) in a .NET Framework 4.7.2 project, you might encounter the following error:

System.IO.FileNotFoundException
Message=Could not load file or assembly 'System.ComponentModel.Annotations, Version=4.2.0.0...'

This issue arises due to a version mismatch of the System.ComponentModel.Annotations assembly that CommunityToolkit.Mvvm internally depends on. Specifically, even though the NuGet package version is 5.0.0, the assembly version is 4.2.1.0, causing the runtime to fail in locating version 4.2.0.0.

Solution

To resolve this issue, add a binding redirect in your project's app.config file.

Steps:

  1. Edit app.config: Open the app.config file located at the root of your project and add or edit the following section within the <configuration> tags:

    <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
          <assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
          <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.1.0" />
        </dependentAssembly>
      </assemblyBinding>
    </runtime>
    

    This redirect ensures that references to version 4.2.0.0 are mapped to the existing 4.2.1.0 version, resolving the error.

  2. Verify NuGet Package Version: Ensure that the System.ComponentModel.Annotations package version is 4.2.1.0 or higher to benefit from the latest fixes and features.

References


Accenture Japan (有志)

Discussion