Task.Wait always returns false, but the task is completed

I use HttpClientwhile trying to execute the POST method in a Web API controller. The controller method is synchronous. I do like this:

var response = owin.HttpClient.PostAsJsonAsync(uri, body);

After that I call Wait:

var result = response.Wait(15000);

When I run this code, I see the completion of the http completion, but the value is resultalways false. What can I lose?

Edit: Now I tried the asynchronous approach, but that didn't help me either

public IHttpActionResult Add(Item item)
{
    var result = _db.AddItem(item);
    return Ok(result);
}

Testing Project:

TestServer _owinTestServer;
public async Task<HttpResponse message> Method1(string url, object body)
{
    return await 
   _owinTestServer.HttpClient.PostAsJsonAsync(url,body);
}

public async Task<ItemPreview> Method2(object body);
{
     return await Method1("..", body ).Result.Content.ReadAsAsync<ItemPreview>();
}

[TestMethod]
public void test1()
{
    Item item = new(...);
    Method2(item).Continue with(task => {// Never reach     here }
}

What am I doing wrong? When debugging, I see that the controller method returns a good answer, but it never returns to my test

+6
source share
4 answers

(.. .Result, .Wait()), .

.

async , .

async

[TestMethod]
public async Task test1() {
   //Arrange
   Item item = new Item(...);

   //Act
   var preview = await Method2(item);

   //Assert
   Assert.IsNotNull(preview);      
}

, .

Method1 asyn/await, , Task,

TestServer _owinTestServer;
public Task<HttpResponse> Method1(string url, object body) {
    return _owinTestServer.HttpClient.PostAsJsonAsync(url, body);
}

Method2 await Method1, .

public async Task<ItemPreview> Method2(object body) {
    var response = await Method1("..", body );
    return await response.Content.ReadAsAsync<ItemPreview>();
}
+10

, . . PostAsJsonAsync , await :

var response = await owin.HttpClient.PostAsJsonAsync(uri, body);

.

0

, ...

var response = owin.HttpClient.PostAsJsonAsync(uri, body);
var result = response.Wait(15000);
var itemPreview = response.Result.Content.ReadAsAsync<ItemPreview>();
...

, 15 . , , A.K.A, . (n) false.

ReadAsAsync...

var response = owin.HttpClient.PostAsJsonAsync(uri, body);
var itemPreview = response.Result.Content.ReadAsAsync<ItemPreview>();

, , , . (n) , true, ReadAsAsync() . - , HttpClient, ReadAsAsync AggregateException InnerException TaskCanceledException.

, owin.HttpClient, HttpClient. , owin, , WebApi, , . , Add (Item item) WebApi db.AddItem(item) ItemPreview, :

[TestMethod]
public void test1()
{
    Item item = new(...);
    var uri = "..";
    var client = new HttpClient();
    var response = client.PostAsJsonAsync(uri, item);
    var itemPreview = response.Result.Content.ReadAsAsync<ItemPreview>();

    /*  The things to happen once you have item preview */

}
0

, _db.AddItem false. , , .

TestServer _owinTestServer;
public async Task<HttpResponse message> Method1(string url, object body)
{
    return await _owinTestServer.HttpClient.PostAsJsonAsync(url,body);
}

public async Task<ItemPreview> Method2(object body);
{
     return await Method1("..", body ).Result.Content.ReadAsAsync<ItemPreview>();
}

[TestMethod]
public void test1()
{
    Item item = new(...);
    await Method2(item).ContinueWith(task => {// Never reach     here }
}

Method2 Async, ContinueWith , , .

0

Source: https://habr.com/ru/post/1016818/


All Articles