You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复.NET 6 Blazor Server应用的SqlException(0x80131904)及SNI_PN11错误52

.NET 6 Blazor Server 发布后连接远程SQL Server 报错(SNI_PN11 error:52)

错误信息

Microsoft.Data.SqlClient.SqlException (0x80131904): 建立SQL Server连接时发生网络或实例特定错误,无法定位Local Database Runtime安装(provider: SNI_PN11, error: 52)。

问题背景

  • 此前版本可正常运行,回退旧NuGet包无法解决问题
  • 远程SQL Server无变更,其他应用可正常连接该数据库
  • 相同代码与NuGet包的旧构建可正常工作,重新构建后出现报错

相关配置

连接字符串

Server=<remoteserver>;Database=Test;Integrated Security=true;TrustServerCertificate=true;Trusted_Connection=True;MultipleActiveResultSets=true

发布配置文件

<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
  <PropertyGroup>
    <DeleteExistingFiles>true</DeleteExistingFiles>
    <ExcludeApp_Data>false</ExcludeApp_Data>
    <LaunchSiteAfterPublish>true</LaunchSiteAfterPublish>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <PublishProvider>FileSystem</PublishProvider>
    <PublishUrl>bin\Release\net6.0\publish\</PublishUrl>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <_TargetId>Folder</_TargetId>
    <SiteUrlToLaunchAfterPublish />
    <TargetFramework>net6.0</TargetFramework>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <PublishSingleFile>true</PublishSingleFile>
    <PublishReadyToRun>true</PublishReadyToRun>
    <ProjectGuid>28c4e0f6-5f0e-422b-a235-1d1ee85e070d</ProjectGuid>
    <SelfContained>true</SelfContained>
  </PropertyGroup>
</Project>

项目文件

<Project Sdk="Microsoft.NET.Sdk.Web" ToolsVersion="Current">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>   
    <UserSecretsId>aspnet-MudBlazorStarter-ed025a1e-e1ac-4144-83c8-2ecb1a6e4c8b</UserSecretsId>
  </PropertyGroup>

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <PublishSingleFile>true</PublishSingleFile>
    <SelfContained>true</SelfContained>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Components" Version="6.0.25" />
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="6.0.25" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.16" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.16" />
    <PackageReference Include="MudBlazor" Version="6.2.3" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\DatabaseTableExport\DatabaseTableExport.csproj" />
  </ItemGroup>

  <ItemGroup>
    <Folder Include="Services\" />
  </ItemGroup>

</Project>

更新说明

测试11月的旧构建可正常运行,NuGet包无差异;出错前一天的构建也正常,但重新构建相同代码后出现错误。


解决方案

1. 修复单文件发布的SNI依赖缺失

单文件发布模式下,.NET可能无法自动包含SQL Server Native Client(SNI)的原生依赖库,导致连接失败。可在项目文件中添加以下配置,强制包含相关文件:

<ItemGroup>
  <RuntimeHostConfigurationOption Include="NativeLibrarySearchPath" Value="$(RuntimeGraphPath)\native" />
  <NativeFileReference Include="$(RuntimeGraphPath)\native\*.dll" />
</ItemGroup>

也可临时禁用PublishSingleFile选项,改用常规发布测试是否解决问题。

2. 清理缓存后重新构建

重新构建时可能存在缓存的异常运行时包,执行以下步骤:

  • 清理本地NuGet缓存:dotnet nuget locals all --clear
  • 重新执行发布命令:dotnet publish -c Release -r win-x64 --self-contained true

3. 简化连接字符串

连接字符串中Integrated Security=true与Trusted_Connection=True作用重复,保留其一避免解析冲突,修改后字符串:

Server=<remoteserver>;Database=Test;Integrated Security=true;TrustServerCertificate=true;MultipleActiveResultSets=true

4. 锁定依赖版本一致性

虽然直接引用的NuGet包版本一致,但间接依赖可能在重新构建时发生变更。执行dotnet list package --include-transitive查看完整依赖树,对比新旧构建的依赖版本,确保完全一致后重新发布。


内容的提问来源于stack exchange,提问作者Tyson Gibby

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 04:27:17