Reputation: 145
I execute an jquery ajax request. The destination is a ashx handler. The problem is that on the backend a second async request is called. Due of this fact, I'll fall in the "error" case of the jquery ajax request (Error: "An asynchronous module or handler completed while an asynchronous operation was still pending").
Here the code:
Ajax call:
$.ajax({
type: "POST",
cache: false,
url: 'ajax/HandlerAsync.ashx',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: '{method: "'+ data.value +'" , meeting: '+ meeting +' }',
success: function (data, status, xhr) {
},
error: function (xhr, status, error) {
}
});
Backend:
public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
{
httpContext = context;
string json = new StreamReader(context.Request.InputStream).ReadToEnd();
dynamic test = JsonConvert.DeserializeObject(json);
AjaxRequest ajax = JsonConvert.DeserializeObject<AjaxRequest>(json);
Async async = new Async(cb, extraData);
// store a little context for us to use later as well
async.context = context;
if (String.IsNullOrEmpty(ajax.meeting.EventId))
{
var calendarEntryId = sendCalendarInvitations((ajax.meeting));
}
else
{
updateCalendarInvitations(ajax.meeting);
}
async.SetCompleted();
return async;
}
public void EndProcessRequest(IAsyncResult result)
{
// Finish things
Async async = result as Async;
async.context.Response.Write("<H1>This is an <i>Asynchronous</i> response!!</H1>");
}
The problem occurs at the following method, which will be called in the send/updatecalendarinvitation on var dcr = await discClient.DiscoverCapabilityAsync(capability); By executing this, the "EndProcessRequest" will be called, unfortunately too soon. I just want it to be called, when the calendarinvitations are made...
AuthenticationContext authContext = new AuthenticationContext(string.Format("{0}/{1}", WebConfigurationManager.AppSettings["AuthorizationUri"], SettingsHelper.TenantId));
DiscoveryClient discClient = new DiscoveryClient(SettingsHelper.DiscoveryServiceEndpointUri,
async () =>
{
var authResult = await authContext.AcquireTokenByRefreshTokenAsync(this.refreshToken,
new ClientCredential(SettingsHelper.ClientId, SettingsHelper.AppKey),
SettingsHelper.DiscoveryServiceResourceId);
return authResult.AccessToken;
});
var dcr = await discClient.DiscoverCapabilityAsync(capability);
OutlookServicesClient exClient = new OutlookServicesClient(dcr.ServiceEndpointUri,
async () =>
{
var authResult = await authContext.AcquireTokenByRefreshTokenAsync(this.refreshToken,
new ClientCredential(SettingsHelper.ClientId, SettingsHelper.AppKey),
dcr.ServiceResourceId);
return authResult.AccessToken;
});
return exClient;
The backend code works fine, the methods will all be called. But I just want the right feedback. I looking forwand for your feedback. Thanks!
Upvotes: 1
Views: 526
Reputation: 145
Ok solved. I wrapped the context (last code section from above) in a seperate Task, which don't fire the "EndprocessRequest".
var client = System.Threading.Tasks.Task.Run<OutlookServicesClient>(() => {
return o365Api.CreateOutlookClientAsync("Calendar");
});
Upvotes: 2