Kir*_*eed 6 vb6 com entity-framework-core .net-standard
[最近的笔记]
如果不是 com-visible 方面,这个问题将是这个问题的副本关于 assembly binding。我已经挣扎了好几天,因为我没有意识到我需要改变我的单元测试项目以及 com 可见项目。如果你不想通读我意识到这一点的挣扎,请跳到最后一次更新。
[前言]
我担心这可能是尝试通过 COM Visible Framework 4.7.2 库从 VB6 访问 dotnet 标准 2.0 库的桥梁,但想尝试一下。
你好世界确实有效。(即调用一个返回字符串的简单函数)
但是,当我尝试通过 IDE 或作为 .EXE 运行 VB6 程序时出现此错误。
该错误消息表明该问题发生在 c# dotnet standard2.0 库的代码中。
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Extensions.DependencyInjection.Abstractions,
Version=3.1.0.0, Culture=neutral,
PublicKeyToken=adb9793829ddae60' or one of its dependencies.
The system cannot find the file specified.
File name: 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=3.1.0.0,
Culture=neutral, PublicKeyToken=adb9793829ddae60'
at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.<>c__DisplayClass4_0.<GetOrAdd>g__BuildServiceProvider|3()
at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.<>c__DisplayClass4_0.<GetOrAdd>b__2(Int64 k)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.GetOrAdd(IDbContextOptions options, Boolean providerRequired)
at Microsoft.EntityFrameworkCore.DbContext..ctor(DbContextOptions options)
at SBDSTD.Standard.Entities.ApiDbContext..ctor(DbContextOptions options)
at SBDSTD.Standard.Services.Helper.MakeContext(String connectionString)
Run Code Online (Sandbox Code Playgroud)
发生错误的库代码是
public static ApiDbContext MakeContext(string connectionString)
{
var optionsBuilder = new DbContextOptionsBuilder<ApiDbContext>();
optionsBuilder.UseSqlServer(connectionString );
var options = optionsBuilder.Options;
return new ApiDbContext(options); // Error occurs here
}
Run Code Online (Sandbox Code Playgroud)
上下文类包含
public class ApiDbContext : DbContext
{
public ApiDbContext(DbContextOptions<ApiDbContext> options)
: base(options)
{
}
// etc
Run Code Online (Sandbox Code Playgroud)
我设法通过创建一个 Framework 控制台 exe 来解决这个问题,然后让 vb6 解决这个问题。
Dim num As Long
num = ShellSync(cmd, vbMinimizedNoFocus)
Run Code Online (Sandbox Code Playgroud)
shellsync 的代码在模块中的位置
Option Explicit
Private Const INFINITE = &HFFFFFFFF
Private Const SYNCHRONIZE = &H100000
Private Const PROCESS_QUERY_INFORMATION = &H400&
Private Declare Function CloseHandle Lib "kernel32" ( _
ByVal hObject As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" ( _
ByVal hProcess As Long, _
lpExitCode As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" ( _
ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" ( _
ByVal hHandle As Long, _
ByVal dwMilliseconds As Long) As Long
Public Function ShellSync( _
ByVal PathName As String, _
ByVal WindowStyle As VbAppWinStyle) As Long
'Shell and wait. Return exit code result, raise an
'exception on any error.
Dim lngPid As Long
Dim lngHandle As Long
Dim lngExitCode As Long
lngPid = Shell(PathName, WindowStyle)
If lngPid <> 0 Then
lngHandle = OpenProcess(SYNCHRONIZE _
Or PROCESS_QUERY_INFORMATION, 0, lngPid)
If lngHandle <> 0 Then
WaitForSingleObject lngHandle, INFINITE
If GetExitCodeProcess(lngHandle, lngExitCode) <> 0 Then
ShellSync = lngExitCode
CloseHandle lngHandle
Else
CloseHandle lngHandle
Err.Raise &H8004AA00, "ShellSync", _
"Failed to retrieve exit code, error " _
& CStr(Err.LastDllError)
End If
Else
Err.Raise &H8004AA01, "ShellSync", _
"Failed to open child process"
End If
Else
Err.Raise &H8004AA02, "ShellSync", _
"Failed to Shell child process"
End If
End Function
Run Code Online (Sandbox Code Playgroud)
有趣的是,如果我调用 shell 而不是 shellsync 我会收到错误
System.Exception: kg1System.IO.FileNotFoundException: Could not load file or assembly 'System.Threading.Tasks.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.
File name: 'System.Threading.Tasks.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
at SBDSTD.Standard.Helpers.LogHelper.LogMessage(String message,
Run Code Online (Sandbox Code Playgroud)
[更新]
我将以下代码添加到 com 可见库的代码中
[DispId(2)]
[ComVisible(true)]
public string ReferenceCheck()
{
var factory = new Microsoft.Extensions.DependencyInjection.DefaultServiceProviderFactory();
return "";
}
Run Code Online (Sandbox Code Playgroud)
但它没有帮助。
我尝试使用Fuslogvw但日志查看器是空的,即使我将设置更改为登录异常文本并尝试进行故障排除
检查此链接
[更新]
但是当我查看 RegEdit 时,它已经是 1
[更新]
阅读 Hans Passant 的评论后,在 com 可见库中,我添加了一些代码来解析程序集
我在com可见库中的测试方法显示如下错误信息。
System.TypeInitializationException:
The type initializer for 'Microsoft.Data.SqlClient.TdsParser'
threw an exception. --->
System.TypeInitializationException:
The type initializer for 'Microsoft.Data.SqlClient.SNILoadHandle' threw an exception.
---> System.TypeInitializationException:
The type initializer for 'Microsoft.Data.SqlClient.SNINativeMethodWrapper' threw an exception. --->
System.ComponentModel.Win32Exception: Failed to load C:\Dev2\UnitTestProjectStandard\bin\Debug\x86\SNI.dll ---> System.ComponentModel.Win32Exception:
The specified module could not be found
Stack Trace:
SNINativeMethodWrapper.cctor() line 64
--- End of inner exception stack trace ---
SNINativeMethodWrapper.SNIInitialize()
SNILoadHandle.ctor() line 36
SNILoadHandle.cctor() line 17
--- End of inner exception stack trace ---
TdsParser.cctor() line 166
--- End of inner exception stack trace ---
DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) line 1411
DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) line 1310
DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) line 357
DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) line 773
DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) line 72
SqlConnection.TryOpenInner(TaskCompletionSource`1 retry) line 1860
SqlConnection.TryOpen(TaskCompletionSource`1 retry) line 1853
SqlConnection.Open() line 1421
RelationalConnection.OpenDbConnection(Boolean errorsExpected)
RelationalConnection.Open(Boolean errorsExpected)
RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)
Enumerator.InitializeReader(DbContext _, Boolean result)
SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
Enumerator.MoveNext()
Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
lambda_method(Closure , QueryContext )
QueryCompiler.Execute[TResult](Expression query)
EntityQueryProvider.Execute[TResult](Expression expression)
Queryable.FirstOrDefault[TSource](IQueryable`1 source)
Run Code Online (Sandbox Code Playgroud)
[更新]
我尝试重新安装 c** 可再发行组件
[更新]
我终于在这个问题的帮助下让 Fuslogvw 工作了
其中引用了问题库的 2 个版本。
我还注意到我的标准库有一个分析器抱怨
0} is an internal API that supports the Entity Framework Core infrastructure and not subject to the same compatibility standards as public APIs. It may be changed or removed without notice in any release.
Run Code Online (Sandbox Code Playgroud)
[更新]
如果我在我的 com 可见框架项目中创建一个 .exe 并运行它,就没有问题。如果我尝试单元测试,就会出现问题。
融合日志显示如下
*** Assembly Binder Log Entry (02-Mar-20 @ 7:54:23 AM) ***
The operation failed.
Bind result: hr = 0x80131040. No description available.
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\Common7\IDE\Extensions\TestPlatform\testhost.x86.exe
--- A detailed error log follows.
=== Pre-bind state information ===
LOG: DisplayName = Microsoft.Extensions.DependencyInjection.Abstractions, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
(Fully-specified)
LOG: Appbase = file:///C:/Dev2/Combridge/SBD.ComBridge/UnitTestProject1/bin/Debug
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = NULL
Calling assembly : Microsoft.Extensions.DependencyInjection, Version=3.1.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\Common7\IDE\Extensions\TestPlatform\testhost.x86.exe.Config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: Microsoft.Extensions.DependencyInjection.Abstractions, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/Dev2/Combridge/SBD.ComBridge/UnitTestProject1/bin/Debug/Microsoft.Extensions.DependencyInjection.Abstractions.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\Dev2\Combridge\SBD.ComBridge\UnitTestProject1\bin\Debug\Microsoft.Extensions.DependencyInjection.Abstractions.dll
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: Microsoft.Extensions.DependencyInjection.Abstractions, Version=3.1.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
WRN: Comparing the assembly name resulted in the mismatch: Build Number
ERR: The assembly reference did not match the assembly definition found.
ERR: Run-from-source setup phase failed with hr = 0x80131040.
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.
*** Assembly Binder Log Entry (02-Mar-20 @ 7:54:23 AM) ***
The operation failed.
Bind result: hr = 0x80131040. No description available.
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\Common7\IDE\Extensions\TestPlatform\testhost.x86.exe
--- A detailed error log follows.
=== Pre-bind state information ===
LOG: DisplayName = Microsoft.Extensions.DependencyInjection.Abstractions, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
(Fully-specified)
LOG: Appbase = file:///C:/Dev2/Combridge/SBD.ComBridge/UnitTestProject1/bin/Debug
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = NULL
Calling assembly : Microsoft.Extensions.DependencyInjection, Version=3.1.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\Common7\IDE\Extensions\TestPlatform\testhost.x86.exe.Config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: Microsoft.Extensions.DependencyInjection.Abstractions, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/Dev2/Combridge/SBD.ComBridge/UnitTestProject1/bin/Debug/Microsoft.Extensions.DependencyInjection.Abstractions.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\Dev2\Combridge\SBD.ComBridge\UnitTestProject1\bin\Debug\Microsoft.Extensions.DependencyInjection.Abstractions.dll
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: Microsoft.Extensions.DependencyInjection.Abstractions, Version=3.1.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
WRN: Comparing the assembly name resulted in the mismatch: Build Number
ERR: The assembly reference did not match the assembly definition found.
ERR: Run-from-source setup phase failed with hr = 0x80131040.
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.
Run Code Online (Sandbox Code Playgroud)
我注意到一些版本差异!
我尝试在另一台装有 VS 16.4.4 的机器上运行测试,以防它是 Visual Studio 预览版的问题,但问题又出现了。
在我的框架测试项目中,我尝试安装 3.1.1 版本的 Microsoft.Extensions.DependencyInjectionAbstractions 并注意到一个警告
Severity Code Description Project File Line Suppression State
Warning NU1605 Detected package downgrade: Microsoft.Extensions.DependencyInjection.Abstractions from 3.1.2 to 3.1.1. Reference the package directly from the project to select a different version.
UnitTestProject1 -> SBD.CombridgeVivadStd -> SBDSTD.Standard 1.0.0-CI-20200301-211054 -> Microsoft.EntityFrameworkCore 3.1.2 -> Microsoft.Extensions.DependencyInjection 3.1.2 -> Microsoft.Extensions.DependencyInjection.Abstractions (>= 3.1.2)
UnitTestProject1 -> Microsoft.Extensions.DependencyInjection.Abstractions (>= 3.1.1) UnitTestProject1 C:\Dev2\Combridge\SBD.ComBridge\UnitTestProject1\UnitTestProject1.csproj 1
Run Code Online (Sandbox Code Playgroud)
我重试了西蒙的建议手动安装 microsoft.data.sqlclient.sni
但仍然有错误
Could not load file or assembly 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=3.1.0.0
Run Code Online (Sandbox Code Playgroud)
我也试过下载 1.1.1 版本的 microsoft.data.sqlclient.sni
【更新】我决定尝试回滚.netstandard2.0项目中的实体框架来使用
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.0" />
Run Code Online (Sandbox Code Playgroud)
现在,当我使用 com 可见框架项目进行单元测试时,我得到
System.Exception: vivadbridgestd:GetValidCarriers line: 20 error:System.Exception: kg1System.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)
File name: 'System.ComponentModel.Annotations, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure.RelationalConventionSetBuilder.CreateConventionSet()
at Microsoft.EntityFrameworkCore.Metadata.Conventions.SqlServerConventionSetBuilder.CreateConventionSet()
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.RuntimeConventionSetBuilder.CreateConventionSet()
at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder)
at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, IConventionSetBuilder conventionSetBuilder)
at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.<>c.<TryAddCoreServices>b__7_3(IServiceProvider p)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite singletonCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEn
我一直在努力解决完全相同的问题并且能够解决它。
就我而言,问题 DLL 是Microsoft.Extensions.Logging
. 这是我的例外:
Could not load file or assembly 'System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Extensions.Logging,
Version=2.2.0.0, Culture=neutral,
PublicKeyToken=adb9793829ddae60' or one of its dependencies.
The system cannot find the file specified.
Run Code Online (Sandbox Code Playgroud)
该解决方案非常简单,基本上app.config
适用于使用 COM 公开的 .NET 类的 VB6 EXE。您只需要使用标签创建一个常规的 app.config<assemblyBinding>
就可以了。棘手的部分是在哪里放置此配置以及如何命名它。
C:\Program Files (x86)\Microsoft Visual Studio\VB98
. 创建配置后不要忘记重新启动 IDE。以下是有关如何在更改后解决重定向的 Fusion 日志(从 IDE 案例运行):
Calling assembly : Unity.Microsoft.Logging, Version=5.11.1.0, Culture=neutral, PublicKeyToken=489b6accfaf20ef0.
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: C:\Program Files (x86)\Microsoft Visual Studio\VB98\VB6.EXE.config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Redirect found in application configuration file: 2.2.0.0 redirected to 5.0.0.0.
LOG: Post-policy reference: Microsoft.Extensions.Logging, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
LOG: Binding succeeds. Returns assembly from C:\Program Files (x86)\Retail\Common\Microsoft.Extensions.Logging.dll.
LOG: Assembly is loaded in LoadFrom load context.
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7720 次 |
最近记录: |