【发布时间】:2020-05-27 09:44:37
【问题描述】:
我成功地从框架 4.7.2 测试项目中调用 .net 标准 2.0 库。
如果我使用我的 .net 标准 2.0 dll (SBD.Standard) 并创建一个引用它的新 winforms 项目,那么我会被要求添加
Microsoft.EntityFrameworkCore,
then Microsoft.EntityFrameworkCore.SqlServer,
then Microsoft.Data.SqlClient
然后我的项目成功运行。 (虽然如果自动添加额外的包会很理想)
但是,如果我尝试通过使用 Azure DevOps 构建的 Nuget 包分发我的库,则会出现问题。
但是,当我通过 Nuget(使用 Azure DevOps 创建 Nuget)分发标准库并将其包含在新项目中时出现错误。
调用栈是
System.IO.FileNotFoundException : Could not load file or assembly 'Microsoft.Data.SqlClient, Version=1.0.19269.1, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5' or one of its dependencies. The system cannot find the file specified.
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerConnection.CreateDbConnection()
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.get_DbConnection()
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.CreateCommand(RelationalCommandParameterObject parameterObject, Guid commandId, DbCommandMethod commandMethod)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.InitializeReader(DbContext _, Boolean result)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()
at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
at SBD.Standard.Helpers.HandyFuncs.QueueCommand(ApiDbContext connect, WorkTypeEnum workType, String
导致问题的代码行是
public static int QueueCommand(ApiDbContext connect, WorkTypeEnum workType, string description, int jobId, int signature, XElement elem)
{
var command =
connect.EngineCommandQueues.SingleOrDefault(
c =>
(c.Status == 0|| c.Status == 2) &&
c.Signature == signature);
我确实发现我需要安装以下软件包
Microsoft.EntityFrameworkCore 3.1.1
Microsoft.EntityFrameworkCore.SqlServer 3.1.1 which has a dependency on Microsoft.Data.SqlClient >=1.019269.1
我看到 Microsoft.Data.SQLClient v1.0.19269.1 已安装
我尝试安装 System.Linq
我在 Nuget 中看到了 Microsoft.Data.SqlClient 的注释
在 Windows 上运行时,此库依赖于 .NET Framework 上的 Microsoft.Data.SqlClient.SNI
并安装 Microsoft.Data.SqlClient.SNI v1.0.19235.1
我尝试将 Microsoft.Data.SqlClient 升级到 1.1 并且错误更改为
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\Combridge\SBD.ComBridge\UnitTestProjectStandard\bin\Debug\x86\SNI.dll ---> System.ComponentModel.Win32Exception: The specified module could not be found
at Microsoft.Data.SqlClient.SNINativeMethodWrapper..cctor() in E:\agent1\_work\31\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\Interop\SNINativeMethodWrapper.cs:line 64
--- End of inner exception stack trace ---
at Microsoft.Data.SqlClient.SNINativeMethodWrapper.SNIInitialize()
at Microsoft.Data.SqlClient.SNILoadHandle..ctor() in E:\agent1\_work\31\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\TdsParserSafeHandles.cs:line 36
at Microsoft.Data.SqlClient.SNILoadHandle..cctor() in E:\agent1\_work\31\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\TdsParserSafeHandles.cs:line 17
--- End of inner exception stack trace ---
at Microsoft.Data.SqlClient.TdsParser..cctor() in E:\agent1\_work\31\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\TdsParser.cs:line 166
--- End of inner exception stack trace ---
at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) in E:\agent1\_work\31\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionPool.cs:line 1411
at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) in E:\agent1\_work\31\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionPool.cs:line 1310
at Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) in E:\agent1\_work\31\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionFactory.cs:line 357
at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) in E:\agent1\_work\31\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionInternal.cs:line 773
at Microsoft.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) in E:\agent1\_work\31\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionClosed.cs:line 72
at Microsoft.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry) in E:\agent1\_work\31\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\SqlConnection.cs:line 1860
at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) in E:\agent1\_work\31\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\SqlConnection.cs:line 1853
at Microsoft.Data.SqlClient.SqlConnection.Open() in E:\agent1\_work\31\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\SqlConnection.cs:line 1421
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnection(Boolean errorsExpected)
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.InitializeReader(DbContext _, Boolean result)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()
at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
at lambda_method(Closure , QueryContext )
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)
[更新] 阅读 This GitHub post 似乎它可能与 Azure Devops 有关
帖子提到
Win32Exception: 加载失败 C:__source_code\repo1\Simplified\bin\x86\SNI.dll 所以它试图 加载 sni.dll 的 32 位版本,它不存在。
这让我怀疑你是在 64 位系统上构建它,然后 只是将文件传输到另一台机器。你需要发布 x86 目标的项目以获得正确的本机依赖项 解决。试试看吧。
我运行的是 Windows 10,所以我不明白为什么它会尝试加载 32 位版本的 sni.dll
我可以在 Azure Artifiacts 中看到
Microsoft.Data.SqlClient.SNI 1.0.19235.1
[更新]
我从this question 看到我需要强制 MSBuild 创建/更新包含必要绑定重定向的 YourProject.dll.config 文件。 所以 Nuget 也应该创建该配置文件。我想知道这对事情有什么影响。
我的 azure-pipelines.yml 是
# .NET Desktop
# Build and run tests for .NET Desktop or Windows classic desktop solutions.
# Add steps that publish symbols, save build artifacts, and more:
# https://docs.microsoft.com/azure/devops/pipelines/apps/windows/dot-net
trigger:
- master
pool:
vmImage: 'windows-latest'
variables:
solution: '**/*.sln'
buildPlatform: 'Any CPU'
buildConfiguration: 'Release'
Major: '2'
Minor: '0'
Patch: '0'
steps:
- task: NuGetToolInstaller@0
inputs:
versionSpec: '>=4.3.0'
checkLatest: true
- task: NuGetCommand@2
inputs:
restoreSolution: '$(solution)'
- task: VSBuild@1
inputs:
solution: '$(solution)'
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
- task: NuGetCommand@2
inputs:
command: pack
packagesToPack: '**/*.csproj'
versioningScheme: byPrereleaseNumber
majorVersion: '$(Major)'
minorVersion: '$(Minor)'
patchVersion: '$(Patch)'
- task: NuGetCommand@2
inputs:
command: pack
packagesToPack: '**/*.vbproj'
versioningScheme: byPrereleaseNumber
majorVersion: '$(Major)'
minorVersion: '$(Minor)'
patchVersion: '$(Patch)'
- task: NuGetCommand@2
displayName: 'NuGet push'
inputs:
command: push
publishVstsFeed: 'SBDCommonFeed'
allowPackageConflicts: true
[更新]
我尝试将 yaml 更改为使用 dotnet pack 但出现错误
[更新]
我尝试用
替换 yaml 的 pack 部分- task: DotNetCoreCLI@2
inputs:
command: 'pack'
selectOrConfig: 'config'
nugetConfigPath: '$(System.DefaultWorkingDirectory)/NuGet.config'
externalEndpoints: $(externalFeedCredential)
当我单击授权按钮时,我收到一条消息:权限不足或缺少资源。即使我以存储库所有者的身份登录。
我想我需要学习在 externalEndpoints 中放入什么
[更新]
我试过这个
- task: DotNetCoreCLI@2
inputs:
command: 'pack'
outputDir: '$(Build.ArtifactStagingDirectory)/TestDir'
现在错误变成了
error MSB4057: The target "pack" does not exist in the project.
【问题讨论】:
-
我将尝试将 system.data.sqlclient 添加到我的标准库中
-
你在VS本地开发库项目时,项目是否引用了上面的三个包?如果我们在 VS 中本地创建 nuget(右键单击项目 => 包),并在您的另一个项目中使用它,它是否可以正常工作?错误消息有点奇怪,但是因为当你
and then my project runs successfully时它运行良好,所以在我看来你的pack step在 Azure devops 中应该有问题......你使用 dotnet pack 命令吗?
标签: azure-devops entity-framework-core nuget .net-standard .net-4.7.2