public void BackgroundWorker_RunWorkerAsync_CallsDoWorkAndWorkerCompleted()
        {
            var threadid = Thread.CurrentThread.ManagedThreadId;

            using (UnitTestContext context = GetContext())
            {
                bool doWorkCalled = false;

                BackgroundWorker target = new BackgroundWorker();
                target.DoWork += (o, e) =>
                {
                    doWorkCalled = true;

                    // make sure that user, clientcontext, globalcontext, currentCulture and currentUIculture are sent
                    context.Assert.IsFalse(threadid == Thread.CurrentThread.ManagedThreadId);
                    context.Assert.IsTrue(Csla.ApplicationContext.User is MyPrincipal);
                    context.Assert.AreEqual("TEST", Csla.ApplicationContext.GlobalContext["BWTEST"]);
                    context.Assert.AreEqual("TEST", Csla.ApplicationContext.ClientContext["BWTEST"]);

                    context.Assert.AreEqual("FR", Thread.CurrentThread.CurrentCulture.Name.ToUpper());
                    context.Assert.AreEqual("FR", Thread.CurrentThread.CurrentUICulture.Name.ToUpper());
                };
                target.RunWorkerCompleted += (o, e) =>
                {
                    context.Assert.IsTrue(threadid == Thread.CurrentThread.ManagedThreadId);
                    context.Assert.IsNull(e.Error);
                    context.Assert.IsTrue(doWorkCalled, "Do work has been called");
                    context.Assert.Success();
                };
                target.RunWorkerAsync(null);
                context.Complete();
            }
        }
示例#2
0
 /// <summary>
 /// Called by <see cref="DataPortal" /> to load an
 /// existing business object.
 /// </summary>
 /// <param name="objectType">Type of business object to retrieve.</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>
 public async Task <DataPortalResult> Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync)
 {
     if (isSync || Csla.ApplicationContext.LogicalExecutionLocation == ApplicationContext.LogicalExecutionLocations.Server)
     {
         return(await _portal.Fetch(objectType, criteria, context, isSync));
     }
     else
     {
         var tcs = new TaskCompletionSource <DataPortalResult>();
         var bw  = new Csla.Threading.BackgroundWorker();
         bw.DoWork += (s, o) =>
         {
             o.Result = _portal.Fetch(objectType, criteria, context, isSync).Result;
         };
         bw.RunWorkerCompleted += (s, o) =>
         {
             if (o.Error == null)
             {
                 tcs.TrySetResult((DataPortalResult)o.Result);
             }
             else
             {
                 tcs.TrySetException(o.Error);
             }
         };
         bw.RunWorkerAsync();
         return(await tcs.Task);
     }
 }
示例#3
0
        public void BackgroundWorker_DoWork_ThrowsInvalidOperationExcpetionWhenWorkerReportsProgressIsFalse()
        {
            UnitTestContext context = GetContext();

            int numTimesProgressCalled = 0;

            BackgroundWorker target = new BackgroundWorker();

            target.DoWork += (o, e) =>
            {
                // report progress changed 10 times
                for (int i = 1; i < 11; i++)
                {
                    target.ReportProgress(i * 10);
                }
            };
            target.WorkerReportsProgress = false;
            target.ProgressChanged      += (o, e) =>
            {
                numTimesProgressCalled++;
            };
            target.RunWorkerCompleted += (o, e) =>
            {
                //  target does not support ReportProgress we shold get a System.InvalidOperationException from DoWork
                context.Assert.IsTrue(e.Error is System.InvalidOperationException);
                context.Assert.Success();
            };
            target.RunWorkerAsync(null);
            context.Complete();
        }
        public void BackgroundWorker_RunWorkerAsync_ReportsProgress()
        {
            var threadid = Thread.CurrentThread.ManagedThreadId;

            using (UnitTestContext context = GetContext())
            {
                int numTimesProgressCalled = 0;

                BackgroundWorker target = new BackgroundWorker();
                target.DoWork += (o, e) =>
                {
                    // report progress changed 10 times
                    for (int i = 1; i < 11; i++)
                    {
                        target.ReportProgress(i * 10);
                    }
                    e.Result = new object();
                };
                target.WorkerReportsProgress = true;
                target.ProgressChanged      += (o, e) =>
                {
                    numTimesProgressCalled++;
                    context.Assert.IsTrue(threadid == Thread.CurrentThread.ManagedThreadId);
                };
                target.RunWorkerCompleted += (o, e) =>
                {
                    context.Assert.IsTrue(threadid == Thread.CurrentThread.ManagedThreadId);
                    context.Assert.IsNull(e.Error);
                    context.Assert.IsTrue(numTimesProgressCalled == 10, "ReportProgress has been called 10 times");
                    context.Assert.Success();
                };
                target.RunWorkerAsync(null);
                context.Complete();
            }
        }
示例#5
0
        public void BackgroundWorker_CancelAsync_ReportsCancelledWhenWorkerSupportsCancellationIsTrue()
        {
            UnitTestContext context = GetContext();

            BackgroundWorker target = new BackgroundWorker();

            target.DoWork += (o, e) =>
            {
                // report progress changed 10 times
                for (int i = 1; i < 11; i++)
                {
                    Thread.Sleep(100);
                    if (target.CancellationPending)
                    {
                        e.Cancel = true;
                        return;
                    }
                }
            };
            target.WorkerSupportsCancellation = true;
            target.RunWorkerCompleted        += (o, e) =>
            {
                //  target does not support ReportProgress we shold get a System.InvalidOperationException from DoWork
                context.Assert.IsNull(e.Error);
                context.Assert.IsTrue(e.Cancelled);
                context.Assert.Success();
            };
            target.RunWorkerAsync(null);
            target.CancelAsync();
            context.Complete();
        }
示例#6
0
        public void BackgroundWorker_CancelAsync_ThrowsInvalidOperationExceptionWhenWorkerSupportsCancellationIsFalse()
        {
            using (UnitTestContext context = GetContext())
            {
                BackgroundWorker target = new BackgroundWorker();
                target.DoWork += (o, e) =>
                {
                    for (int i = 1; i < 11; i++)
                    {
                        Thread.Sleep(10);
                    }
                };
                target.WorkerSupportsCancellation = false;
                target.RunWorkerAsync(null);

                try
                {
                    target.CancelAsync(); // this call throws exception
                }
                catch (InvalidOperationException ex)
                {
                    context.Assert.Fail(ex);
                }
                context.Complete();
            }
        }
示例#7
0
 /// <summary>
 /// Called by <see cref="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">
 /// <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>
 public async Task<DataPortalResult> Create(
   Type objectType, object criteria, DataPortalContext context, bool isSync)
 {
   if (isSync || Csla.ApplicationContext.LogicalExecutionLocation == ApplicationContext.LogicalExecutionLocations.Server)
   {
     return await _portal.Create(objectType, criteria, context, isSync);
   }
   else
   {
     var tcs = new TaskCompletionSource<DataPortalResult>();
     var bw = new Csla.Threading.BackgroundWorker();
     bw.DoWork += (s, o) =>
     {
       o.Result = _portal.Create(objectType, criteria, context, isSync).Result;
     };
     bw.RunWorkerCompleted += (s, o) =>
     {
       if (o.Error == null)
         tcs.TrySetResult((DataPortalResult)o.Result);
       else
         tcs.TrySetException(o.Error);
     };
     bw.RunWorkerAsync();
     return await tcs.Task;
   }
 }
        public void BackgroundWorker_RunWorkerAsync_CallsDoWorkAndWorkerCompleted()
        {
            using (UnitTestContext context = GetContext())
            {
                BackgroundWorkerSyncContextHelper.DoTests(() =>
                {
                    var UIThreadid = Thread.CurrentThread.ManagedThreadId;

                    _originalPrincipal           = Csla.ApplicationContext.User;
                    _originalCulture             = Thread.CurrentThread.CurrentCulture;
                    _originalUICulture           = Thread.CurrentThread.CurrentUICulture;
                    Csla.ApplicationContext.User = new MyPrincipal();
                    Csla.ApplicationContext.ClientContext["BWTEST"] = "TEST";
                    Csla.ApplicationContext.GlobalContext["BWTEST"] = "TEST";


                    Thread.CurrentThread.CurrentCulture   = CultureInfo.GetCultureInfo("FR");
                    Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("FR");

                    BackgroundWorker target = new BackgroundWorker();
                    bool doWorkCalled       = false;

                    target.DoWork += (o, e) =>
                    {
                        doWorkCalled = true;
                        context.Assert.IsFalse(Thread.CurrentThread.ManagedThreadId == UIThreadid);

                        // make sure that user, clientcontext, globalcontext, currentCulture and currentUIculture are sent
                        context.Assert.IsTrue(Csla.ApplicationContext.User is MyPrincipal);
                        context.Assert.AreEqual("TEST", Csla.ApplicationContext.GlobalContext["BWTEST"]);
                        context.Assert.AreEqual("TEST", Csla.ApplicationContext.ClientContext["BWTEST"]);
                        context.Assert.AreEqual("FR", Thread.CurrentThread.CurrentCulture.Name.ToUpper());
                        context.Assert.AreEqual("FR", Thread.CurrentThread.CurrentUICulture.Name.ToUpper());
                    };
                    target.RunWorkerCompleted += (o, e) =>
                    {
                        // assert that this callback comes on the "UI" thread
                        context.Assert.IsTrue(Thread.CurrentThread.ManagedThreadId == UIThreadid);
                        context.Assert.IsNull(e.Error);
                        context.Assert.IsTrue(doWorkCalled, "Do work has been called");
                        context.Assert.Success();
                    };
                    target.RunWorkerAsync(null);


                    while (target.IsBusy)
                    {
                        // this is the equvalent to Application.DoEvents in Windows Forms
                        BackgroundWorkerSyncContextHelper.PumpDispatcher();
                    }

                    context.Complete();
                });
            }
        }
示例#9
0
        public void DataPortal_Fetch(Csla.DataPortalClient.LocalProxy <AddressListCreator> .CompletedHandler handler)
        {
            var bw = new Csla.Threading.BackgroundWorker();

            bw.DoWork += (a, b) =>
            {
                b.Result = AddressEditList.GetList();
            };
            bw.RunWorkerCompleted += (a, b) =>
            {
                Result = (AddressEditList)b.Result;
                handler(this, b.Error);
            };
            bw.RunWorkerAsync();
        }
示例#10
0
        public void DataPortal_Fetch(Csla.DataPortalClient.LocalProxy <Root> .CompletedHandler handler)
        {
            var bw = new Csla.Threading.BackgroundWorker();

            bw.DoWork += (a, b) =>
            {
                Id = 123;
                //Addresses = AddressEditList.GetList();
            };
            bw.RunWorkerCompleted += (a, b) =>
            {
                handler(this, b.Error);
            };
            bw.RunWorkerAsync();
        }
示例#11
0
        public void BackgroundWorker_RunWorkerAsync_ReportsProgress()
        {
            using (UnitTestContext context = GetContext())
            {
                BackgroundWorkerSyncContextHelper.DoTests(() =>
                {
                    var UIThreadid             = Thread.CurrentThread.ManagedThreadId;
                    int numTimesProgressCalled = 0;

                    BackgroundWorker target = new BackgroundWorker();
                    target.DoWork          += (o, e) =>
                    {
                        // report progress changed 10 times
                        for (int i = 1; i < 11; i++)
                        {
                            target.ReportProgress(i * 10);
                        }
                        e.Result = new object();
                    };
                    target.WorkerReportsProgress = true;
                    target.ProgressChanged      += (o, e) =>
                    {
                        context.Assert.IsTrue(Thread.CurrentThread.ManagedThreadId == UIThreadid);
                        numTimesProgressCalled++;
                    };
                    target.RunWorkerCompleted += (o, e) =>
                    {
                        context.Assert.IsTrue(Thread.CurrentThread.ManagedThreadId == UIThreadid);
                        context.Assert.IsNull(e.Error);
                        context.Assert.IsTrue(numTimesProgressCalled == 10, "ReportProgress has been called 10 times");
                        context.Assert.Success();
                    };
                    target.RunWorkerAsync(null);

                    while (target.IsBusy)
                    {
                        // this is the equvalent to Application.DoEvents in Windows Forms
                        BackgroundWorkerSyncContextHelper.PumpDispatcher();
                    }

                    context.Complete();
                });
            }
        }
示例#12
0
    public void BackgroundWorker_DoWork_ThrowsInvalidOperationExcpetionWhenWorkerReportsProgressIsFalse()
    {
      UnitTestContext context = GetContext();

      int numTimesProgressCalled = 0;

      BackgroundWorker target = new BackgroundWorker();
      target.DoWork += (o, e) =>
      {
        // report progress changed 10 times
        for (int i = 1; i < 11; i++)
        {
          target.ReportProgress(i * 10);
        }
      };
      target.WorkerReportsProgress = false;
      target.ProgressChanged += (o, e) =>
      {
        numTimesProgressCalled++;
      };
      target.RunWorkerCompleted += (o, e) =>
      {
        //  target does not support ReportProgress we shold get a System.InvalidOperationException from DoWork
        context.Assert.IsTrue(e.Error is System.InvalidOperationException);
        context.Assert.Success();
      };
      target.RunWorkerAsync(null);
      context.Complete();
    }
示例#13
0
文件: WcfProxy.cs 项目: pisch/csla
        /// <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
        }
示例#14
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
    }
示例#15
0
    public void BackgroundWorker_RunWorkerAsync_ReportsProgress()
    {
      var threadid = Thread.CurrentThread.ManagedThreadId;
      using (UnitTestContext context = GetContext())
      {

        int numTimesProgressCalled = 0;

        BackgroundWorker target = new BackgroundWorker();
        target.DoWork += (o, e) =>
                           {
                             // report progress changed 10 times
                             for (int i = 1; i < 11; i++)
                             {
                               target.ReportProgress(i * 10);
                             }
                             e.Result = new object();
                           };
        target.WorkerReportsProgress = true;
        target.ProgressChanged += (o, e) =>
                                    {
                                      numTimesProgressCalled++;
                                      context.Assert.IsTrue(threadid == Thread.CurrentThread.ManagedThreadId);
                                    };
        target.RunWorkerCompleted += (o, e) =>
                                       {
                                         context.Assert.IsTrue(threadid == Thread.CurrentThread.ManagedThreadId);
                                         context.Assert.IsNull(e.Error);
                                         context.Assert.IsTrue(numTimesProgressCalled == 10, "ReportProgress has been called 10 times");
                                         context.Assert.Success();
                                       };
        target.RunWorkerAsync(null);
        context.Complete();
      }
    }
示例#16
0
文件: WcfProxy.cs 项目: ruanbl/csla
    /// <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
    }
示例#17
0
    public void BackgroundWorker_RunWorkerAsync_CallsDoWorkAndWorkerCompleted()
    {

      using (UnitTestContext context = GetContext())
      {
        BackgroundWorkerSyncContextHelper.DoTests(() =>
        {

          var UIThreadid = Thread.CurrentThread.ManagedThreadId;

          _originalPrincipal = Csla.ApplicationContext.User;
          _originalCulture = Thread.CurrentThread.CurrentCulture;
          _originalUICulture = Thread.CurrentThread.CurrentUICulture;
          Csla.ApplicationContext.User = new MyPrincipal();
          Csla.ApplicationContext.ClientContext["BWTEST"] = "TEST";
          Csla.ApplicationContext.GlobalContext["BWTEST"] = "TEST";


          Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("FR");
          Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("FR");

          BackgroundWorker target = new BackgroundWorker();
          bool doWorkCalled = false;

          target.DoWork += (o, e) =>
          {
            doWorkCalled = true;
            context.Assert.IsFalse(Thread.CurrentThread.ManagedThreadId == UIThreadid);

            // make sure that user, clientcontext, globalcontext, currentCulture and currentUIculture are sent 
            context.Assert.IsTrue(Csla.ApplicationContext.User is MyPrincipal);
            context.Assert.AreEqual("TEST", Csla.ApplicationContext.GlobalContext["BWTEST"]);
            context.Assert.AreEqual("TEST", Csla.ApplicationContext.ClientContext["BWTEST"]);
            context.Assert.AreEqual("FR", Thread.CurrentThread.CurrentCulture.Name.ToUpper());
            context.Assert.AreEqual("FR", Thread.CurrentThread.CurrentUICulture.Name.ToUpper());
          };
          target.RunWorkerCompleted += (o, e) =>
          {
            // assert that this callback comes on the "UI" thread 
            context.Assert.IsTrue(Thread.CurrentThread.ManagedThreadId == UIThreadid);
            context.Assert.IsNull(e.Error);
            context.Assert.IsTrue(doWorkCalled, "Do work has been called");
            context.Assert.Success();
          };
          target.RunWorkerAsync(null);


          while (target.IsBusy)
          {
            // this is the equvalent to Application.DoEvents in Windows Forms 
            BackgroundWorkerSyncContextHelper.PumpDispatcher();
          }

          context.Complete();
        });
      }
    }
示例#18
0
    public void BackgroundWorker_RunWorkerAsync_CallsDoWorkAndWorkerCompleted()
    {
      var threadid = Thread.CurrentThread.ManagedThreadId;
      using (UnitTestContext context = GetContext())
      {
        bool doWorkCalled = false;

        BackgroundWorker target = new BackgroundWorker();
        target.DoWork += (o, e) =>
                           {
                             doWorkCalled = true;

                             // make sure that user, clientcontext, globalcontext, currentCulture and currentUIculture are sent 
                             context.Assert.IsFalse(threadid == Thread.CurrentThread.ManagedThreadId);
                             context.Assert.IsTrue(Csla.ApplicationContext.User is MyPrincipal);
                             context.Assert.AreEqual("TEST", Csla.ApplicationContext.GlobalContext["BWTEST"]);
                             context.Assert.AreEqual("TEST", Csla.ApplicationContext.ClientContext["BWTEST"]);

                             context.Assert.AreEqual("FR", Thread.CurrentThread.CurrentCulture.Name.ToUpper());
                             context.Assert.AreEqual("FR", Thread.CurrentThread.CurrentUICulture.Name.ToUpper());
                           };
        target.RunWorkerCompleted += (o, e) =>
                                       {
                                         context.Assert.IsTrue(threadid == Thread.CurrentThread.ManagedThreadId);
                                         context.Assert.IsNull(e.Error);
                                         context.Assert.IsTrue(doWorkCalled, "Do work has been called");
                                         context.Assert.Success();
                                       };
        target.RunWorkerAsync(null);
        context.Complete();
      }
    }
示例#19
0
    public void BackgroundWorker_RunWorkerAsync_ReportsProgress()
    {
      using (UnitTestContext context = GetContext())
      {
        BackgroundWorkerSyncContextHelper.DoTests(() =>
        {

          var UIThreadid = Thread.CurrentThread.ManagedThreadId;
          int numTimesProgressCalled = 0;

          BackgroundWorker target = new BackgroundWorker();
          target.DoWork += (o, e) =>
                              {
                                // report progress changed 10 times
                                for (int i = 1; i < 11; i++)
                                {
                                  target.ReportProgress(i*10);
                                }
                                e.Result = new object();
                              };
          target.WorkerReportsProgress = true;
          target.ProgressChanged += (o, e) =>
                                      {
                                        context.Assert.IsTrue(Thread.CurrentThread.ManagedThreadId == UIThreadid);
                                        numTimesProgressCalled++;
                                      };
          target.RunWorkerCompleted += (o, e) =>
                                          {
                                            context.Assert.IsTrue(Thread.CurrentThread.ManagedThreadId == UIThreadid);
                                            context.Assert.IsNull(e.Error);
                                            context.Assert.IsTrue(numTimesProgressCalled == 10,"ReportProgress has been called 10 times");
                                            context.Assert.Success();
                                          };
          target.RunWorkerAsync(null);

          while (target.IsBusy)
          {
            // this is the equvalent to Application.DoEvents in Windows Forms 
            BackgroundWorkerSyncContextHelper.PumpDispatcher();
          }

          context.Complete();
        });
      }
    }
示例#20
0
        /// <summary>
        /// Creates the rich text box in the background thread.
        /// </summary>
        /// <param name="action">The action.</param>
        /// <param name="contentControl">The content control.</param>
        public static void CreateRichTextBox(Action action, RichToolTip contentControl)
        {
            var bw = new BackgroundWorker();
            DoWorkEventHandler runWorkerAction = (s, e) => contentControl.Dispatcher.BeginInvoke(
            () =>
                {
                    richTextBox = new RadRichTextBox
                    {
                        IsReadOnly = true,
                        IsSelectionEnabled = false,
                        Background = new SolidColorBrush(Colors.Transparent),
                        IsSpellCheckingEnabled = false,
                        AcceptsTab = false,
                        AcceptsReturn = false,
                        IsContextMenuEnabled = false,
                        IsImageMiniToolBarEnabled = false,
                        IsSelectionMiniToolBarEnabled = false,
                        IsTabStop = false,
                        BorderThickness = new Thickness(0),
                        Cursor = Cursors.Hand,
                        MaxWidth = 400
                    };
                });
            RunWorkerCompletedEventHandler runWOrkerComplitedHandler = null;
            runWOrkerComplitedHandler = (s, e) =>
            {
                bw.DoWork -= runWorkerAction;
                bw.RunWorkerCompleted -= runWOrkerComplitedHandler;
                action();
            };

            bw.DoWork += runWorkerAction;
            bw.RunWorkerCompleted += runWOrkerComplitedHandler;
            bw.RunWorkerAsync(richTextBox);
        }
示例#21
0
    public void BackgroundWorker_CancelAsync_ReportsCancelledWhenWorkerSupportsCancellationIsTrue()
    {
      UnitTestContext context = GetContext();

      int numTimesProgressCalled = 0;

      BackgroundWorker target = new BackgroundWorker();
      target.DoWork += (o, e) =>
      {
        // report progress changed 10 times
        for (int i = 1; i < 11; i++)
        {
          Thread.Sleep(100);
          if (target.CancellationPending)
          {
            e.Cancel = true;
            return;
          }
        }
      };
      target.WorkerSupportsCancellation = true;
      target.RunWorkerCompleted += (o, e) =>
      {
        //  target does not support ReportProgress we shold get a System.InvalidOperationException from DoWork
        context.Assert.IsNull(e.Error);
        context.Assert.IsTrue(e.Cancelled);
        context.Assert.Success();
      };
      target.RunWorkerAsync(null);
      target.CancelAsync();
      context.Complete();
    }
示例#22
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
        }
示例#23
0
    public void BackgroundWorker_CancelAsync_ThrowsInvalidOperationExceptionWhenWorkerSupportsCancellationIsFalse()
    {
      using (UnitTestContext context = GetContext())
      {
        BackgroundWorker target = new BackgroundWorker();
        target.DoWork += (o, e) =>
        {
          for (int i = 1; i < 11; i++)
          {
            Thread.Sleep(10);
          }
        };
        target.WorkerSupportsCancellation = false;
        target.RunWorkerAsync(null);

        try
        {
          target.CancelAsync();   // this call throws exception 
        }
        catch (InvalidOperationException ex)
        {
          context.Assert.Fail(ex);
        }
        context.Complete();
      }
    }