2
votes

We have an existing WCF service hosted in a Windows service. The service has been built and deployed targeting .NET Framework 4.0.

A couple of days ago, we installed .NET Framework 4.6.1. The server previously had .NET Framework 4.0.

We are now seeing an exception generated from the service. I've included the exception details below as well as the code for the FillProcessorNetTcpBinding class.

It seems that the .NET upgrade has updated some of the assemblies in a way that is not backwards compatible with .NET 4.0. Our goal is to have the pre-deployed service function as it did before without needing to target .NET 4.6.1.

Why are .NET 4.5+ assemblies being used when the application is targeting .NET 4.0?

Any suggestions?

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

Referenced class that extends 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>
3
can you change the build to point to .net 4.0..? if the code hasn't changed but the .net version has been upgraded you can still down grade the build you use .net 4.0 vs .net 4.6.xMethodMan
Thanks for the reply, MethodMan. The version that is generating the exception is already targeting .NET 4.0. We haven't deployed a version targeting a newer framework yet.fatmumuhomer
then it sounds like not only will you need to add this to the using section using System.Net.WebSockets.WebSocket but you will need to manually add the reference to the project's references node as wellMethodMan
System.Net.WebSockets.WebSocket was not introduced until .NET 4.5, so it doesn't exist for an application targeting .NET 4.0. My big question is "why is the application trying to load .NET 4.5+ assemblies when the application targets .NET 4.0"?fatmumuhomer
would have to see the using section of your header in your .cs files.. and it could have also been added to the .config file as well..MethodMan

3 Answers

0
votes

I think if you use the System.Net.WebSockets.WebSocket type then you have to target at least .NET 4.5 because as you wrote:

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

That is exactly what the TypeLoadException is about. This type cannot be found in an assembly that is for .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>

The question is: Why does your solution can be built successfully when using a .NET 4.5 type while targeting .NET 4.0?

0
votes

.Net 4.5/4.6 is in-place update, i.e. the update replaces the 4.0 assemblies on your machine with newer bits. Your app can still target 4.0 and get 4.0 behaviors, but under the hood it would use 4.5/4.6 assemblies.

The error you got is odd. It seems the WCF assemblies on your machine had been updated to 4.6, while System.dll is not. Can you please check the version of your System.dll and see if it's upgraded? It should be upgraded. If it's not, maybe re-installing the framework or rebooting your machine may help.

0
votes

As X-Mao pointed out, if you ran into the problem, I suspected that your machine's .NET Framework setup may have problem. If you tried X-Mao's suggested steps, but still doesn't resolve your problem. It could be two possible reasons: 1. The .NET Framework setup issue still exist. Please check the following: Check System.dll and system.servicemodel.dll file versions. The file version should be something like 4.6.1055.0. Check your machine registry (using Regedit.exe) should have below entries: [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Client] "Version"="4.6.01055" "TargetVersion"="4.0.0" "Install"=dword:00000001 "InstallPath"="C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\" "Release"=dword:0006041f If your machine has different file version or .NET Framework setup regkeys, and you believe that you have installed .NET v4.6.1 on the machine. Please open an issue at http://connect.microsoft.com/ with detailed installation steps and above file version and regkeys values info. 2. Your machine has the right .NET V4.6.1 file versions and setup regkeys, however, you still can repro the problem. Please open an issue at http://connect.microsoft.com/ with repro program, and file versions and setup regkey info. We will do further investigation.