daregazi
daregazi

Reputation: 65

.Net assembly binding referencing issue

I have a .NetStandard 2.0 project called AB which is referencing projects A and B. Project A is a .Net Standard2.0 which requires System.ComponentModel.DataAnnotation which is coming from System.ComponentModel.Annotations, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a.

Project B however is a .NetFramework4.6.1 project which also requires System.ComponentModel.DataAnnotation which is resolved from System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.

When the AB assembly is built, it emits only one System.ComponentModel.DataAnnotation.dll which happens to be from project B.

Therefore in consuming the AB assembly, objects from project A cannot be instantiated with error :

System.IO.FileNotFoundException: Could not load file or assembly 'System.ComponentModel.Annotations, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.

However objects from project B work as normal.

How do I fix this?

Please note that there is no problem as long as the consumer is a .Net consumer (Standard or Framework). The problem is discovered when our non .Net consumers started using our library.

Upvotes: 4

Views: 548

Answers (1)

magicandre1981
magicandre1981

Reputation: 28776

check for framework to reference a specific version in the csproj of project AB:

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
    <PackageReference Include="System.ComponentModel.Annotations" Version="4.5.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net461' ">
    <Reference Include="System.ComponentModel.DataAnnotations" />
</ItemGroup>

Upvotes: 2

Related Questions