[C#] แก้ปัญหา Could not load file or assembly ‘System.ComponentModel.Annotations, Version=X.Y.0.0

ลองทำ Automate Test แล้วเอา json มา mock ข้อมูล พอ execute จริง Error ครับ

Message: 
Test method ds.Invest.Enquiry.Service.Impl.MSTest.Report.RealizedGLFromARAPDSServiceTests.T326715_ProcessRealizedGLFromARAP threw exception: 
System.IO.FileLoadException: Could not load file or assembly 'System.ComponentModel.Annotations, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

ตรวจสอบ และแก้ไข

- ตรวจสอบว่า Code Reference DLL เวอรชันเดียวกัน หรือ ไม่
  • ตรวจสอบว่า Solution และ Library ต่างๆที่ Reference มาใช้ System.ComponentModel.Annotations ส่วนตัวเคยเจอว่า Project Library ใช้ 5.0.0 และปรากฏว่า Main Project มา Reference 4.5.0 ตอนใช้จริงมันฟ้องหา 5.0.0
  • การแก้ไข ปรับ Reference ให้เท่ากัน
- app.config / web.config มี Lock Version DLL
  • ตรวจสอบว่ามีการ Lock Version ของ Dependency ไหม แบบนี้
<dependentAssembly>
   <assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
   <bindingRedirect oldVersion="0.0.0.0-4.7.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
  • การแก้ไข ถ้าไม่จำเป็นต้อง Lock ให้ลบออกไปครับ
- ลองแก้แล้วไม่ได้จริงๆ
  • แก้ไขไฟล์ .csproj เพิ่ม 2 บรรทัดนี้ลงไปครับ
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>

Discover more from naiwaen@DebuggingSoft

Subscribe to get the latest posts to your email.