Receiving System.TypeLoadException in the .net core API project

Hi,
Created a new .Net Core based project using x new web projectname, I am using PostgreSQL installed on the network and available for other desktop based .net applications. I have installed ORM lite Db Provider for PostgreSQL for .CORE ServiceStack.OrmLite.PostgreSQL.Core installed package using Nuget PackageManager from Visual Studo

This is my appsettings.json
{
“Logging”: {
“IncludeScopes”: false,
“Debug”: {
“LogLevel”: {
“Default”: “Warning”
}
},
“Console”: {
“LogLevel”: {
“Default”: “Warning”
}
}
},
“ConnectionStrings”: {
“PostgreConnectionString”: “Database=GrcDbDev;Server=111.111.111.111;Port=5432;User Id=postgres;Password=passwordabc”
}
}
This is my startup class

public class Startup : ModularStartup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public new void ConfigureServices(IServiceCollection services)
        {
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseServiceStack(new AppHost
            {
                AppSettings = new NetCoreAppSettings(Configuration)
            });
        }
    }

    public class AppHost : AppHostBase
    {
        public AppHost() : base("GrcStr", typeof(MyServices).Assembly) { }

        // Configure your AppHost with the necessary configuration and dependencies your App needs
        public override void Configure(Container container)
        {
            var connectionString = AppSettings.GetConnectionString("PostgreConnectionString");
            var dbConnectionFactory = new OrmLiteConnectionFactory(connectionString,PostgreSqlDialect.Provider);
            container.Register<IDbConnectionFactory>(c=>new OrmLiteConnectionFactory(connectionString,PostgreSqlDialect.Provider));
            using (var db = dbConnectionFactory.Open())
            {
                if (db.CreateTableIfNotExists<StoreEntity>())
                {
                    db.Insert(new StoreEntity {StoreName = "Abc Store" });
                }

                var result = db.SingleById<StoreEntity>(1);
               // result.PrintDump(); //= {Id: 1, Name:Seed Data}
            }

            SetConfig(new HostConfig
            {
                DefaultRedirectPath = "/metadata",
                DebugMode = AppSettings.Get(nameof(HostConfig.DebugMode), false)
            });
        }
      
    }

This is my Model for which I wanted to create the table in the PostgreSQl server
public class StoreEntity
{
[AutoIncrement]
public int Id { get; set; }

        [Required]
        [Alias("store_name")]
        [Index(Unique = true)]
        public string StoreName { get; set; } 
        public ulong RowVersion { get; set; }
    }

Now when I am trying to run the project I am getting error at this line if > (db.CreateTableIfNotExists()) in my AppHost class below is the error

System.TypeLoadException
HResult=0x80131522
Message=Could not load type ‘ServiceStack.DataAnnotations.PersistedAttribute’ from assembly ‘ServiceStack.Interfaces, Version=5.0.0.0, Culture=neutral, PublicKeyToken=null’.
Source=ServiceStack.OrmLite
StackTrace:
at ServiceStack.OrmLite.OrmLiteConfigExtensions.GetModelDefinition(Type modelType) in C:\BuildAgent\work\27e4cc16641be8c0\src\ServiceStack.OrmLite\OrmLiteConfigExtensions.cs:line 222
at ServiceStack.OrmLite.OrmLiteWriteCommandExtensions.CreateTable(IDbCommand dbCmd, Boolean overwrite, Type modelType) in C:\BuildAgent\work\27e4cc16641be8c0\src\ServiceStack.OrmLite\OrmLiteWriteCommandExtensions.cs:line 79
at ServiceStack.OrmLite.OrmLiteWriteCommandExtensions.CreateTable[T](IDbCommand dbCmd, Boolean overwrite) in C:\BuildAgent\work\27e4cc16641be8c0\src\ServiceStack.OrmLite\OrmLiteWriteCommandExtensions.cs:line 74
at ServiceStack.OrmLite.OrmLiteSchemaApi.<>c__111.<CreateTableIfNotExists>b__11_0(IDbCommand dbCmd) in C:\BuildAgent\work\27e4cc16641be8c0\src\ServiceStack.OrmLite\OrmLiteSchemaApi.cs:line 125 at ServiceStack.OrmLite.OrmLiteExecFilter.Exec[T](IDbConnection dbConn, Func2 filter) in C:\BuildAgent\work\27e4cc16641be8c0\src\ServiceStack.OrmLite\OrmLiteExecFilter.cs:line 64
at ServiceStack.OrmLite.OrmLiteReadExpressionsApi.Exec[T](IDbConnection dbConn, Func`2 filter) in C:\BuildAgent\work\27e4cc16641be8c0\src\ServiceStack.OrmLite\OrmLiteReadExpressionsApi.cs:line 16
at ServiceStack.OrmLite.OrmLiteSchemaApi.CreateTableIfNotExists[T](IDbConnection dbConn) in C:\BuildAgent\work\27e4cc16641be8c0\src\ServiceStack.OrmLite\OrmLiteSchemaApi.cs:line 125
at GrcStr.AppHost.Configure(Container container) in D:\Data\Projects\DotNet\servicestack\GroccerSol\GrcStr\GrcStr\Startup.cs:line 52
at ServiceStack.ServiceStackHost.Init() in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\ServiceStackHost.cs:line 268
at ServiceStack.NetCoreAppHostExtensions.UseServiceStack(IApplicationBuilder app, AppHostBase appHost) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\AppHostBase.NetCore.cs:line 294
at GrcStr.Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env) in D:\Data\Projects\DotNet\servicestack\GroccerSol\GrcStr\GrcStr\Startup.cs:line 32

Whenever you get Type or Method Not Found or Load Exceptions when using the pre-release packages on MyGet you’ll need to clear your MyGet packages before downloading the latest v5.8.1 releases, e.g:

$ nuget locals all -clear

To ensure a clean solution also delete your /bin and /obj folders to remove the older .dll’s.

Thanks a lot. Your suggestion worked.

1 Like