/// <summary> /// Call an async webservice method by using the specific info. /// </summary> /// <param name="e"></param> protected virtual void RunAsyncMethod(BeforeRunAsyncMethodEventArgs e) { if (e.ServiceState.AddToAsyncList) { if (BeforeRunAsyncMethod != null) { BeforeRunAsyncMethod(this, e); } ChangeCacheKeyAndPreferredHostForSpecifiedMethod(e.WebService, e.ServiceType, e.ServiceState, e.Request); lock (asyncRequests) { asyncRequests[e.ServiceState] = e.Request; } // Run async method now e.WebService.GetType().InvokeMember( e.ServiceState.MethodName + "Async", System.Reflection.BindingFlags.InvokeMethod, null, e.WebService, new object[] { e.Request, e.ServiceState } ); } }
protected virtual void OnAfterCompleted(ServiceOperationEventArgs e) { object request = null; lock (asyncRequests) { if (asyncRequests.ContainsKey(e.MsnServiceState)) { request = asyncRequests[e.MsnServiceState]; asyncRequests.Remove(e.MsnServiceState); } } if (e.MsnServiceState.AddToAsyncList) { lock (asyncStates) asyncStates.Remove(e.MsnServiceState); } if (e.AsyncCompletedEventArgs.Cancelled) { Trace.WriteLineIf(Settings.TraceSwitch.TraceInfo, "Async method cancelled:\r\n" + "Service: " + e.WebService.ToString() + "\r\n" + "MethodName: " + e.MsnServiceState.MethodName + "\r\n" + "PartnerScenario: " + e.MsnServiceState.PartnerScenario); } else if (e.AsyncCompletedEventArgs.Error != null) { BeforeRunAsyncMethodEventArgs reinvokeArgs = null; if (e.AsyncCompletedEventArgs.Error is WebException) { WebException webException = e.AsyncCompletedEventArgs.Error as WebException; HttpWebResponse webResponse = webException.Response as HttpWebResponse; if (webResponse != null && request != null) { if (webResponse.StatusCode == HttpStatusCode.MovedPermanently) { DeltasList deltas = NSMessageHandler.ContactService.Deltas; if (deltas == null) { throw new MSNPSharpException("Deltas is null."); } string redirctURL = webResponse.Headers[HttpResponseHeader.Location]; string preferredHostKey = e.WebService.ToString() + "." + e.MsnServiceState.MethodName; lock (deltas.SyncObject) { deltas.PreferredHosts[preferredHostKey] = FetchHost(redirctURL); deltas.Save(); } e.WebService.Url = redirctURL; reinvokeArgs = new BeforeRunAsyncMethodEventArgs(e.WebService, e.ServiceType, e.MsnServiceState, request); } } } if (reinvokeArgs == null) { OnServiceOperationFailed(this, new ServiceOperationFailedEventArgs(e.MsnServiceState.MethodName, e.AsyncCompletedEventArgs.Error)); } else { RunAsyncMethod(reinvokeArgs); } } else { // HandleServiceHeader if (NSMessageHandler.MSNTicket != MSNTicket.Empty && (e.ServiceType == MsnServiceType.AB || e.ServiceType == MsnServiceType.Sharing)) { HandleServiceHeader(e.WebService, e.ServiceType, e.MsnServiceState); } // Fire event if (AfterCompleted != null) { AfterCompleted(this, e); } } }