question

CraigMuckleston avatar image
0 Votes"
CraigMuckleston asked ·

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

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;
     }
dotnet-aspnetcore-webapidotnet-aspnet-webapi
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DuaneArnold-0443 avatar image
0 Votes"
DuaneArnold-0443 answered ·

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

· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks DuaneArnold,

It is in the Nuget package though (see attachment)

67814-package.png


0 Votes 0 ·
package.png (9.8 KiB)
DuaneArnold-0443 avatar image
0 Votes"
DuaneArnold-0443 answered ·

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/




·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.