1

I'm trying to build an MVC that requests through a PCL to a WebApi. I am sending a get requests and getting stuck on the awaiting for the response. Postman returns the correct values. I also don t get exceptions on send. The 3 projects are both on the same solution.

PCL

        HttpResponseMessage httpResponse = null;
        try
        {
             httpResponse = await _http.GetAsync( "http://localhost:43818/api/values" );

        }
        catch (Exception e)
        {
            var meessage = e.Message;
            var stack = e.StackTrace;

        }

        if (httpResponse.StatusCode == HttpStatusCode.OK)
        {
            string json = await httpResponse.Content.ReadAsStringAsync( );
        }

So the issue is that in the PCL, it's doesn pass the await, it gets stuck.

MVC

       var result = apiClient.GetIndex( );

Web Api

public class ValuesController : ApiController
{
    // GET api/values
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }
}

Also, how do i wait in my MVC for the response before rendering the controller view

6
  • What is your definition of PCL? Commented Oct 13, 2015 at 15:55
  • @ErikPhilips Portable Class Library, i m sorry for not mentioning Commented Oct 13, 2015 at 18:04
  • Can you paste signature of your PCL and all its methods ? Commented Oct 13, 2015 at 18:26
  • I see an await. It's really important when posting async code to post the entire chain of code using async/await. Commented Oct 13, 2015 at 18:50
  • The apiClient.GetIndex( ); is actually the PCL function with a return Commented Oct 13, 2015 at 20:05

2 Answers 2

3

In your Class library (PCL), Create method GetIndex as below,

    public async Task GetIndexAsync()
    {
        HttpResponseMessage httpResponse = null;
        try
        {
            _http.BaseAddress = new Uri("http://localhost:43818/");
            httpResponse = await _http.GetAsync("api/values");

        }
        catch (Exception e)
        {
            var meessage = e.Message;
            var stack = e.StackTrace;

        }

        if (httpResponse.StatusCode == HttpStatusCode.OK)
        {
            string json = await httpResponse.Content.ReadAsStringAsync();
        }
    }

And In MVC calling method as below,

var result = apiClient.GetIndexAsync().Wait();

which solved both your problems.

Sign up to request clarification or add additional context in comments.

1 Comment

it worked, just that you can't store the result in var, a task type must be set and from there you can fetch the result. Thank you for your answer!
0

Ok so i found the best sollution. Blocking threads is not a very good idea.

This is the fix

PCL

public async Task<HttpResponseMessage> Register()
{
    HttpRequestMessage request = new HttpRequestMessage
    {
        RequestUri = new Uri( _http.BaseAddress, "account/register/" ),
        Method = HttpMethod.Post,
        Content = new StringContent( "{\"Email\": \"[email protected]\",\"Password\": \"Password!1\",\"ConfirmPassword\": \"Password!1\"}",
        Encoding.UTF8,
        _contentType
        ),
    };


        HttpResponseMessage response = new HttpResponseMessage();

        try
        {
            response = await _http.SendAsync( request, CancellationToken.None );
        }
        catch (Exception e)
        {
            Debugger.Break();
        }

    return response;
}

MVC Client

    public async Task<ViewResult> Index( )
    {
        var thisTask = await Api.Register( );

        return View();
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.