HttpClient.PostAsync - System.Net.Http.Formatting exception

Craig Muckleston 161 Reputation points
2021-02-13T11:55:39.693+00:00

I am running a .NET Standard 2 project. I have added the Microsoft.AspNet.WebApi.Client (5.2.7) package. My call to HttpClient.GetAsync are working fine, but as soon as I try HttpClient.PostAsync, HttpClient.PostAsJsonAsync (or event the PutAsync versions), this error is thrown:

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, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35

I call the method like this:
private async Task<T> Add<T>(string path, T item) where T : class
{
try
{
return await _httpClientService
.Add(path, item)
.ConfigureAwait(false);
}
catch (Exception ex) //the exception is thrown here
{
throw ex;
}
}

   //and this is the service
    public async Task<T> Add<T>(string path, T item)
    {
        if (string.IsNullOrEmpty(baseUrl))
        {
            return default(T);
        }

        T result = default;

        try
        {
            SetAuthorizationHeader(httpClient);
            var response = await httpClient.PutAsync(path, item, new JsonMediaTypeFormatter()).ConfigureAwait(false); //without this line, the function is called. When this line is included, this method does not get called

            //response.EnsureSuccessStatusCode();

            //await response.Content.ReadAsStringAsync().ContinueWith((Task<string> x) =>
            //{
            //    if (x.IsFaulted)
            //    {
            //        this._loggingService.Fatal($"{nameof(Add)} failed");
            //        throw x.Exception;
            //    }

            //    result = JsonConvert.DeserializeObject<T>(x.Result);

            //});
        }
        catch (Exception ex)
        {
            throw ex;
        }

        return result;
    }
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,199 questions
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
301 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Duane Arnold 3,216 Reputation points
    2021-02-13T22:45:41.433+00:00

    The DLL for the package you installed is not in the Bin folder, and .NET cannot find the DLL.


  2. Duane Arnold 3,216 Reputation points
    2021-02-14T13:15:42.71+00:00

    The place to go look is the Bin folder on the hard-drive for the DLL. Is
    It there? It could be a dependency DLL the main DLL needs is not in the Bin folder. Maybe another version of the main DLL is there, and .NET is looking for the DLL version it knows about.

    https://www.dependencywalker.com/

    0 comments No comments