Consuming a homemade rest service in asp.net webform
-
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
-
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
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
-
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