Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Consuming a homemade rest service in asp.net webform

Consuming a homemade rest service in asp.net webform

Scheduled Pinned Locked Moved ASP.NET
databasejsoncsharpasp-netwcf
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wilcodk
    wrote on last edited by
    #1

    Hi I've created my own rest service. For test purpose i've used Soap UI for testing of the service. Now i try to consume this service in a webform. On the controller i can see that it has processed a call, so the service request gets through to my service. By debugging i can see that the rest server gets the data from the database. When i try consuming the service in a asp.net 4.5 webform it hangs... it just hangs on this statement

    var response = await client.GetAsync("ServerConfig");

    My request code looks like this:

    private WebApiClientOptions options = new WebApiClientOptions("http://localhost:54990/api", "ServerConfig");

    protected void Page\_Load(object sender, EventArgs e)
    {
        List list = null;
    
        list = this.Index().Result;
      
    }
    
    public async Task\> Index()
    {
        List list = null;
    
        using (WebApiClient client = new WebApiClient(options))
        {
            var liste = await client.GetManyAsync();
    
        }
    
        return list;
    }
    

    i hope there is a clever person WHO can help a complete newbiee

    Yours Wilco

    Richard DeemingR 1 Reply Last reply
    0
    • W wilcodk

      Hi I've created my own rest service. For test purpose i've used Soap UI for testing of the service. Now i try to consume this service in a webform. On the controller i can see that it has processed a call, so the service request gets through to my service. By debugging i can see that the rest server gets the data from the database. When i try consuming the service in a asp.net 4.5 webform it hangs... it just hangs on this statement

      var response = await client.GetAsync("ServerConfig");

      My request code looks like this:

      private WebApiClientOptions options = new WebApiClientOptions("http://localhost:54990/api", "ServerConfig");

      protected void Page\_Load(object sender, EventArgs e)
      {
          List list = null;
      
          list = this.Index().Result;
        
      }
      
      public async Task\> Index()
      {
          List list = null;
      
          using (WebApiClient client = new WebApiClient(options))
          {
              var liste = await client.GetManyAsync();
      
          }
      
          return list;
      }
      

      i hope there is a clever person WHO can help a complete newbiee

      Yours Wilco

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      You have a classic deadlock. await signs the rest of the method up to run as a continuation when the awaited task completes. By default, it will be scheduled to run in the same execution context as the caller. Calling .Result on a task blocks the current thread until the task has completed. But the task can't complete until the continuation has run on the current execution context. Which can't happen until the current thread is un-blocked. Which can't happen until the task has completed. The quick-and-dirty fix is to add .ConfigureAwait(false) to your awaited tasks. That allows the continuation to run on a different thread:

      var liste = await client.GetManyAsync().ConfigureAwait(false);

      The best solution would be to move your work to a proper task-returning async method, and register that as an async task:

      private void Page_Load(object sender, EventArgs e)
      {
      RegisterAsyncTask(new PageAsyncTask(LoadAsync));
      }

      private async Task LoadAsync()
      {
      List<ServerConfig> list = await Index();
      ...
      }

      Using Asynchronous Methods in ASP.NET 4.5[^]


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      W 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        You have a classic deadlock. await signs the rest of the method up to run as a continuation when the awaited task completes. By default, it will be scheduled to run in the same execution context as the caller. Calling .Result on a task blocks the current thread until the task has completed. But the task can't complete until the continuation has run on the current execution context. Which can't happen until the current thread is un-blocked. Which can't happen until the task has completed. The quick-and-dirty fix is to add .ConfigureAwait(false) to your awaited tasks. That allows the continuation to run on a different thread:

        var liste = await client.GetManyAsync().ConfigureAwait(false);

        The best solution would be to move your work to a proper task-returning async method, and register that as an async task:

        private void Page_Load(object sender, EventArgs e)
        {
        RegisterAsyncTask(new PageAsyncTask(LoadAsync));
        }

        private async Task LoadAsync()
        {
        List<ServerConfig> list = await Index();
        ...
        }

        Using Asynchronous Methods in ASP.NET 4.5[^]


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        W Offline
        W Offline
        wilcodk
        wrote on last edited by
        #3

        Thanks I got the registerAsyncTask to Work :)

        Yours Wilco

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups