C# 无法加载类型';System.Net.WebSockets.WebSocket';来自组件';系统,版本=4.0.0.0
我们有一个现有的WCF服务托管在Windows服务中。该服务是针对.NET Framework 4.0构建和部署的 几天前,我们安装了.NETFramework 4.6.1。服务器以前有.NET Framework 4.0 我们现在看到该服务生成了一个异常。我已经在下面介绍了异常详细信息以及FillProcessorNetTcpBinding类的代码 NET升级似乎以与.NET 4.0不向后兼容的方式更新了一些程序集。我们的目标是像以前一样拥有预部署的服务功能,而无需以.NET4.6.1为目标 当应用程序以.NET 4.0为目标时,为什么要使用.NET 4.5+程序集? 有什么建议吗C# 无法加载类型';System.Net.WebSockets.WebSocket';来自组件';系统,版本=4.0.0.0,c#,wcf,.net-4.0,.net-4.6.1,C#,Wcf,.net 4.0,.net 4.6.1,我们有一个现有的WCF服务托管在Windows服务中。该服务是针对.NET Framework 4.0构建和部署的 几天前,我们安装了.NETFramework 4.6.1。服务器以前有.NET Framework 4.0 我们现在看到该服务生成了一个异常。我已经在下面介绍了异常详细信息以及FillProcessorNetTcpBinding类的代码 NET升级似乎以与.NET 4.0不向后兼容的方式更新了一些程序集。我们的目标是像以前一样拥有预部署的服务功能,而无需以.NET4.6.1为目标
<TypeLoadException xmlns="http://schemas.datacontract.org/2004/07/System">
<Message>
Could not load type 'System.Net.WebSockets.WebSocket' from assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
</Message>
<Source>
System.ServiceModel
</Source>
<TargetSite>
Boolean get_IsApplicationTargeting45()
</TargetSite>
<StackTrace>
at System.ServiceModel.OSEnvironmentHelper.get_IsApplicationTargeting45()
at System.ServiceModel.Channels.ConnectionOrientedTransportBindingElement..ctor()
at System.ServiceModel.NetTcpBinding.Initialize()
at System.ServiceModel.NetTcpBinding..ctor()
at Company.Shared.Processor.Access.WCF.ProcessorNetTcpBinding..ctor() in c:\TeamCity\BuildAgent\work\11b9dc3c2c069bfb\Release \Main\Shared\Processor\Access\WCF\ProcessorNetTcpBinding.cs:line 12
at Company.Shared.Processor.Access.WCF.Client.ProcessorServiceClient.CreateTcpEndPoint(String hostName, UInt16 portNumber) in c:\TeamCity\BuildAgent\work\11b9dc3c2c069bfb\Release\Main\Shared\Processor\Access\WCF \ProcessorServiceClient.cs:line 34
at Company.shared.Processor.Listener.ListenerManager.CallProcessor(Object msg) in c:\TeamCity\BuildAgent\work\11b9dc3c2c069bfb\Release\Main\Shared\Processor\Listener\Service\ListenerManager.cs:line 212
</StackTrace>
</TypeLoadException>
App.config
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Security;
using System.ServiceModel;
using System.Text;
namespace Company.Shared.Processor.Access.WCF
{
public class ProcessorNetTcpBinding : NetTcpBinding
{
public ProcessorNetTcpBinding()
{
MaxBufferSize = Int32.MaxValue;
MaxReceivedMessageSize = MaxBufferSize;
ReaderQuotas.MaxStringContentLength = Int32.MaxValue;
ReaderQuotas.MaxArrayLength = Int32.MaxValue;
ReaderQuotas.MaxBytesPerRead = 65536;
ReaderQuotas.MaxDepth = 64;
SendTimeout = TimeSpan.FromMinutes(5);
Security.Mode = SecurityMode.Transport;
Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
Security.Transport.ProtectionLevel = ProtectionLevel.Sign;
}
}
}
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="CompanyWcf" type="Company.Shared.Utils.Wcf.Config.WcfConfigSection, Company.Shared.Utils"/>
</configSections>
<appSettings>
<add key="Company.Shared.Alerting" value="http://Alerting.company.com/api/"/>
</appSettings>
<connectionStrings>
<add name="AlertingDb" connectionString="Data Source=SQLSERVER;Initial Catalog=Company;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
<add name="OracleConnection" connectionString="Data Source=ODEP;User ID=/;Pooling=false;Connection Timeout=260;" providerName="System.Data.OracleClient" />
<add name="ThorConnection" connectionString="Data Source=SQLSERVER;Initial Catalog=Thor;Integrated Security=True"/>
</connectionStrings>
<!-- Company WCF-->
<CompanyWcf>
<Services>
<add name="Company Processor" host="localhost" port="4329" />
<add name="Company Processor Listener" host="localhost" port="4330" />
</Services>
</CompanyWcf>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
我认为如果您使用System.Net.WebSocket.WebSocket
类型,那么您必须至少以.Net 4.5为目标,因为正如您所写:
System.Net.WebSockets.WebSocket
是在.Net 4.5之前引入的,因此针对.Net 4.0的应用程序不存在它
这正是TypeLoadException
的作用所在。在用于.NET Framework 4.0的程序集中找不到此类型
无法从程序集“系统,版本=4.0.0.0,区域性=中性,PublicKeyToken=b77a5c561934e089”加载类型“System.Net.WebSocket.WebSocket”。
问题是:为什么在以.NET 4.0为目标的情况下使用.NET 4.5类型时可以成功构建解决方案?.NET 4.5/4.6是就地更新,即该更新使用较新的位替换计算机上的4.0程序集。你的应用程序仍然可以以4.0为目标,获得4.0行为,但在引擎盖下,它将使用4.5/4.6程序集
你犯的错误很奇怪。您计算机上的WCF程序集似乎已更新为4.6,而System.dll未更新。请检查System.dll的版本,看看是否已升级?它应该升级。如果不是,可能重新安装框架或重新启动计算机会有所帮助。正如X-Mao指出的,如果您遇到问题,我怀疑您的计算机的.NET framework设置可能有问题。如果您尝试了X-Mao建议的步骤,但仍然无法解决您的问题。可能有两个原因: 1..NET Framework安装问题仍然存在。请检查以下内容: 检查System.dll和System.servicemodel.dll文件版本。文件版本应该类似于4.6.1055.0。 检查您的计算机注册表(使用Regedit.exe)应包含以下条目:
[HKEY\U LOCAL\U MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Client]
“版本”=“4.6.01055”
“TargetVersion”=“4.0.0”
“安装”=dword:00000001
“InstallPath”=“C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\”
“Release”=dword:0006041f
如果您的计算机具有不同的文件版本或.NET Framework安装注册表项,并且您认为您已经在该计算机上安装了.NET v4.6.1。请在上打开一个问题,提供详细的安装步骤和以上文件版本和regkeys值信息。
2.您的计算机具有正确的.NET V4.6.1文件版本和设置注册表项,但是,您仍然可以重新设置该问题。请使用repro程序、文件版本和设置注册表项信息在打开问题。我们将进行进一步调查。您能否将构建更改为指向.net 4.0。。?如果代码没有更改,但是.net版本已经升级,您仍然可以对您使用的版本进行降级
.net 4.0 vs.net 4.6.x
谢谢您的回复,MethodMan。生成异常的版本已针对.NET 4.0。我们还没有部署一个针对较新框架的版本。这样看来,您不仅需要使用System.Net.WebSocket.WebSocket将其添加到using部分,而且还需要手动添加对项目引用节点的引用,System.Net.WebSocket.WebSocket直到.Net 4.5才引入,因此,针对.NET4.0的应用程序不存在该功能。我的大问题是“当应用程序以.NET 4.0为目标时,为什么应用程序尝试加载.NET 4.5+程序集”?必须在.cs文件中查看头的使用部分。。它也可以被添加到.config文件中。。