gpt4 book ai didi

c# - 无法从程序集“系统”加载类型 'System.Net.WebSockets.WebSocket',版本 = 4.0.0.0

转载 作者:太空宇宙 更新时间:2023-11-03 15:22:53 26 4
gpt4 key购买 nike

我们有一个托管在 Windows 服务中的现有 WCF 服务。该服务已针对 .NET Framework 4.0 构建和部署。

几天前,我们安装了 .NET Framework 4.6.1。服务器以前有 .NET Framework 4.0。

我们现在看到服务生成异常。我在下面包含了异常详细信息以及 FillProcessorNetTcpBinding 类的代码。

似乎 .NET 升级以不向后兼容 .NET 4.0 的方式更新了一些程序集。我们的目标是像以前一样拥有预部署的服务功能,而无需面向 .NET 4.6.1。

为什么当应用程序以 .NET 4.0 为目标时使用 .NET 4.5+ 程序集?

有什么建议吗?

<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>

扩展 NetTcpBinding 的引用类

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;
}
}
}

App.config

<?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.WebSockets.WebSocket 类型,那么您必须至少以 .NET 4.5 为目标,因为正如您所写:

System.Net.WebSockets.WebSocket was not introduced until .NET 4.5, so it doesn't exist for an application targeting .NET 4.0.

这正是 TypeLoadException 的意义所在。在 .NET Framework 4.0 的程序集中找不到此类型

<Message>
Could not load type 'System.Net.WebSockets.WebSocket' from assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
</Message>

问题是:为什么在面向 .NET 4.0 的同时使用 .NET 4.5 类型可以成功构建您的解决方案?

关于c# - 无法从程序集“系统”加载类型 'System.Net.WebSockets.WebSocket',版本 = 4.0.0.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36555969/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com