1
我執行jquery ajax請求。目標是一個ashx處理程序。 問題是在後端調用了第二個異步請求。由於這個事實,我將陷入jquery ajax請求的「錯誤」情況(錯誤:「異步模塊或處理程序在異步操作仍未完成時完成」)。使用Async c#服務處理程序的Ajax請求
下面的代碼:
Ajax調用:
$.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) {
}
});
後端:
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>");
}
在下面的方法,這將在VAR發送/ updatecalendarinvitation被稱爲出現的問題dcr = await discClient.DiscoverCapabilityAsync(capability); 通過執行此操作,將會調用「EndProcessRequest」,遺憾的是太快。我只是希望它被稱爲,當日歷圖像製作時...
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;
後端代碼工作正常,方法都將被調用。但我只想要正確的反饋。 我期待您的反饋。 謝謝!
很難從發佈的代碼中知道,但它看起來像各種「客戶端」類可能採取「動作」回調。在這種情況下,'async' lambda將成爲'async void',這可能會導致您看到的錯誤。 –