简体   繁体   English

System.TypeInitializationException: 'Bid' 的类型初始值设定项引发异常。

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

I have a service layer with the target framework set to .NET Standard 2.0 .我有一个目标框架设置为.NET Standard 2.0的服务层。 Some of the services uses DbContext from EntityFrameWorkCore .一些服务使用来自EntityFrameWorkCore DbContext

There are two web projects which uses this service layer:有两个 Web 项目使用此服务层:

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

The Core project uses dependency injection like this (shortened): Core项目使用这样的依赖注入(缩短):

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): MVC项目通过 Unity 使用依赖注入,如下所示(缩短):

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:当调用调用CompetitorContext (从DbContext继承)的服务方法之一时,它在Core项目中工作正常,但在MVC项目中,我收到此错误:

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.其他不使用EntityFrameWorkCore工作正常,所以我认为问题在于将EntityFrameWorkCore.NET MVC 4.6.2项目一起使用,但我不确定。

What might the issue to this exception be or how can I found out more information?此异常的问题可能是什么,或者我如何找到更多信息?

Edited: The CompetitorService :编辑: 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 : CompetitorContext

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

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

The CompetitorModel : 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注意:引用的Bid类不是我的数据模型,我也不了解它

我们遇到了这个问题,并通过将 EF 核心更改为 EF 6.4.4 来解决它可能是您遇到了这样的问题,需要更改或降级您的 EF 版本(如果您使用了 EF)

I've had a simular issue using a .NET Standard 2.0 reference in a WPF application.我在 WPF 应用程序中使用 .NET Standard 2.0 引用时遇到了类似问题。 Removing all references to System.Data(.xxx) in the WPF app did help.在 WPF 应用程序中删除对 System.Data(.xxx) 的所有引用确实有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.TypeInitializationException:&#39;类型初始值设定项引发了异常。 - System.TypeInitializationException: 'The type initializer threw an exception.' System.TypeInitializationException:“'System.Management.ManagementPath' 的类型初始值设定项引发异常。” - System.TypeInitializationException: "The type initializer for 'System.Management.ManagementPath' threw an exception." System.TypeInitializationException:&#39;&#39;CRM.Models.SiteSettings&#39;的类型初始值设定项引发异常。 - System.TypeInitializationException: 'The type initializer for 'CRM.Models.SiteSettings' threw an exception.' System.TypeInitializationException:&#39;Tips&#39;的类型初始值设定项引发了异常 - System.TypeInitializationException: The type initializer for 'Tips' threw an exception System.TypeInitializationException:“SQLite.SQLiteConnection”的类型初始化程序引发异常 - System.TypeInitializationException: The type initializer for 'SQLite.SQLiteConnection' threw an exception System.TypeInitializationException:&#39;MyApp.Core.App&#39;的类型初始值设定项引发了异常 - System.TypeInitializationException: The type initializer for 'MyApp.Core.App' threw an exception System.TypeInitializationException:“ Oracle.DataAccess.Client.OracleConnection”的类型初始值设定项引发了异常 - System.TypeInitializationException: The type initializer for 'Oracle.DataAccess.Client.OracleConnection' threw an exception 未处理的类型异常(System.TypeInitializationException) - Unhandled Type Exception(System.TypeInitializationException) 读取json文件异常“未处理的异常:System.TypeInitializationException:类型初始值设定项” - reading a json file exception “Unhandled Exception: System.TypeInitializationException: The type initializer for ” TypeInitializationException: &#39; 的类型初始值设定项<Module> &#39;抛出异常 - TypeInitializationException: The type initializer for '<Module>' threw an exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM