System.TypeInitializationException: 'The type initializer for 'Bid' threw an exception.'

2

I have a service layer with the target framework set to .NET Standard 2.0. Some of the services uses DbContext from EntityFrameWorkCore.

There are two web projects which uses this service layer:

  • ASP.NET Core 3.0 (Blazor)
  • ASP.NET MVC 4.6.2

The Core project uses dependency injection like this (shortened):

public void ConfigureServices(IServiceCollection services)
{
    var json = File.ReadAllText("appsettings.json");
    var settings = JsonConvert.DeserializeObject<Settings>(json);
    services.AddScoped(provider => new CompetitorContext(settings.ConnectionStrings.Web));
    services.AddScoped<ICompetitorService, CompetitorService>();
}

The MVC project uses dependency injection through Unity like this (shortened):

public static void RegisterComponents()
{
    _container = new UnityContainer();

    var settings = 
        JsonConvert.DeserializeObject<Settings>(
            File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, 
            @"appsettings.json")));

    _container.RegisterInstance(typeof(CompetitorContext), new CompetitorContext(settings.ConnectionStrings.Web));
    _container.RegisterType<ICompetitorService, CompetitorService>();

    DependencyResolver.SetResolver(new UnityDependencyResolver(_container));
}

When calling one of the services' method that makes a call to the CompetitorContext (which inherits from DbContext) it works fine in the Core project but in the MVC project I get this error:

System.TypeInitializationException
  HResult=0x80131534
  Message=The type initializer for 'Bid' threw an exception.
  Source=<Cannot evaluate the exception source>
  StackTrace:
<Cannot evaluate the exception stack trace>

  This exception was originally thrown at this call stack:
    Bid.internalInitialize()
    Bid.Bid()

Inner Exception 1:
EntryPointNotFoundException: Unable to find an entry point named 'DllBidEntryPoint' in DLL 'System.Data.dll'.

Other services that do not use EntityFrameWorkCore works fine, so I think the issue is with using EntityFrameWorkCore together with an .NET MVC 4.6.2 project but I'm not sure.

What might the issue to this exception be or how can I found out more information?

Edited: The CompetitorService:

public class CompetitorService : ICompetitorService
    {
        private readonly CompetitorContext _ctx;

        public CompetitorService(CompetitorContext ctx)
        {
            _ctx = ctx;
        }

        public IList<CompetitorModel> GetAll()
        {
           return _ctx.Competitors.OrderBy(c => c.ID).ToList();
        }

        public CompetitorModel GetById(int id)
        {
            return this.GetAll().Where(c => c.ID == id).First();
        }

        public CompetitorModel GetByCompetitorKey(string competitorKey)
        {
            return this.GetAll().Where(c => c.CompetitorKey == competitorKey).FirstOrDefault();
        }
    }

The CompetitorContext:

public class CompetitorContext : DbContext
    {
        public DbSet<CompetitorModel> Competitors { get; set; }

        public CompetitorContext(string connectionString) :
            base(SqlServerDbContextOptionsExtensions.UseSqlServer(new DbContextOptionsBuilder(), connectionString).Options)
        {
        }
    }

The CompetitorModel:

[Table("vw_competitors")]
    public class CompetitorModel
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int ID { get; set; }
        public string CompetitorKey { get; set; }
        public string Name { get; set; }
        public string ShortName { get; set; }
        public string Experience { get; set; }
        public string BBB_Rating { get; set; }
        public string BBB_URL { get; set; }
        public string BBB_Complaints { get; set; }
        public string BBB_Accredited { get; set; }
        public string TrustPilot_URL { get; set; }
        public string TrustPilot_Rating { get; set; }
        public string TrustPilot_Reviews { get; set; }
        public string RipoffReport_Complaints { get; set; }
        public string MoneyBackGuarantee { get; set; }
        public DateTime LastUpdated { get; set; }
    }

Note: The referenced Bid class is not a data model of mine nor do I have any knowledge of it

c#
asp.net-mvc
.net-4.6.2
entity-framework-core-3.0
asked on Stack Overflow Feb 3, 2020 by Westerlund.io • edited Feb 3, 2020 by Westerlund.io

2 Answers

1

We had this problem and solved it by changing EF core to EF 6.4.4 It may be you have a problem like this and need to change or downgrade your version of EF (If you used EF)

answered on Stack Overflow Jun 14, 2020 by keivan kashani
0

I've had a simular issue using a .NET Standard 2.0 reference in a WPF application. Removing all references to System.Data(.xxx) in the WPF app did help.

answered on Stack Overflow Feb 14, 2020 by Marc Fischer

User contributions licensed under CC BY-SA 3.0