.NET Core中用DacServices.Deploy部署DACPAC至SQL Server 2022超时失败
在.NET Core中使用DacServices.Deploy发布DACPAC至SQL Server 2022时遇到执行超时错误
问题说明
代码刚从旧框架迁移到.NET Core,首次部署到新建的SQL Server 2022数据库时,调用DacServices.Deploy抛出执行超时异常。
相关代码
var package = DacPackage.Load(dacpacPath); if (package == null) { throw new Exception("Error loading DACPAC"); } Log.Info($"DACPAC loaded....: Version='{package.Version}', Name='{package.Name}'"); Log.Info("Deploying DACPAC to server..."); var options = new DacDeployOptions() { BlockOnPossibleDataLoss = true }; var services = new DacServices(setup.ConnectionString); services.Message += Services_Message; services.ProgressChanged += Services_ProgressChanged; var builder = new SqlConnectionStringBuilder(setup.ConnectionString); services.Deploy(package: package, targetDatabaseName: builder.InitialCatalog, upgradeExisting: false, options); Log.Info("DACPAC deployed to server.");
异常详情
Microsoft.SqlServer.Dac.DacServicesException: 部署计划生成期间发生错误。部署无法继续。 ---> Microsoft.Data.Tools.Schema.Sql.Deployment.DeploymentFailedException: 无法重新连接到数据库:执行超时已过期。操作完成前超时时间已过,或者服务器未响应。 ---> Microsoft.Data.Tools.Schema.Sql.SchemaModel.SqlReverseEngineerException: 无法重新连接到数据库:执行超时已过期。操作完成前超时时间已过,或者服务器未响应。 ---> Microsoft.Data.SqlClient.SqlException (0x80131904): 执行超时已过期。操作完成前超时时间已过,或者服务器未响应。 ---> System.ComponentModel.Win32Exception (258): 未知错误 258 在 Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) 在 Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) 在 Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) 在 Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) 在 Microsoft.Data.SqlClient.SqlDataReader.TryConsumeMetaData() 在 Microsoft.Data.SqlClient.SqlDataReader.get_MetaData() 在 Microsoft.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption, Boolean shouldCacheForAlwaysEncrypted) 在 Microsoft.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean isAsync, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest) 在 Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String method) 在 Microsoft.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior) 在 Microsoft.Data.SqlClient.SqlCommand.ExecuteReader() 在 Microsoft.Data.Tools.Schema.Sql.SchemaModel.SqlReverseEngineerImpl.ExecutePopulators(Tuple`2 connectionTuple, IList`1 populators, Int32 totalPopulatorsCount, Int32 startIndex, Boolean progressAlreadyUpdated, ReverseEngineerOption option, SqlReverseEngineerRequest request) ClientConnectionId:b869fd60-a7a4-4baa-9e6f-0c985fa1b68e Error Number:-2,State:0,Class:11 --- 内部异常堆栈跟踪结束 --- 在 Microsoft.Data.Tools.Schema.Sql.SchemaModel.SqlReverseEngineerImpl.ExecutePopulatorsInPass(SqlReverseEngineerConnectionContext context, ReverseEngineerOption option, SqlReverseEngineerRequest request, Int32 totalCount, Tuple`2[] populatorsArray) 在 Microsoft.Data.Tools.Schema.Sql.SchemaModel.SqlReverseEngineerImpl.PopulateBatch(SqlReverseEngineerConnectionContext context, SqlSchemaModel model, ReverseEngineerOption option, ErrorManager errorManager, SqlReverseEngineerRequest request, SqlImportScope importScope) 在 Microsoft.Data.Tools.Schema.Sql.SchemaModel.SqlReverseEngineer.PopulateAll(SqlReverseEngineerConnectionContext context, ReverseEngineerOption option, ErrorManager errorManager, Boolean filterManagementScopedElements, SqlImportScope importScope, Boolean optimizeForQuery, ModelStorageType modelType) 在 Microsoft.Data.Tools.Schema.Sql.Deployment.SqlDeploymentEndpointServer.ImportDatabase(SqlReverseEngineerConstructor constructor, DeploymentEngineContext context, ErrorManager errorManager) 在 Microsoft.Data.Tools.Schema.Sql.Deployment.SqlDeploymentEndpointServer.CreateNewModel(ErrorManager errors, DeploymentEngineContext context) --- 内部异常堆栈跟踪结束 ---
解决建议
- 延长连接超时:在连接字符串中添加
Connect Timeout=300(单位秒,可根据实际调整),DacServices首次反向工程数据库结构时可能需要更长时间。 - 检查SQL Server配置:确认SQL Server 2022允许远程连接,防火墙开放1433端口,服务器CPU、内存资源充足。
- 更新DacFx库:确保使用的
Microsoft.SqlServer.DacFxNuGet包是适配.NET Core的最新稳定版本,旧版本对SQL Server 2022支持可能不完善。 - 优化部署选项:若数据库对象较多,可尝试禁用非必要的反向工程选项,或拆分部署步骤。
内容的提问来源于stack exchange,提问作者vaisakh raveendran
相关产品推荐
相关产品推荐

