Ejemplo n.º 1
0
        private void HandleDataPortalException(string operation, Server.DataPortalException ex, bool isSync, Csla.DataPortalClient.IDataPortalProxy proxy)
        {
            var result   = ex.Result;
            var original = ex.InnerException;

            if (original.InnerException != null)
            {
                original = original.InnerException;
            }
            throw new DataPortalException(
                      String.Format("DataPortal.{2} {0} ({1})", Resources.Failed, original.Message, operation),
                      ex.InnerException, result.ReturnObject);
        }
Ejemplo n.º 2
0
        private void HandleDataPortalException(string operation, Server.DataPortalException ex, bool isSync, Csla.DataPortalClient.IDataPortalProxy proxy)
        {
            var result = ex.Result;

            GlobalContext = result.GlobalContext;
            if (proxy.IsServerRemote && isSync)
            {
                ApplicationContext.ContextManager.SetGlobalContext(GlobalContext);
            }
            var original = ex.InnerException;

            if (original.InnerException != null)
            {
                original = original.InnerException;
            }
            throw new DataPortalException(
                      String.Format("DataPortal.{2} {0} ({1})", Resources.Failed, original.Message, operation),
                      ex.InnerException, result.ReturnObject);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Called by the client-side DataPortal to create a
 /// new business object.
 /// </summary>
 /// <param name="objectType">Type of business object to create.</param>
 /// <param name="criteria">Criteria object describing business object.</param>
 /// <param name="context">Server.DataPortalContext object passed to the server.</param>
 /// <param name="isSync">True if the client-side proxy should synchronously invoke the server.</param>
 public async Task<DataPortalResult> Create(Type objectType, object criteria, DataPortalContext context, bool isSync)
 {
   DataPortalResult result = null;
   if (isSync)
     throw new NotSupportedException("isSync == true");
   try
   {
     if (!(criteria is IMobileObject))
       criteria = new PrimitiveCriteria(criteria);
     var contextData = Csla.Serialization.Mobile.MobileFormatter.Serialize(context);
     var criteriaData = Csla.Serialization.Mobile.MobileFormatter.Serialize(criteria);
     var portal = new BrokeredHost();
     var objectTypeName = objectType.AssemblyQualifiedName;
     var resultData = await portal.Create(objectTypeName, criteriaData, contextData);
     var response = (Csla.Server.Hosts.HttpChannel.HttpResponse)Csla.Serialization.Mobile.MobileFormatter.Deserialize(resultData.ToArray());
     var globalContext = (Csla.Core.ContextDictionary)Csla.Serialization.Mobile.MobileFormatter.Deserialize(response.GlobalContext);
     if (response != null && response.ErrorData == null)
     {
       var obj = MobileFormatter.Deserialize(response.ObjectData);
       result = new DataPortalResult(obj, null, globalContext);
     }
     else if (response != null && response.ErrorData != null)
     {
       var ex = new DataPortalException(response.ErrorData);
       result = new DataPortalResult(null, ex, globalContext);
     }
     else
     {
       throw new DataPortalException("null response", null);
     }
   }
   catch (Exception ex)
   {
     result = new DataPortalResult(null, ex, null);
   }
   if (result.Error != null)
     throw result.Error;
   return result;
 }
Ejemplo n.º 4
0
    /// <summary>
    /// Called by <see cref="DataPortal" /> to delete a
    /// business object.
    /// </summary>
    /// <param name="objectType">Type of business object to create.</param>
    /// <param name="criteria">Criteria object describing business object.</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> Delete(Type objectType, object criteria, DataPortalContext context, bool isSync)
#pragma warning restore 1998
    {
      DataPortalResult result = null;
      try
      {
        if (isSync)
          throw new NotSupportedException("isSync == true");
        var client = GetClient();
        if (this.Timeout > 0)
          client.Timeout = TimeSpan.FromMilliseconds(this.Timeout);
        var request = GetBaseCriteriaRequest();
        request.TypeName = AssemblyNameTranslator.GetAssemblyQualifiedName(objectType.AssemblyQualifiedName);
        if (!(criteria is IMobileObject))
        {
          criteria = new PrimitiveCriteria(criteria);
        }
        request.CriteriaData = MobileFormatter.Serialize(criteria);
        request = ConvertRequest(request);

        var serialized = MobileFormatter.Serialize(request);

        var httpRequest = new HttpRequestMessage(HttpMethod.Post, string.Format("{0}?operation=delete", DataPortalUrl));
        httpRequest.Content = new ByteArrayContent(serialized);

        var httpResponse = await client.SendAsync(httpRequest);
        httpResponse.EnsureSuccessStatusCode();
        serialized = await httpResponse.Content.ReadAsByteArrayAsync();

        var response = (Csla.Server.Hosts.HttpChannel.HttpResponse)MobileFormatter.Deserialize(serialized);
        response = ConvertResponse(response);
        var globalContext = (ContextDictionary)MobileFormatter.Deserialize(response.GlobalContext);
        if (response != null && response.ErrorData == null)
        {
          result = new DataPortalResult(null, null, globalContext);
        }
        else if (response != null && response.ErrorData != null)
        {
          var ex = new DataPortalException(response.ErrorData);
          result = new DataPortalResult(null, ex, globalContext);
        }
        else
        {
          throw new DataPortalException("null response", null);
        }
      }
      catch (Exception ex)
      {
        result = new DataPortalResult(null, ex, null);
      }
      if (result.Error != null)
        throw result.Error;
      return result;
    }
Ejemplo n.º 5
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
    {
      DataPortalResult result = null;
      try
      {
        if (isSync)
          throw new NotSupportedException("isSync == true");
        var client = GetClient();
        if (this.Timeout > 0)
          client.Timeout = TimeSpan.FromMilliseconds(this.Timeout); 
        var request = GetBaseUpdateCriteriaRequest();
        request.ObjectData = MobileFormatter.Serialize(obj);
        request = ConvertRequest(request);

        var serialized = Csla.Serialization.Mobile.MobileFormatter.Serialize(request);

        var httpRequest = new HttpRequestMessage(HttpMethod.Post, string.Format("{0}?operation=update", DataPortalUrl));
        httpRequest.Content = new ByteArrayContent(serialized);

        var httpResponse = await client.SendAsync(httpRequest);
        httpResponse.EnsureSuccessStatusCode();
        serialized = await httpResponse.Content.ReadAsByteArrayAsync();

        var response = (Csla.Server.Hosts.HttpChannel.HttpResponse)Csla.Serialization.Mobile.MobileFormatter.Deserialize(serialized);
        response = ConvertResponse(response);
        var globalContext = (ContextDictionary)MobileFormatter.Deserialize(response.GlobalContext);
        if (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
        {
          throw new DataPortalException("null response", null);
        }
      }
      catch (Exception ex)
      {
        result = new DataPortalResult(null, ex, null);
      }
      if (result.Error != null)
        throw result.Error;
      return result;
    }
Ejemplo n.º 6
0
 private void HandleDeleteDataPortalException(Server.DataPortalException ex, bool isSync, Csla.DataPortalClient.IDataPortalProxy proxy)
 {
     HandleDataPortalException("Delete", ex, isSync, proxy);
 }
Ejemplo n.º 7
0
    /// <summary>
    /// Called by <see cref="DataPortal" /> to delete a
    /// business object.
    /// </summary>
    /// <param name="objectType">Type of business object to create.</param>
    /// <param name="criteria">Criteria object describing business object.</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> Delete(Type objectType, object criteria, DataPortalContext context, bool isSync)
#pragma warning restore 1998
    {
      var request = GetBaseCriteriaRequest();
      request.TypeName = objectType.AssemblyQualifiedName;
      if (!(criteria is IMobileObject))
      {
        criteria = new PrimitiveCriteria(criteria);
      }
      request.CriteriaData = MobileFormatter.Serialize(criteria);
      request = ConvertRequest(request);

      var proxy = GetProxy();
      DataPortalResult result = null;
      try
      {
        var response = await proxy.DeleteAsync(request);
        response = ConvertResponse(response);
        var globalContext = (ContextDictionary)MobileFormatter.Deserialize(response.GlobalContext);
        if (response != null && response.ErrorData == null)
        {
          result = new DataPortalResult(null, null, globalContext);
        }
        else if (response != null && response.ErrorData != null)
        {
          var ex = new DataPortalException(response.ErrorData);
          result = new DataPortalResult(null, ex, globalContext);
        }
        else
        {
          throw new DataPortalException("null response", null);
        }
      }
      catch (Exception ex)
      {
        result = new DataPortalResult(null, ex, null);
      }
      if (result.Error != null)
        throw result.Error;
      return result;
    }
Ejemplo n.º 8
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
    {
      var request = GetBaseUpdateCriteriaRequest();
      request.ObjectData = MobileFormatter.Serialize(obj);
      request = ConvertRequest(request);

      var proxy = GetProxy();
      DataPortalResult result = null;
      try
      {
        var response = await proxy.UpdateAsync(request);
        response = ConvertResponse(response);
        var globalContext = (ContextDictionary)MobileFormatter.Deserialize(response.GlobalContext);
        if (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
        {
          throw new DataPortalException("null response", null);
        }
      }
      catch (Exception ex)
      {
        result = new DataPortalResult(null, ex, null);
      }
      if (result.Error != null)
        throw result.Error;
      return result;
    }
Ejemplo n.º 9
0
 /// <summary>
 /// Called by the client-side DataPortal to update a
 /// business object.
 /// </summary>
 /// <param name="obj">Business object to update.</param>
 /// <param name="context">Server.DataPortalContext object passed to the server.</param>
 /// <param name="isSync">True if the client-side proxy should synchronously invoke the server.</param>
 public async Task<DataPortalResult> Update(object obj, DataPortalContext context, bool isSync)
 {
   DataPortalResult result = null;
   if (isSync)
     throw new NotSupportedException("isSync == true");
   try
   {
     var contextData = Csla.Serialization.Mobile.MobileFormatter.Serialize(context);
     var objectData = Csla.Serialization.Mobile.MobileFormatter.Serialize(obj);
     var portal = new BrokeredHost();
     var resultData = await portal.Update(objectData, contextData);
     var response = (Csla.Server.Hosts.HttpChannel.HttpResponse)Csla.Serialization.Mobile.MobileFormatter.Deserialize(resultData.ToArray());
     var globalContext = (Csla.Core.ContextDictionary)Csla.Serialization.Mobile.MobileFormatter.Deserialize(response.GlobalContext);
     if (response != null && response.ErrorData == null)
     {
       obj = MobileFormatter.Deserialize(response.ObjectData);
       result = new DataPortalResult(obj, null, globalContext);
     }
     else if (response != null && response.ErrorData != null)
     {
       var ex = new DataPortalException(response.ErrorData);
       result = new DataPortalResult(null, ex, globalContext);
     }
     else
     {
       throw new DataPortalException("null response", null);
     }
   }
   catch (Exception ex)
   {
     result = new DataPortalResult(null, ex, null);
   }
   return result;
 }
Ejemplo n.º 10
0
    /// <summary>
    /// Called by <see cref="DataPortal" /> to delete a
    /// business object.
    /// </summary>
    /// <param name="objectType">Type of business object to create.</param>
    /// <param name="criteria">Criteria object describing business object.</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> Delete(Type objectType, object criteria, 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 DeleteRequest(objectType, criteria, context);
        if (isSync)
        {
          response = proxy.Delete(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.Delete(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 DeleteRequest(objectType, criteria, context);
        if (isSync)
          response = proxy.Delete(request);
        else
          response = await proxy.DeleteAsync(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 = GetBaseCriteriaRequest();
      request.TypeName = objectType.AssemblyQualifiedName;
      if (!(criteria is IMobileObject))
      {
        criteria = new PrimitiveCriteria(criteria);
      }
      request.CriteriaData = MobileFormatter.Serialize(criteria);
      request = ConvertRequest(request);

      var proxy = GetProxy();
      DataPortalResult result = null;
#if !NETFX_CORE && !(IOS || ANDROID)
      var tcs = new TaskCompletionSource<DataPortalResult>();
      proxy.DeleteCompleted += (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)
            {
              result = new DataPortalResult(null, 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.DeleteAsync(request);
      var finalresult = await tcs.Task;
      if (finalresult.Error != null)
        throw finalresult.Error;
      return finalresult;
#else
      try
      {
        var response = await proxy.DeleteAsync(request);
        response = ConvertResponse(response);
        if (response == null)
          throw new DataPortalException("null response", null);
        var globalContext = (ContextDictionary)MobileFormatter.Deserialize(response.GlobalContext);
        if (response.ErrorData == null)
        {
          result = new DataPortalResult(null, 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
    }
Ejemplo n.º 11
0
    /// <summary>
    /// Called by <see cref="DataPortal" /> to delete a
    /// business object.
    /// </summary>
    /// <param name="objectType">Type of business object to create.</param>
    /// <param name="criteria">Criteria object describing business object.</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> Delete(Type objectType, object criteria, DataPortalContext context, bool isSync)
#pragma warning restore 1998
    {
      var request = GetBaseCriteriaRequest();
      request.TypeName = objectType.AssemblyQualifiedName;
      if (!(criteria is IMobileObject))
      {
        criteria = new PrimitiveCriteria(criteria);
      }
      request.CriteriaData = MobileFormatter.Serialize(criteria);
      request = ConvertRequest(request);

      var proxy = GetProxy();
      DataPortalResult result = null;
      try
      {
        Csla.WcfPortal.WcfResponse response = null;
        if (isSync)
          response = proxy.Delete(request);
        else
        {
#if !NET40
          response = await proxy.DeleteAsync(request).ConfigureAwait(false);
#else
          var tcs = new TaskCompletionSource<WcfResponse>();
          proxy.DeleteCompleted += (o, e) =>
          {
            if (e.Error == null)
              tcs.TrySetResult(e.Result);
            else
              tcs.TrySetException(e.Error);
          };
          proxy.DeleteAsync(request);
          await tcs.Task;
          response = tcs.Task.Result;
#endif
        }
        response = ConvertResponse(response);
        var globalContext = (ContextDictionary)MobileFormatter.Deserialize(response.GlobalContext);
        if (response != null && response.ErrorData == null)
        {
          result = new DataPortalResult(null, null, globalContext);
        }
        else if (response != null && response.ErrorData != null)
        {
          var ex = new DataPortalException(response.ErrorData);
          result = new DataPortalResult(null, ex, globalContext);
        }
        else
        {
          throw new DataPortalException("null response", null);
        }
      }
      catch (Exception ex)
      {
        result = new DataPortalResult(null, ex, null);
      }
      if (result.Error != null)
        throw result.Error;
      return result;
    }
Ejemplo n.º 12
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 !SILVERLIGHT && !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
      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
    }