Search code examples
c#.net.net-coreservicestackormlite-servicestack

Cannot load System.ComponentModel.Annotations from OrmLiteConfigExtensions (ServiceStack.OrmLite.Core)


I get a runtime error when using ServiceStack.OrmLite.Core package (5.4.1) and trying to get a ModelDefinition (ServiceStack.OrmLite.ModelDefinition) by doing:

var model = ModelDefinition<T>.Definition;

The error reads as follows:

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)'

I have tried to install nuget System.ComponentModel.Annotations (4.5.0.0 since 4.2.0.0 isnt available) to no avail. I have also tried various fixes suggested when encountering System.IO.FileLoadException, but nothing works.

The project is a .Net Framework 4.7.1 project but .Net Standard projects are included in the solution so I need to run the .Core version of ServiceStack.OrmLite.

I have tried this on two workspaces (two separate machines), (1) as described above, and (2) where no .Net Standard projects are in the solution. On the (2) machine it works when running the non Core version of ServiceStack.OrmLite, but switching to ServiceStack.OrmLite.Core the runtime error occurs.

Does anyone have any ideas?


Solution

  • Eventually I figured it out…

    As It turns out ServiceStack.OrmLite.MySql.Core requires System.ComponentModel.Annotations version 4.2.0.0 as the error message clearly states. Normally it works to install a version equal to or greater than what’s required but in this case none of that worked. And for some reason it seems as the latest release included in FX is 4.0.0.0 (or at least that’s what I get when I include the assembly version of it.) There is also a nuget installing version 4.5.0.0, but the outcome is the same.

    I have no idea why it works out of the box using the FX versions of ServiceStack.OrmLite.MySql, but when using the Core version as I do, I find no way of avoiding this without the following fix:

    In my startup project, I need to add an assembly binding telling ServiceStack.OrmLite.MySql.Core that System.ComponentModel.Annotations version 4.2.0.0 “redirects” to 4.0.0.0 (of the same assembly). It looks like this:

    <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.1.0" newVersion="4.0.0.0" />
         </dependentAssembly>
     </assemblyBinding>
    

    I'm sure there are people out there who can explain the details around this, me not one of them but nonetheless… adding this to the startup projects app.config will do the trick.