public void BeginSaveDataModel(DataModelEntity dataModelEntity, SaveDataModelCallback callback) { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create( new Uri(serverBaseUri, relativeSaveDataModelURI)); request.Method = "POST"; saveDataModelCallback = callback; request.BeginGetRequestStream(delegate(IAsyncResult result) { HttpWebRequest webRequest = result.AsyncState as HttpWebRequest; webRequest.ContentType = "text/xml"; Stream requestStream = webRequest.EndGetRequestStream(result); StreamWriter streamWriter = new StreamWriter(requestStream); // Serializar los datos a enviar. DataContractSerializer serializer = new DataContractSerializer(typeof(DataModelEntity)); serializer.WriteObject(streamWriter.BaseStream, dataModelEntity); streamWriter.Close(); // Invocar una llamada asíncrona. Un Callback será invocado en un hilo ejecutándose en segundo plano. request.BeginGetResponse(new AsyncCallback(EndSaveDataModel), request); } , request); }
public void EndSaveDataModel(System.IAsyncResult result) { SaveDataModelResult callResult = new SaveDataModelResult(); try { HttpWebRequest request = (HttpWebRequest)result.AsyncState; HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result); // Crear la instancia del deserializador. DataContractSerializer serializer = new DataContractSerializer(typeof(bool)); // Deserializar el resultado. bool deserializedResult = (bool)serializer.ReadObject(response.GetResponseStream()); callResult.Success = deserializedResult; } catch (SerializationException serializationError) { Debug.WriteLine(serializationError.Message); } catch (WebException webError) { Debug.WriteLine(webError.Message); } // Si existe callback, invocarlo. if (saveDataModelCallback != null) { saveDataModelCallback(callResult); saveDataModelCallback = null; } }
public void BeginSaveDataModel(DataModel dataModel, SaveDataModelCallback callback) { try { DataModelEntity dataModelEntity = Utilities.ConvertDataModelToEntity(dataModel); dataModelEntity.IdStore = customServiceId; CircularReferencesManager.BrokeDataModelCircularReferences(dataModelEntity); SilverlightServicesClient client = Utils.SilverlightServicesClient; client.BeginSaveDataModel(dataModelEntity, callback); } catch (Exception) { throw; } }