通过COM调用DbContext时出现PlatformNotSupportedException异常求助
.NET 7类库通过COM暴露给VB6时,EF(非EF Core)初始化DbContext报错
背景
- 曾开发一个.NET Framework类库,正确暴露给COM后可在VB6中创建使用,该类库基于Entity Framework完成计算与数据库读取。
- 已将该类库迁移至.NET 7(Core)版本,仍使用旧版Entity Framework(非EF Core),通过.NET控制台应用调用时功能完全正常。
- 已按以下步骤将.NET 7类库暴露给COM:
- 在
.csproj中添加配置:<EnableComHosting>true</EnableComHosting> <PlatformTarget>x86</PlatformTarget> - 注册生成的comhost DLL:
regsvr32 my-library.comhost.dll - 在VB6中创建对象实例:
Set obj = CreateObject("new:{C3BBD3C1-FED4-43B0-A9CE-38C356D2BA8E}")
- 在
问题
对象可成功创建,调用暴露的函数时代码能在.NET类库中正常执行(可通过VS附加调试),但创建DbContext时抛出以下异常:
System.TypeInitializationException: 'The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception.' ConfigurationErrorsException: Configuration system failed to initialize PlatformNotSupportedException: Operation is not supported on this platform.
完整异常详情(编辑1)
System.TypeInitializationException HResult=0x80131534 Message=“System.Data.Entity.Internal.AppConfig”的类型初始值设定项引发异常。 Source=EntityFramework StackTrace: at System.Data.Entity.Internal.AppConfig.get_DefaultInstance() at System.Data.Entity.Internal.LazyInternalConnection..ctor(DbContext context, String nameOrConnectionString) at System.Data.Entity.DbContext..ctor() at LibTest.Domain.Infrastructure.ConfigurationDbContext..ctor() in X:\SOURCE\WEBSERVE\1\LibTestCore\LibTestCore.Domain\Infrastructure\ConfigurationDbContext.cs:line 27 at LibTest.Domain.Configuration.Concrete.DbConfigurationRepository..ctor() in X:\SOURCE\WEBSERVE\1\LibTestCore\LibTestCore.Domain\Concrete\Configuration\DbConfigurationRepository.cs:line 12 at LibTest.Domain.Concrete.Configuration.LegacyConfigurationRepository..ctor() at LibTest.Domain.Concrete.Legacy.ComInterface..ctor() in X:\SOURCE\WEBSERVE\1\LibTestCore\LibTestCore.Domain\Concrete\Legacy\ComInterface.cs:line 16 at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions) 此异常最初是在此调用堆栈中引发的: System.Configuration.ClientConfigPaths.ClientConfigPaths(string, bool) System.Configuration.ClientConfigPaths.GetPaths(string, bool) System.Configuration.ClientConfigurationHost.ConfigPaths.get() System.Configuration.ClientConfigurationHost.GetStreamName(string) System.Configuration.ClientConfigurationHost.IsAppConfigHttp.get() System.Configuration.Internal.DelegatingConfigHost.IsAppConfigHttp.get() System.Configuration.ClientConfigurationSystem.ClientConfigurationSystem() System.Configuration.ConfigurationManager.EnsureConfigurationSystem() 内部异常 1: ConfigurationErrorsException: 配置系统未能初始化 内部异常 2: PlatformNotSupportedException: 此平台不支持该操作。
已知当前执行环境存在平台识别问题,但不清楚具体解决方案。
内容的提问来源于stack exchange,提问作者John Freebs
相关产品推荐
相关产品推荐

