Ejemplo n.º 1
0
 public WcfResponse Update(UpdateRequest request)
 {
   Csla.Server.DataPortal portal = new Csla.Server.DataPortal();
   object result;
   try
   {
     result = portal.Update(request.Object, request.Context, true).Result;
   }
   catch (Exception ex)
   {
     result = ex;
   }
   return new WcfResponse(result);
 }
Ejemplo n.º 2
0
    /// <summary>
    /// Called by <see cref="DataPortal" /> to update a
    /// business object.
    /// </summary>
    /// <param name="obj">The business object to update.</param>
    /// <param name="context">
    /// <see cref="Server.DataPortalContext" /> object passed to the server.
    /// </param>
    /// <param name="isSync">True if the client-side proxy should synchronously invoke the server.</param>
#pragma warning disable 1998
    public async Task<DataPortalResult> Update(object obj, DataPortalContext context, bool isSync)
#pragma warning restore 1998
    {
#if !(ANDROID || IOS) && !NETFX_CORE
      ChannelFactory<IWcfPortal> cf = GetChannelFactory();
      var proxy = GetProxy(cf);
      WcfResponse response = null;
#if NET40
      try
      {
        var request = new UpdateRequest(obj, context);
        if (isSync)
        {
          response = proxy.Update(request);
        }
        else
        {
          var worker = new Csla.Threading.BackgroundWorker();
          var tcs = new TaskCompletionSource<WcfResponse>();
          worker.RunWorkerCompleted += (o, e) =>
            {
              tcs.SetResult((WcfResponse)e.Result);
            };
          worker.DoWork += (o, e) =>
            {
              e.Result = proxy.Update(request);
            };
          worker.RunWorkerAsync();
          response = await tcs.Task;
        }
        if (cf != null)
          cf.Close();
        object result = response.Result;
        if (result is Exception)
          throw (Exception)result;
        return (DataPortalResult)result;
      }
      catch
      {
        cf.Abort();
        throw;
      }
#else
      try
      {
        var request = new UpdateRequest(obj, context);
        if (isSync)
          response = proxy.Update(request);
        else
          response = await proxy.UpdateAsync(request);
        if (cf != null)
          cf.Close();
      }
      catch
      {
        cf.Abort();
        throw;
      }
      object result = response.Result;
      if (result is Exception)
        throw (Exception)result;
      return (DataPortalResult)result;
#endif
#else
      var request = GetBaseUpdateCriteriaRequest();
      request.ObjectData = MobileFormatter.Serialize(obj);
      request = ConvertRequest(request);

      var proxy = GetProxy();
      DataPortalResult result = null;
#if !NETFX_CORE && !(IOS || ANDROID)
      var tcs = new TaskCompletionSource<DataPortalResult>();
      proxy.UpdateCompleted += (s, e) => 
        {
          try
          {
            Csla.WcfPortal.WcfResponse response = null;
            if (e.Error == null)
              response = ConvertResponse(e.Result);
            ContextDictionary globalContext = null;
            if (response != null)
              globalContext = (ContextDictionary)MobileFormatter.Deserialize(response.GlobalContext);
            if (e.Error == null && response != null && response.ErrorData == null)
            {
              var newobj = MobileFormatter.Deserialize(response.ObjectData);
              result = new DataPortalResult(newobj, null, globalContext);
            }
            else if (response != null && response.ErrorData != null)
            {
              var ex = new DataPortalException(response.ErrorData);
              result = new DataPortalResult(null, ex, globalContext);
            }
            else
            {
              result = new DataPortalResult(null, e.Error, globalContext);
            }
          }
          catch (Exception ex)
          {
            result = new DataPortalResult(null, ex, null);
          }
          finally
          {
            tcs.SetResult(result);
          }
        };
      proxy.UpdateAsync(request);
      var finalresult = await tcs.Task;
      if (finalresult.Error != null)
        throw finalresult.Error;
      return finalresult;
#else
      try
      {
        var response = await proxy.UpdateAsync(request);
        response = ConvertResponse(response);
        if (response == null)
          throw new DataPortalException("null response", null);
        var globalContext = (ContextDictionary)MobileFormatter.Deserialize(response.GlobalContext);
        if (response.ErrorData == null)
        {
          var newobj = MobileFormatter.Deserialize(response.ObjectData);
          result = new DataPortalResult(newobj, null, globalContext);
        }
        else
        {
          var ex = new DataPortalException(response.ErrorData);
          result = new DataPortalResult(null, ex, globalContext);
        }
      }
      catch (Exception ex)
      {
        result = new DataPortalResult(null, ex, null);
      }
      if (result.Error != null)
        throw result.Error;
      return result;
#endif
#endif
    }