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

4

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?

c#
.net
.net-core
servicestack
ormlite-servicestack
asked on Stack Overflow Feb 19, 2019 by Erik Lindberg

3 Answers

3

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.

answered on Stack Overflow Mar 1, 2019 by Erik Lindberg
1

In trying to repro this I've resolved a different issue with the ASP.NET Core Apps on the .NET Framework templates referencing the latest v5.4.1 MyGet packages where the Tests .csproj should be referencing the new ServiceStack.Kestrel.Core package instead of ServiceStack.Kestrel, i,e:

<PackageReference Include="ServiceStack.Kestrel.Core" Version="5.*" />

Which is now updated in all .Core templates. To access the latest v5.4.1 packages on MyGet you'd need to clear your NuGet packages cache:

$ nuget locals all -clear

Then install latest web-corefix project using the new web .NET Core tool:

$ dotnet tool install --global web

$ web new web-corefx WebCoreFx

I've then added a reference to OrmLite MySql in WebCoreFx.csproj:

<PackageReference Include="ServiceStack.OrmLite.MySql.Core" Version="5.*" />

And updated Startup.cs to register the MySql provider, create and select from a table simple table and access its ModelDefinition<T>.Definition API:

class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public override void Configure(Container container)
{
    SetConfig(new HostConfig {
        DefaultRedirectPath = "/metadata",
        DebugMode = AppSettings.Get(nameof(HostConfig.DebugMode), false)
    });

    container.Register<IDbConnectionFactory>(c => new OrmLiteConnectionFactory(
        "Server=localhost;Database=test;UID=root;Password=test;SslMode=none",
        MySqlDialect.Provider));

    using (var db = container.Resolve<IDbConnectionFactory>().OpenDbConnection())
    {
        db.DropAndCreateTable<Person>();

        db.Insert(new Person {Id = 1, Name = "Name"});

        var s = db.Select<Person>().Dump();
        s.PrintDump();
    }

    var model = ModelDefinition<Person>.Definition;
    model.Name.Print();
}

Which works as expected and runs without issue.

If you still have issues can you upload a MCVE on GitHub I can run locally to repro the issue.

answered on Stack Overflow Feb 20, 2019 by mythz
0

I was having this issue and others related to SqlClient not supported. Make sure the regular .net app is set to use PackageReference instead of packages.config. If not migrate it. This solved my issues without having to put in a bunch of binding redirects.

answered on Stack Overflow Mar 25, 2019 by Blane Bunderson

User contributions licensed under CC BY-SA 3.0