示例#1
0
        /// <summary>
        /// Cancels the operation.
        /// </summary>
        /// <remarks>
        /// Upon completion of the operation, check the IsCanceled property to determine whether
        /// or not the operation was successfully canceled. Note that successful cancellation
        /// does not guarantee state changes were prevented from happening on the server.
        /// </remarks>
        /// <exception cref="NotSupportedException"> is thrown when <see cref="SupportsCancellation"/>
        /// is <c>false</c>.
        /// </exception>
        public void Cancel()
        {
            if (!this.SupportsCancellation)
            {
                throw new NotSupportedException(Resources.AsyncOperation_CancelNotSupported);
            }

            this.EnsureNotCompleted();
            if (this._cancellationTokenSource != null)
            {
                try
                {
                    this._cancellationTokenSource?.Cancel();
                }
                catch (AggregateException ex)
                {
                    throw ExceptionHandlingUtility.GetUnwrappedException(ex);;
                }
            }
        }
示例#2
0
        /// <summary>
        /// Transforms the specified exception as appropriate into a fault message that can be sent
        /// back to the client.
        /// </summary>
        /// <remarks>
        /// This method will also trace the exception if tracing is enabled.
        /// </remarks>
        /// <param name="e">The exception that was caught.</param>
        /// <returns>The exception to return.</returns>
        internal static FaultException <DomainServiceFault> CreateFaultException(Exception e)
        {
            Debug.Assert(!e.IsFatal(), "Fatal exception passed in");
            DomainServiceFault fault = new DomainServiceFault();

            HttpContext context = HttpContext.Current;

            // Unwrap any TargetInvocationExceptions to get the real exception.
            e = ExceptionHandlingUtility.GetUnwrappedException(e);

            // we always send back a 200 (i.e. not re-throwing) with the actual error code in
            // the results (except fo 404) because silverlight only supports 404/500 error code. If customErrors
            // are disabled, we'll also send the error message.
            int errorCode = (int)HttpStatusCode.InternalServerError;

            if (e is InvalidOperationException)
            {
                // invalid operation exception at root level generates BadRequest
                errorCode = (int)HttpStatusCode.BadRequest;
            }
            else if (e is UnauthorizedAccessException)
            {
                errorCode = (int)HttpStatusCode.Unauthorized;
            }
            else
            {
                DomainException dpe = e as DomainException;
                if (dpe != null)
                {
                    // we always propagate error info to the client for DomainServiceExceptions
                    fault.ErrorCode         = dpe.ErrorCode;
                    fault.ErrorMessage      = FormatExceptionMessage(dpe);
                    fault.IsDomainException = true;
                    if (context != null && context.IsCustomErrorEnabled == false)
                    {
                        // also send the stack trace if custom errors is disabled
                        fault.StackTrace = dpe.StackTrace;
                    }

                    return(new FaultException <DomainServiceFault>(fault, new FaultReason(new FaultReasonText(fault.ErrorMessage ?? String.Empty, CultureInfo.CurrentCulture))));
                }
                else
                {
                    HttpException httpException = e as HttpException;
                    if (httpException != null)
                    {
                        errorCode = httpException.GetHttpCode();
                        if (errorCode == (int)HttpStatusCode.NotFound)
                        {
                            // for NotFound errors, we don't provide detailed error
                            // info, we just rethrow
                            throw e;
                        }
                    }
                }
            }

            // set error code. Also set error message if custom errors is disabled
            fault.ErrorCode = errorCode;
            if (context != null && !context.IsCustomErrorEnabled)
            {
                fault.ErrorMessage = FormatExceptionMessage(e);
                fault.StackTrace   = e.StackTrace;
            }

            return(new FaultException <DomainServiceFault>(fault, new FaultReason(new FaultReasonText(fault.ErrorMessage ?? String.Empty, CultureInfo.CurrentCulture))));
        }