I am writing an ASP.NET MVC 5 application that, among other things, uses web services to receive / process some data.
The application data stream is as follows: MVC Action -> Service B -> ExtSvc, which is an asynchronous web service wrapper
Here are some examples:
public class ExtSvc
{
private Task<Response> ProcessExtRequestAsync(Request request)
{
TaskCompletionSource<Response> taskCompletionSource =
AsyncServiceClientHelpers.CreateSource<Response>(request);
ProcessRequestCompletedEventHandler handler = null;
handler =
(sender, e) =>
AsyncServiceClientHelpers.TransferCompletion(
taskCompletionSource,
e,
() => e.Result,
() => this.Service.ProcessRequestCompleted -= handler);
this.Service.ProcessRequestCompleted += handler;
try
{
this.Service.ProcessRequestAsync(request, taskCompletionSource);
}
catch (Exception)
{
this.Service.ProcessRequestCompleted -= handler;
taskCompletionSource.TrySetCanceled();
throw;
}
return taskCompletionSource.Task;
}
public async Task<Response> UpdateRequest(some arguments)
{
var response = await this.ProcessExtRequestAsync(request)
.ConfigureAwait(false);
return response;
}
}
Class B is the one that uses ExtSvc in a synchronous way.
public class B
{
public ExtSvc service {get; set;}
public Response Update(arguments)
{
var result = this.ExtSvc.UpdateRequest(arguments).Result;
return result
}
}
Finally, the MVC action (also synchronous )
public ActionResult GetResponse(int id)
{
B.Update(id);
return View(...);
}
The described stream throws an error
The first exception of type "System.InvalidOperationException" exception occurred in System.Web.dll
: . . , , <% @Page Async = "true" % > . "", ASP.NET. , .
ExtSvc: this.Service.ProcessRequestAsync(request, taskCompletionSource); ProcessRequestAsync void
:
"", ASP.NET
, GetResponse MVC ( async/await), B, ExtSvc , .
:
B (- , ), Task<Response> Response, , async/await , ?