Customer Engagement & Dynamics CRM Forum

Expand all | Collapse all

Could not load file or assembly 'System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

  • 1.  Could not load file or assembly 'System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

    SILVER CONTRIBUTOR
    Posted Jul 16, 2019 08:57 AM
    Hi,

    I am working on Dynamics 365 on-premise. One of the custom workflow Activities makes a call to a Web API. Then I got following exception:

    System.IO.FileNotFoundException: Could not load file or assembly 'System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

    System.Net.Http.Formatting came from Microsoft.AspNet.WebApi.Client (NuGet), and in assembly System.Net.Http.Formatting.dll.

    System.Net.Http.Formatting.dll is in the project package folder of course since it comes with NuGet. However it does seem in Dynamics server GAC or someway needed by the custom workflow.

    Any suggestions?

    Thank you very much,




    ------------------------------
    Junbin Duan
    Senior Application Developer
    National Research Council Canada
    Ottawa ON
    ------------------------------


  • 2.  RE: Could not load file or assembly 'System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

    TOP CONTRIBUTOR
    Posted Jul 16, 2019 09:27 AM
    It's been a while since I worked on an on-premise deployment, but I think you are on the right track.  The DLL is required to be registered and available on the server in order for the custom workflow activity to work.  Couple of things to check.

    1. If it is registered, are they the same version between the development PC and the server?
    2. If you have your async server process segmented to another server from the web server, make sure the BOTH have the .DLL registered in the GAC

    I seem to recall it being a good idea to also have the actual .DLL file in a couple of places on the server, but i can't recall the folders (and I don't have access to an on-premise install to verify, sorry).

    Good luck!

    ------------------------------
    Chris Harrington
    Solutions Architect
    PowerObjects, an HCL Company
    ------------------------------



  • 3.  RE: Could not load file or assembly 'System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

    TOP CONTRIBUTOR
    Posted Jul 16, 2019 10:20 AM
    Edited by Sunil Raheja Jul 16, 2019 10:22 AM
    Good old on-prem days!!

    I do remember we used to place any dlls in the folder server -> bin -> assembly
    And sometimes have to register it with the GAC

    Also make sure you are not registering your plugin/workflow in Sandbox. it should be running from the disk.

    ------------------------------
    Sunil Raheja
    Aha Apps
    Glen Allen VA
    https://www.ahaapps.com/microsoft-dynamics-crm/
    ------------------------------



  • 4.  RE: Could not load file or assembly 'System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

    SILVER CONTRIBUTOR
    Posted Jul 16, 2019 11:48 AM
    Hi Chris and Sunil,

    Thank you both very much for the response.

    Registering the system assembly into server GAC is a good idea. The only issue is that there is not gacutil tool installed on the server. So I tried to use ILMerge tool to merge the system assemblies into the custom assembly, and it works too.

    I will evaluate both of the approaches to see which one can better suit my situation. Any suggestions will be great appreciated.

    Best regards,
    Junbin

    ------------------------------
    Junbin Duan
    Senior Application Developer
    National Research Council Canada
    Ottawa ON
    ------------------------------



  • 5.  RE: Could not load file or assembly 'System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

    TOP CONTRIBUTOR
    Posted Jul 17, 2019 07:26 AM

    Junbin

    What function are you using? You can try to find an open source equivalent and compile that code into your dll.

    Cheers!



    ------------------------------
    Rex Kenley Tan, MCSA: Web Applications
    Tallmadge OH
    https://www.youracclaim.com/users/rex-kenley-tan

    *Always be CURRENT with JavaScript & C#, NEVER be obsolete.

    DISCLAIMER: All views expressed on this site are my own and DO NOT represent the opinions of ANY entity whatsoever with which I have been, am now, or will be affiliated.
    ------------------------------



  • 6.  RE: Could not load file or assembly 'System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

    SILVER CONTRIBUTOR
    Posted Jul 17, 2019 10:43 AM
    Hi Rex,

    Well, the "missing" system assemblies are automatically copied to the bin folder. So either registering them on server, or merging them into my assembly will work

    The source code for the similar or equivalent system assemblies is possible to be found and compiled into the project, however, since assemblies are already here, it does not seem necessary to use the source code at this point.

    The "missing" assemblies are:

    • System.Net.Http.Formatting.dll
    • Newtonsoft.Json.dll
    Looks like they do exist in Azure.

    Cheers!

    ------------------------------
    Junbin Duan
    Senior Application Developer
    National Research Council Canada
    Ottawa ON
    ------------------------------



  • 7.  RE: Could not load file or assembly 'System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

    TOP CONTRIBUTOR
    Posted Jul 17, 2019 11:21 AM

    Junbin

    Be aware that NewtonSoft is not sandbox friendly. This is a big consideration if you decided to go online.

    There is a nuget package called SimpleJson which works fairly well and can replace NewtonSoft.

    Please let me know if the Azure method works, that's the one thing I didn't try.

    Cheers!



    ------------------------------
    Rex Kenley Tan, MCSA: Web Applications
    Tallmadge OH
    https://www.youracclaim.com/users/rex-kenley-tan

    *Always be CURRENT with JavaScript & C#, NEVER be obsolete.

    DISCLAIMER: All views expressed on this site are my own and DO NOT represent the opinions of ANY entity whatsoever with which I have been, am now, or will be affiliated.
    ------------------------------



  • 8.  RE: Could not load file or assembly 'System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

    SILVER CONTRIBUTOR
    Posted Jul 17, 2019 02:29 PM
    Hi Rex,

    Thank you for the remind.  I did not particularly use Newtonsoft.Json. It came when adding Microsoft.AspNet.WebApi.Client NuGet package. It looks like WebApi has dependents on it.

    Cheers,
    Junbin

    ------------------------------
    Junbin Duan
    Senior Application Developer
    National Research Council Canada
    Ottawa ON
    ------------------------------



  • 9.  RE: Could not load file or assembly 'System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

    Posted Jul 18, 2019 04:03 AM
    You do not need WebAPI.Client to call the API, though it makes it easier. You could just use XmlHttpRequest.

    Rex also makes a valid point on that third party libraries are not sandbox friendly and will be difficult to use in the cloud.

    You can use ILMerge, but in our experience that makes it impossible to debug the merged assembles.

    We are going for shared projects, to compile dependencies directly into the assembly. It requires that we have the source code for all dependencies though.

    Cheers

    ------------------------------
    Peder Møller Wagner
    Senior Software Architect
    Danske Bank Group
    København K
    ------------------------------



  • 10.  RE: Could not load file or assembly 'System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

    SILVER CONTRIBUTOR
    Posted Jul 18, 2019 07:46 AM
    Hi Peder,

    You have a great point. Thank you.

    I am using HttpClient to call A Web API in plugin (C# code), not from JavaScript. And I have to pass a complex object as parameter so it is a post action. Do you have any suggestions?

    Calling ILMerge in post build event works, but does cause trouble for plugin debugging.

    Thank you very much...

    ------------------------------
    Junbin Duan
    Senior Application Developer
    National Research Council Canada
    Ottawa ON
    ------------------------------



  • 11.  RE: Could not load file or assembly 'System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

    Posted 30 days ago
    Sorry, you are right. The C# equivalent of XmlHttpRequest is using HttpWebRequest (deprecated) or WebClient. They are in the System.Net namespace and are included in standard .Net.

    Rex' link should help you get started.

    ------------------------------
    Peder Møller Wagner
    Senior Software Architect
    Danske Bank Group
    København K
    ------------------------------



  • 12.  RE: Could not load file or assembly 'System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

    SILVER CONTRIBUTOR
    Posted 30 days ago
    Hi,

    You can use the following class which is sandbox friendly to Serialize/Deserialize JSON objects:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization.Json;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace YourNameSpace
    {
        public class SerializerWrapper
        {
            public static string Serialize<T>(T srcObject)
            {
                using (System.IO.MemoryStream serializeStream = new System.IO.MemoryStream())
                {
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
                    serializer.WriteObject(serializeStream, srcObject);
    
                    string jsonString = Encoding.Default.GetString(serializeStream.ToArray());
                    return jsonString;
                }
            }
    
            public static T Deserialize<T>(string jsonObject)
            {
                using (System.IO.MemoryStream DeserializeStream = new System.IO.MemoryStream())
                {
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
    
                    System.IO.StreamWriter writer = new System.IO.StreamWriter(DeserializeStream);
                    writer.Write(jsonObject);
                    writer.Flush();
                    DeserializeStream.Position = 0;
    
                    T deserializedObject = (T)serializer.ReadObject(DeserializeStream);
                    return deserializedObject;
                }
            }
        }
    }


    ------------------------------
    Hasan Bazerbashi
    Programmer Analyst
    Cineplex Inc.
    Toronto ON
    ------------------------------



  • 13.  RE: Could not load file or assembly 'System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

    TOP CONTRIBUTOR
    Posted 30 days ago

    Try this

    https://nishantrana.me/2017/04/27/using-webclient-to-call-external-service-from-crm-online-sandboxed-plugin-http-post-and-json/



    ------------------------------
    Rex Kenley Tan, MCSA: Web Applications
    Tallmadge OH
    https://www.youracclaim.com/users/rex-kenley-tan

    *Always be CURRENT with JavaScript & C#, NEVER be obsolete.

    DISCLAIMER: All views expressed on this site are my own and DO NOT represent the opinions of ANY entity whatsoever with which I have been, am now, or will be affiliated.
    ------------------------------



  • 14.  RE: Could not load file or assembly 'System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

    SILVER CONTRIBUTOR
    Posted 30 days ago
    Hi Rex, Hasan, Peder,

    Thank you all for the suggestion. With the customized deseriialize method, I do not have to use NewtonSoft's converter, though Http.Net.Http.Formatting still needs to load.

    Cheers.


    ------------------------------
    Junbin Duan
    Senior Application Developer
    National Research Council Canada
    Ottawa ON
    ------------------------------



  • 15.  RE: Could not load file or assembly 'System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

    Posted 30 days ago
    Nishantrana's example is not using Formatting as far as I can see.

    Are you sure it is not just some leftover references for Microsoft.AspNet.WebApi hidden somewhere?
    Can you see what part of your code is using it?

    ------------------------------
    Peder Møller Wagner
    Senior Software Architect
    Danske Bank Group
    København K
    ------------------------------



  • 16.  RE: Could not load file or assembly 'System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

    SILVER CONTRIBUTOR
    Posted 29 days ago
    Yes, I found it used in quite a few places such as WebClient.PostAsJsonAsync. I will try to replace them with PostAsync.

    Thank you so much,

    ------------------------------
    Junbin Duan
    Senior Application Developer
    National Research Council Canada
    Ottawa ON
    ------------------------------



  • 17.  RE: Could not load file or assembly 'System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

    SILVER CONTRIBUTOR
    Posted 25 days ago
    Hi Peder, Hasan and Rex,

    It works as I expected now.

    Thank you all for the suggestions and the helpful information.

    Cheers!




    ------------------------------
    Junbin Duan
    Senior Application Developer
    National Research Council Canada
    Ottawa ON
    ------------------------------



If you've found this thread useful, dive deeper into User Group community content by role