示例#1
0
文件: Fx.cs 项目: dox0/DotNet471RS3
        public static Exception AssertAndFailFast(string description)
        {
            Fx.Assert(description);
            string failFastMessage = InternalSR.FailFastMessage(description);

            // The catch is here to force the finally to run, as finallys don't run until the stack walk gets to a catch.
            // The catch makes sure that the finally will run before the stack-walk leaves the frame, but the code inside is impossible to reach.
            try
            {
                try
                {
                    Fx.Exception.TraceFailFast(failFastMessage);
                }
                finally
                {
                    Environment.FailFast(failFastMessage);
                }
            }
            catch
            {
                throw;
            }

            return(null); // we'll never get here since we've just fail-fasted
        }
示例#2
0
 public static void ThrowIfNonPositiveArgument(TimeSpan timeout, string argumentName)
 {
     if (timeout <= TimeSpan.Zero)
     {
         throw Fx.Exception.ArgumentOutOfRange(argumentName, timeout, InternalSR.TimeoutMustBePositive(argumentName, timeout));
     }
 }
示例#3
0
        public static T Convert <T>(object source)
        {
            T t = default(T);

            if (!(source is T))
            {
                if (source != null)
                {
                    if (!TypeHelper.TryNumericConversion <T>(source, out t))
                    {
                        throw Fx.Exception.AsError(new InvalidCastException(InternalSR.CannotConvertObject(source, typeof(T))));
                    }
                    else
                    {
                        return(t);
                    }
                }
                else
                {
                    if (!typeof(T).IsValueType || TypeHelper.IsNullableType(typeof(T)))
                    {
                        T t1 = default(T);
                        return(t1);
                    }
                    else
                    {
                        throw Fx.Exception.AsError(new InvalidCastException(InternalSR.CannotConvertObject(source, typeof(T))));
                    }
                }
            }
            else
            {
                return((T)source);
            }
        }
示例#4
0
        // handles not only the simple cast, but also value type widening, etc.
        public static T Convert <T>(object source)
        {
            // first check the common cases
            if (source is T)
            {
                return((T)source);
            }

            if (source == null)
            {
                if (typeof(T).IsValueType && !IsNullableType(typeof(T)))
                {
                    throw Fx.Exception.AsError(new InvalidCastException(InternalSR.CannotConvertObject(source, typeof(T))));
                }

                return(default(T));
            }

            T result;

            if (TryNumericConversion <T>(source, out result))
            {
                return(result);
            }

            throw Fx.Exception.AsError(new InvalidCastException(InternalSR.CannotConvertObject(source, typeof(T))));
        }
示例#5
0
文件: TaskHelpers.cs 项目: yukozh/wcf
        // Used by WebSocketTransportDuplexSessionChannel on the sync code path.
        // TODO: Try and switch as many code paths as possible which use this to async
        public static void Wait(this Task task, TimeSpan timeout, Action <Exception, TimeSpan, string> exceptionConverter, string operationType)
        {
            bool timedOut = false;

            try
            {
                if (timeout > TimeoutHelper.MaxWait)
                {
                    task.Wait();
                }
                else
                {
                    timedOut = !task.Wait(timeout);
                }
            }
            catch (Exception ex)
            {
                if (Fx.IsFatal(ex) || exceptionConverter == null)
                {
                    throw;
                }

                exceptionConverter(ex, timeout, operationType);
            }

            if (timedOut)
            {
                throw Fx.Exception.AsError(new TimeoutException(InternalSR.TaskTimedOutError(timeout)));
            }
        }
示例#6
0
        public async Task <T> DequeueAsync(TimeSpan timeout)
        {
            (bool success, T value) = await TryDequeueAsync(timeout);

            if (!success)
            {
                throw Fx.Exception.AsError(new TimeoutException(InternalSR.TimeoutInputQueueDequeue(timeout)));
            }

            return(value);
        }
示例#7
0
        public T Dequeue(TimeSpan timeout)
        {
            T value;

            if (!this.Dequeue(timeout, out value))
            {
                throw Fx.Exception.AsError(new TimeoutException(InternalSR.TimeoutInputQueueDequeue(timeout)));
            }

            return(value);
        }
示例#8
0
 public static void ThrowIfNegativeArgument(TimeSpan timeout, string argumentName)
 {
     if (timeout >= TimeSpan.Zero)
     {
         return;
     }
     else
     {
         throw Fx.Exception.ArgumentOutOfRange(argumentName, timeout, InternalSR.TimeoutMustBeNonNegative(argumentName, timeout));
     }
 }
示例#9
0
        public T Dequeue(TimeSpan timeout)
        {
            T t = null;

            if (this.Dequeue(timeout, out t))
            {
                return(t);
            }
            else
            {
                throw Fx.Exception.AsError(new TimeoutException(InternalSR.TimeoutInputQueueDequeue(timeout)));
            }
        }
示例#10
0
 public static byte[] AllocateByteArray(int size)
 {
     byte[] numArray;
     try
     {
         numArray = new byte[size];
     }
     catch (OutOfMemoryException outOfMemoryException1)
     {
         OutOfMemoryException outOfMemoryException = outOfMemoryException1;
         throw Fx.Exception.AsError(new InsufficientMemoryException(InternalSR.BufferAllocationFailed(size), outOfMemoryException));
     }
     return(numArray);
 }
示例#11
0
 public static char[] AllocateCharArray(int size)
 {
     char[] chrArray;
     try
     {
         chrArray = new char[size];
     }
     catch (OutOfMemoryException outOfMemoryException1)
     {
         OutOfMemoryException outOfMemoryException = outOfMemoryException1;
         throw Fx.Exception.AsError(new InsufficientMemoryException(InternalSR.BufferAllocationFailed(size * 2), outOfMemoryException));
     }
     return(chrArray);
 }
示例#12
0
文件: Fx.cs 项目: dox0/DotNet471RS3
 public static char[] AllocateCharArray(int size)
 {
     try
     {
         // Safe to catch OOM from this as long as the ONLY thing it does is a simple allocation of a primitive type (no method calls).
         return(new char[size]);
     }
     catch (OutOfMemoryException exception)
     {
         // Convert OOM into an exception that can be safely handled by higher layers.
         throw Fx.Exception.AsError(
                   new InsufficientMemoryException(InternalSR.BufferAllocationFailed(size * sizeof(char)), exception));
     }
 }
示例#13
0
            protected override void Invoke()
            {
                TimeoutException timeoutException;
                Action <object, TimeoutException> action = this.callback;
                object obj = this.state;

                if (this.TimedOut)
                {
                    timeoutException = new TimeoutException(InternalSR.TimeoutOnOperation(this.originalTimeout));
                }
                else
                {
                    timeoutException = null;
                }
                action(obj, timeoutException);
            }
示例#14
0
        public static Exception AssertAndFailFast(string description)
        {
            string str = InternalSR.FailFastMessage(description);

            try
            {
                try
                {
                    Fx.Exception.TraceFailFast(str);
                }
                finally
                {
                    Environment.FailFast(str);
                }
            }
            catch
            {
                throw;
            }
            return(null);
        }
示例#15
0
        public static void Wait(this Task task, TimeSpan timeout, Action <Exception, TimeSpan, string> exceptionConverter, string operationType)
        {
            bool flag = false;

            try
            {
                if (timeout <= TimeoutHelper.MaxWait)
                {
                    flag = !task.Wait(timeout);
                }
                else
                {
                    task.Wait();
                }
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                if (Fx.IsFatal(exception) || exceptionConverter == null)
                {
                    throw;
                }
                else
                {
                    exceptionConverter(exception, timeout, operationType);
                }
            }
            if (!flag)
            {
                return;
            }
            else
            {
                throw Fx.Exception.AsError(new TimeoutException(InternalSR.TaskTimedOutError(timeout)));
            }
        }
示例#16
0
 protected void Complete(bool completedSynchronously)
 {
     if (!this.isCompleted)
     {
         this.completedSynchronously = completedSynchronously;
         if (this.OnCompleting != null)
         {
             try
             {
                 this.OnCompleting(this, this.exception);
             }
             catch (Exception exception1)
             {
                 Exception exception = exception1;
                 if (!Fx.IsFatal(exception))
                 {
                     this.exception = exception;
                 }
                 else
                 {
                     throw;
                 }
             }
         }
         if (!completedSynchronously)
         {
             lock (this.ThisLock)
             {
                 this.isCompleted = true;
                 if (this.manualResetEvent != null)
                 {
                     this.manualResetEvent.Set();
                 }
             }
         }
         else
         {
             this.isCompleted = true;
         }
         if (this.callback != null)
         {
             try
             {
                 if (this.VirtualCallback == null)
                 {
                     this.callback(this);
                 }
                 else
                 {
                     this.VirtualCallback(this.callback, this);
                 }
             }
             catch (Exception exception3)
             {
                 Exception exception2 = exception3;
                 if (!Fx.IsFatal(exception2))
                 {
                     throw Fx.Exception.AsError(new CallbackException(InternalSR.AsyncCallbackThrewException, exception2));
                 }
                 else
                 {
                     throw;
                 }
             }
         }
         return;
     }
     else
     {
         throw Fx.Exception.AsError(new InvalidOperationException(InternalSR.AsyncResultCompletedTwice(this.GetType())));
     }
 }
示例#17
0
文件: Fx.cs 项目: dox0/DotNet471RS3
 public InternalException(string description)
     : base(InternalSR.ShipAssertExceptionMessage(description))
 {
 }
 protected virtual Exception CreateQuotaExceededException(int maxSizeQuota)
 {
     return(new InvalidOperationException(InternalSR.BufferedOutputStreamQuotaExceeded(maxSizeQuota)));
 }
示例#19
0
        protected void Complete(bool completedSynchronously)
        {
            if (IsCompleted)
            {
                throw Fx.Exception.AsError(new InvalidOperationException(InternalSR.AsyncResultCompletedTwice(GetType())));
            }



            CompletedSynchronously = completedSynchronously;
            if (OnCompleting != null)
            {
                // Allow exception replacement, like a catch/throw pattern.
                try
                {
                    OnCompleting(this, _exception);
                }
                catch (Exception exception)
                {
                    if (Fx.IsFatal(exception))
                    {
                        throw;
                    }
                    _exception = exception;
                }
            }

            if (completedSynchronously)
            {
                // If we completedSynchronously, then there's no chance that the manualResetEvent was created so
                // we don't need to worry about a race condition.
                Fx.Assert(_manualResetEvent == null, "No ManualResetEvent should be created for a synchronous AsyncResult.");
                IsCompleted = true;
            }
            else
            {
                lock (ThisLock)
                {
                    IsCompleted = true;
                    if (_manualResetEvent != null)
                    {
                        _manualResetEvent.Set();
                    }
                }
            }

            if (_callback != null)
            {
                try
                {
                    if (VirtualCallback != null)
                    {
                        VirtualCallback(_callback, this);
                    }
                    else
                    {
                        _callback(this);
                    }
                }
                catch (Exception e)
                {
                    if (Fx.IsFatal(e))
                    {
                        throw;
                    }

                    throw Fx.Exception.AsError(new CallbackException(InternalSR.AsyncCallbackThrewException, e));
                }
            }
        }
示例#20
0
 protected static void ThrowInvalidAsyncResult(IAsyncResult result)
 {
     throw Fx.Exception.AsError(new InvalidOperationException(InternalSR.InvalidAsyncResultImplementation(result.GetType())));
 }
示例#21
0
 public ArgumentException ArgumentNullOrEmpty(string paramName)
 {
     return(this.Argument(paramName, InternalSR.ArgumentNullOrEmpty(paramName)));
 }
示例#22
0
 internal static TimeoutException CreateEnterTimedOutException(TimeSpan timeout)
 {
     return(new TimeoutException(InternalSR.LockTimeoutExceptionMessage(timeout)));
 }
 protected override void Invoke()
 {
     this.callback(this.state,
                   this.TimedOut ? new TimeoutException(InternalSR.TimeoutOnOperation(this.originalTimeout)) : null);
 }
示例#24
0
        protected void Complete(bool completedSynchronously)
        {
            if (this.isCompleted)
            {
                throw Fx.Exception.AsError(new InvalidOperationException(InternalSR.AsyncResultCompletedTwice(GetType())));
            }

#if DEBUG
            this.marker.AsyncResult = null;
            this.marker             = null;
            if (!Fx.FastDebug && completeStack == null)
            {
                completeStack = new StackTrace();
            }
#endif

            this.completedSynchronously = completedSynchronously;
            if (OnCompleting != null)
            {
                // Allow exception replacement, like a catch/throw pattern.
                try
                {
                    OnCompleting(this, this.exception);
                }
                catch (Exception exception)
                {
                    if (Fx.IsFatal(exception))
                    {
                        throw;
                    }
                    this.exception = exception;
                }
            }

            if (completedSynchronously)
            {
                // If we completedSynchronously, then there's no chance that the manualResetEvent was created so
                // we don't need to worry about a ----
                Fx.Assert(this.manualResetEvent == null, "No ManualResetEvent should be created for a synchronous AsyncResult.");
                this.isCompleted = true;
            }
            else
            {
                lock (ThisLock)
                {
                    this.isCompleted = true;
                    if (this.manualResetEvent != null)
                    {
                        this.manualResetEvent.Set();
                    }
                }
            }

            if (this.callback != null)
            {
                try
                {
                    if (VirtualCallback != null)
                    {
                        VirtualCallback(this.callback, this);
                    }
                    else
                    {
                        this.callback(this);
                    }
                }
#pragma warning disable 1634
#pragma warning suppress 56500 // transferring exception to another thread
                catch (Exception e)
                {
                    if (Fx.IsFatal(e))
                    {
                        throw;
                    }

                    throw Fx.Exception.AsError(new CallbackException(InternalSR.AsyncCallbackThrewException, e));
                }
#pragma warning restore 1634
            }
        }