ASP.NET Core 6引用System.ServiceModel.Security时遇文件未找到错误求助
问题
将类库集成到ASP.NET Core 6客户端应用时,已添加类库引用且编译阶段可正常访问类和方法,但运行时抛出以下错误:
System.IO.FileNotFoundException: 未能加载文件或程序集'System.ServiceModel.Security, Version=4.8.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'。系统找不到指定的文件。
已安装.NET Framework 4.8.1运行时,类库引用列表中可见System.ServiceModel.Security,但该引用的运行时版本为v4.0.30319、版本号为4.0.0.0,导致开发无法推进。
相关VB代码如下:
Imports System.ServiceModel Imports System.ServiceModel.Channels Imports System.ServiceModel.Security Imports System.Xml Imports System.Runtime.Serialization Public NotInheritable Class GlobalProperties Public Shared Function ServiceBinding() As CustomBinding Dim binding As New CustomBinding() binding.Name = "customBinding" Dim security = TransportSecurityBindingElement.CreateUserNameOverTransportBindingElement() security.EnableUnsecuredResponse = True security.AllowInsecureTransport = True security.IncludeTimestamp = False security.DefaultAlgorithmSuite = SecurityAlgorithmSuite.Basic256 security.MessageSecurityVersion = MessageSecurityVersion.WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10 Dim encoding = New TextMessageEncodingBindingElement() encoding.MessageVersion = MessageVersion.Soap11 encoding.WriteEncoding = System.Text.Encoding.UTF8 Dim transport = New HttpTransportBindingElement() transport.MaxReceivedMessageSize = Integer.MaxValue transport.MaxBufferSize = Integer.MaxValue transport.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard encoding.ReaderQuotas.MaxArrayLength = Convert.ToInt64(16384) encoding.ReaderQuotas.MaxDepth = 32 encoding.ReaderQuotas.MaxStringContentLength = Convert.ToInt64(8192) encoding.ReaderQuotas.MaxBytesPerRead = Convert.ToInt64(4096) encoding.ReaderQuotas.MaxNameTableCharCount = Convert.ToInt64(16384) binding.Elements.Add(security) binding.Elements.Add(encoding) binding.Elements.Add(transport) binding.CloseTimeout = New TimeSpan(0, 1, 0) binding.OpenTimeout = New TimeSpan(0, 1, 0) binding.ReceiveTimeout = New TimeSpan(0, 10, 0) binding.SendTimeout = New TimeSpan(0, 2, 0) Return binding End Function End Class
解决方案
1. 替换为.NET Core兼容的NuGet包
ASP.NET Core 6不直接依赖.NET Framework版本的System.ServiceModel.Security,需安装官方提供的跨平台兼容包:
- 在ASP.NET Core 6项目的NuGet包管理器中,安装
System.ServiceModel.Security包(选择与.NET 6兼容的6.x系列版本) - 移除类库中对.NET Framework版本
System.ServiceModel.Security的引用,改为引用NuGet包中的版本
2. 添加程序集绑定重定向(针对.NET Framework类库)
如果类库基于.NET Framework开发,在ASP.NET Core 6项目的runtimeconfig.json或项目配置中添加绑定重定向,将4.8.1.0版本的请求导向已存在的4.0.0.0版本:
{ "runtimeOptions": { "assemblyBinding": { "dependentAssembly": [ { "assemblyIdentity": { "name": "System.ServiceModel.Security", "publicKeyToken": "b03f5f7f11d50a3a", "culture": "neutral" }, "bindingRedirect": { "oldVersion": "0.0.0.0-4.8.1.0", "newVersion": "4.0.0.0" } } ] } } }
3. 对齐类库与项目的目标框架
- 若类库为.NET Framework项目,建议升级为.NET Standard 2.0或.NET 6,消除跨框架引用的兼容性冲突
- 若无法升级类库,确保其目标框架版本不高于.NET Framework 4.8,并在ASP.NET Core项目的.csproj文件中确认兼容配置正确:
<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>net6.0</TargetFramework> </PropertyGroup> </Project>
4. 清理重建项目
- 删除项目的
bin和obj目录 - 清理NuGet缓存后重新还原所有包
- 重新编译并运行项目
内容的提问来源于stack exchange,提问作者Mohamed Nuhid
相关产品推荐
相关产品推荐

