Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CS1061 'HttpClient' does not contain a definition for 'PostAsJsonAsync' For asp.net core project #3849

Closed
hanslai opened this issue Jul 30, 2017 · 8 comments
Labels
Pri3 Priority 3
Milestone

Comments

@hanslai
Copy link

hanslai commented Jul 30, 2017

Not sure if the is the right place to post this issue.

When try to following the Doc for "Integration Testing" example https://github.com/aspnet/Docs/tree/master/aspnetcore/mvc/controllers/testing/sample, for a pure asp.net core project, I got the following error.

CS1061 'HttpClient' does not contain a definition for 'PostAsJsonAsync'

Here are the nudge package was including.

<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
  <PackageReference Include="Microsoft.AspNetCore.TestHost" Version="1.1.2" />
  <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
  <PackageReference Include="System.Net.Http" Version="4.3.2" />
  <PackageReference Include="xunit" Version="2.2.0" />
  <PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />

I have to add the package below to make it to work.
<PackageReference Include="WinInsider.System.Net.Http.Formatting" Version="1.0.5" />

But this does not seem right because the "TestingControllersSample" does not need to include it.

Is this a packing versioning problem? The "TestingControllersSample" is using "System.Net.Http Version"4.1.1"

Thanks

@Rick-Anderson
Copy link
Contributor

I'm guessing the sample is missing a using statement.
WinInsider.System.Net.Http.Formatting is not an ASP.NET Core assembly.
We'll investigate, thanks for the report.

@Rick-Anderson Rick-Anderson added this to the Backlog milestone Jul 31, 2017
@Rick-Anderson Rick-Anderson added the Pri3 Priority 3 label Jul 31, 2017
@hanslai
Copy link
Author

hanslai commented Aug 1, 2017

@Rick-Anderson I am not sure if it is because of a missing using statement. I believe it is something else, below are the using statements

using Newtonsoft.Json;
using System.Linq;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;
using System.Net;

@hanslai
Copy link
Author

hanslai commented Aug 20, 2017

@Rick-Anderson I just read this Where is PostAsJsonAsync method in ASP.NET Core?
on stackoverflow, and realized PostAsJsonAsync is not part of the aspnet.core.

So, I call end up unreference WinInsider.System.Net.Http.Formatting, but calling JsonConvert.SerializeObject(); first then call PostAsync instead.
But I still don't understand how the example https://github.com/aspnet/Docs/blob/master/aspnetcore/mvc/controllers/testing/sample/TestingControllersSample/tests/TestingControllersSample.Tests/IntegrationTests/ApiIdeasControllerTests.cs works by calling PostAsJsonAsync

My problem is solved, but I am not sure if I should close this issue, so please close it if you see fit.

Thanks

@Rick-Anderson
Copy link
Contributor

@danroth27
Copy link
Member

@hanslai hanslai closed this as completed Nov 7, 2017
@SofiaSanabria
Copy link

I'm new here, I was pretty confused following that tutorial (Call a Web API From a .NET Client (C#))
I wrote this solution based on @Rick-Anderson comments, I hope this help.

using System.Web.Script.Serialization;
.
.
.
static async Task CreateProductAsync(Product product)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
var myContent = jss.Serialize(product);
var httpContent = new StringContent(myContent, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync("api/products", httpContent);
response.EnsureSuccessStatusCode();

        //return URI of the create resource.
        return response.Headers.Location;
    }

@optimum88
Copy link

Hi!
Just install Microsoft.AspNet.WebApi.Client nuget package

@sfshakil
Copy link

Hi!
Just install Microsoft.AspNet.WebApi.Client nuget package

correct answer.thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Pri3 Priority 3
Projects
None yet
Development

No branches or pull requests

6 participants