locked
Use HttpClient to send http request , it will thrown exception "System.MissingMethodException: Method not found: 'Void System.Net.HttpWebRequest.set_SslProtocols" on windows 2012 R2 machine RRS feed

  • Question

  • Hi, 

    we just have a simply c# program to send http request to web server with httpclient API ,it works on  local machine but failed on window2012 R2 VM and thrown expection of "System.MissingMethodException: Method not found: 'Void System.Net.HttpWebRequest.set_SslProtocols(System.Security.Authentication.SslProtocols)".

    The simple code like below:

                    string strurl = "http://www.xxxx.com";
    
                    HttpClient theConnect = new HttpClient();
                    HttpResponseMessage hresult = theConnect.GetAsync(strurl).Result;
    
                    if (hresult.IsSuccessStatusCode)
                    {
                        Task<string> t = hresult.Content.ReadAsStringAsync();
                        string s = t.Result;
                        MessageBox.Show(s);
                    }

    it work well on most of local developer computer (win7 and win10 and win2016) but only failed on one windows 2012 r2 with exception below:

    ************** Exception Text **************
    System.AggregateException: One or more errors occurred. ---> System.MissingMethodException: Method not found: 'Void System.Net.HttpWebRequest.set_SslProtocols(System.Security.Authentication.SslProtocols)'.
       at System.Net.Http.HttpClientHandler.CreateAndPrepareWebRequest(HttpRequestMessage request)
       at System.Net.Http.HttpClientHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
       --- End of inner exception stack trace ---
       at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
       at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
       at System.Threading.Tasks.Task`1.get_Result()
       at tHttpClient.Form1.button1_Click(Object sender, EventArgs e) in E:\work\temp-proj\tHttpClient\Form1.cs:line 26
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    ---> (Inner Exception #0) System.MissingMethodException: Method not found: 'Void System.Net.HttpWebRequest.set_SslProtocols(System.Security.Authentication.SslProtocols)'.
       at System.Net.Http.HttpClientHandler.CreateAndPrepareWebRequest(HttpRequestMessage request)
       at System.Net.Http.HttpClientHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)<---



    ************** Loaded Assemblies **************
    mscorlib
        Assembly Version: 4.0.0.0
        Win32 Version: 4.6.81.0 built by: NETFXREL2
        CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
    ----------------------------------------
    tHttpClient
        Assembly Version: 1.0.0.0
        Win32 Version: 1.0.0.0
        CodeBase: file:///C:/Users/administrator.ADFS/Desktop/httpclient/Debug/tHttpClient.exe
    ----------------------------------------
    System.Windows.Forms
        Assembly Version: 4.0.0.0
        Win32 Version: 4.6.81.0 built by: NETFXREL2
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
    ----------------------------------------
    System
        Assembly Version: 4.0.0.0
        Win32 Version: 4.6.81.0 built by: NETFXREL2
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
    ----------------------------------------
    System.Drawing
        Assembly Version: 4.0.0.0
        Win32 Version: 4.6.81.0 built by: NETFXREL2
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
    ----------------------------------------
    System.Configuration
        Assembly Version: 4.0.0.0
        Win32 Version: 4.6.81.0 built by: NETFXREL2
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
    ----------------------------------------
    System.Core
        Assembly Version: 4.0.0.0
        Win32 Version: 4.6.81.0 built by: NETFXREL2
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
    ----------------------------------------
    System.Xml
        Assembly Version: 4.0.0.0
        Win32 Version: 4.6.81.0 built by: NETFXREL2
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
    ----------------------------------------
    System.Net.Http
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.3221.0 built by: NET472REL1LAST_C
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Net.Http/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Net.Http.dll

    I just check and confirm that : the window2012R2 vm use the same System.Net.Http.dll as others.

    any comments or idea about this?

    Best regards

    Tonny 

    Tuesday, December 25, 2018 7:19 AM

Answers

  • The 4.0.30319 is the standard (original) v4 CLR. That is expected as it has never been updated since release. Unfortunately that doesn't map to the framework you targeted or installed. Since 4.x is installed over the top of itself you cannot use the file system to determine which version is installed. Personally I just go to Add/Remove Programs but you can also look it up in the registry.

    "IIS will load default System.net.http instead of NuGet package in runtime"

    No that isn't true. IIS doesn't load .NET at all. That is up to the runtime of your app. If your app is using System.Net.Http from NuGet then that binary is in your site bin folder and that version is the one that will be used. In your web.config there would be a binding redirect placed (when you added the package) to use this version. In theory just updating your app to use the NuGet package would be sufficient to work around this. Because of the binding redirect any dependent assemblies you have will use it as well irrelevant of the version of the framework installed.

    However the long term solution is to require that if your app is compiling against (for example) 4.6.1 then the target machine must have at least that as well. You cannot "downgrade" framework versions on the server as things won't work.

    But somewhere in our conversation we switched to IIS which doesn't make sense to me. Your call stack indicates a Winforms app. So the error is occurring in Winforms, not IIS. Your Winforms app will also need a dependency on System.Net.Http from NuGet or that the target system have the version (or higher) of the framework you're compiling against.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by Tonny_Qitai Friday, December 28, 2018 2:11 AM
    Thursday, December 27, 2018 3:13 PM

All replies

  • It looks to me like your app is targeting a version of the framework that is newer than what the server has. Check the version of the framework and compare it against what your code requires. 

    I've also seen problems like this when you have mismatched System.Net.Http assemblies. This is a common problem because System.Net.Http is part of the framework AND ships as a NuGet package. The versioning on this assembly has been messed up for years causing havoc in apps. At a minimum apps generally have a binding redirect explicitly for this assembly so it uses the version that ships in NuGet. Secondly the build needs to ensure the correct version is part of the output directory. Thirdly the app needs to be sure it isn't mixing .NET Standard and .NET Framework code because that will throw things off.


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, December 26, 2018 4:16 PM
  • Thx for your quick response. CoolDadTx

    Actually, I am agree with your idea and i did below troubleshooting at later.

    =====================================================

    i just check  .net framework version used by application , it is .Net framework 4.0.

    and then ,i just check my local machine (the application works well on my local machine, it is win7) , .net framework version is  v4.0.30319.

    then , i just check windows2012 R2, .net framework version is same as my local machine, v4.0.30319

    and then, i just check the applicaiton, it is using same version of System.Net.Http module at runtime on win7 and win2012 r2.  win7 works well but failed on win2012 because the exception.

    =====================================================

    After that, i just compile the test application with .Net framework 4.6.1 and 4.3.4 System.net.http from NuGet package, and then upgrade win2012's .net framework to 4.6.1 and retry the test applicaiton, it work well at this time.

    +++++++++++++++++++++++++++++++++++++++++++++++++++++++

    so the conclusion is: there is something different on win2012R2 even there is same version's .Net framework and System.Net.Http.

    Hope we can get the root cause, i can upgrade everything on lab env, but It's hard to upgrade System.net.http module to 4.3.4  in really world, our code is running in IIS on customer side, IIS will load default System.net.http instead of NuGet package in runtime.

    Hope it doesn't confuse you and any comments or suggestion will very appreciate.
    Thursday, December 27, 2018 6:47 AM
  • The 4.0.30319 is the standard (original) v4 CLR. That is expected as it has never been updated since release. Unfortunately that doesn't map to the framework you targeted or installed. Since 4.x is installed over the top of itself you cannot use the file system to determine which version is installed. Personally I just go to Add/Remove Programs but you can also look it up in the registry.

    "IIS will load default System.net.http instead of NuGet package in runtime"

    No that isn't true. IIS doesn't load .NET at all. That is up to the runtime of your app. If your app is using System.Net.Http from NuGet then that binary is in your site bin folder and that version is the one that will be used. In your web.config there would be a binding redirect placed (when you added the package) to use this version. In theory just updating your app to use the NuGet package would be sufficient to work around this. Because of the binding redirect any dependent assemblies you have will use it as well irrelevant of the version of the framework installed.

    However the long term solution is to require that if your app is compiling against (for example) 4.6.1 then the target machine must have at least that as well. You cannot "downgrade" framework versions on the server as things won't work.

    But somewhere in our conversation we switched to IIS which doesn't make sense to me. Your call stack indicates a Winforms app. So the error is occurring in Winforms, not IIS. Your Winforms app will also need a dependency on System.Net.Http from NuGet or that the target system have the version (or higher) of the framework you're compiling against.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by Tonny_Qitai Friday, December 28, 2018 2:11 AM
    Thursday, December 27, 2018 3:13 PM
  • Thx,CoolDadTx, your explain is clear and make sense.

    The winforms app is just a test app used to troubleshooting the problem .

    Actually, our really project is a http module which is running in SharePoint server (IIS) and SharePoint will load  System.Net.Http by default as what i saw, so once our http module was load by SharePoint, our http module doens't have chance to  use new version from NuGet module.

    Luckey thing is: we found the root cause, you are right, the System.Net.Http on win2012r2 under GAC was upgrade to 4.7.3221.0 when install our http module via our install package, but System.Net.Http version (under Microsoft.NET\Framework64\v4.0.30319) is 4.6.26907.1,  so it cause imcompatible problem during runtime.

    we force to downgrade the System.Net.Http under GAC to 4.6.26907.1 , then the problem is gone.

    That's our problem, sorry to bother you.

    Friday, December 28, 2018 2:11 AM
  • How do you do that? I am having the same problem.

    Thanks!


    JTellier

    Tuesday, July 23, 2019 2:10 PM
  • Please post your question in a new thread instead of opening up an old one.

    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, July 23, 2019 2:25 PM