当前位置: 首页>>代码示例>>C#>>正文


C# IExceptionHandler.HandleAsync方法代码示例

本文整理汇总了C#中IExceptionHandler.HandleAsync方法的典型用法代码示例。如果您正苦于以下问题:C# IExceptionHandler.HandleAsync方法的具体用法?C# IExceptionHandler.HandleAsync怎么用?C# IExceptionHandler.HandleAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IExceptionHandler的用法示例。


在下文中一共展示了IExceptionHandler.HandleAsync方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: HandleAsyncCore

        private static async Task<HttpResponseMessage> HandleAsyncCore(IExceptionHandler handler,
            ExceptionHandlerContext context, CancellationToken cancellationToken)
        {
            Contract.Assert(handler != null);
            Contract.Assert(context != null);

            await handler.HandleAsync(context, cancellationToken);

            IHttpActionResult result = context.Result;

            if (result == null)
            {
                return null;
            }

            HttpResponseMessage response = await result.ExecuteAsync(cancellationToken);

            if (response == null)
            {
                throw new InvalidOperationException(Error.Format(SRResources.TypeMethodMustNotReturnNull,
                    typeof(IHttpActionResult).Name, "ExecuteAsync"));
            }

            return response;
        }
开发者ID:huangw-t,项目名称:aspnetwebstack,代码行数:25,代码来源:ExceptionHandlerExtensions.cs

示例2: HandleAsyncCore

        private static async Task<HandlerResponse> HandleAsyncCore(IExceptionHandler handler, ExceptionHandlerContext context, CancellationToken cancellationToken)
        {
            Contract.Assert(handler != null);
            Contract.Assert(context != null);

            await handler.HandleAsync(context, cancellationToken);

            ICommandHandlerResult result = context.Result;

            if (result == null)
            {
                return null;
            }

            HandlerResponse response = await result.ExecuteAsync(cancellationToken);

            if (response == null)
            {
                throw Error.ArgumentNull(Resources.TypeMethodMustNotReturnNull, typeof(ICommandHandlerResult).Name, "ExecuteAsync");
            }

            return response;
        }
开发者ID:ctguxp,项目名称:Waffle,代码行数:23,代码来源:ExceptionHandlerExtensions.cs

示例3: CopyErrorResponseAsync

        internal static async Task<bool> CopyErrorResponseAsync(ExceptionContextCatchBlock catchBlock,
            HttpContextBase httpContextBase, HttpRequestMessage request, HttpResponseMessage response,
            Exception exception, IExceptionLogger exceptionLogger, IExceptionHandler exceptionHandler,
            CancellationToken cancellationToken)
        {
            Contract.Assert(httpContextBase != null);
            Contract.Assert(httpContextBase.Response != null);
            Contract.Assert(request != null);
            Contract.Assert(exception != null);
            Contract.Assert(catchBlock != null);
            Contract.Assert(catchBlock.CallsHandler);

            HttpResponseBase httpResponseBase = httpContextBase.Response;
            HttpResponseMessage errorResponse = null;
            HttpResponseException responseException = exception as HttpResponseException;

            // Ensure all headers and content are cleared to eliminate any partial results.
            ClearContentAndHeaders(httpResponseBase);

            // If the exception we are handling is HttpResponseException,
            // that becomes the error response.
            if (responseException != null)
            {
                errorResponse = responseException.Response;
            }
            else
            {
                ExceptionContext exceptionContext = new ExceptionContext(exception, catchBlock, request)
                {
                    Response = response
                };
                await exceptionLogger.LogAsync(exceptionContext, cancellationToken);
                errorResponse = await exceptionHandler.HandleAsync(exceptionContext, cancellationToken);

                if (errorResponse == null)
                {
                    return false;
                }
            }

            Contract.Assert(errorResponse != null);
            if (!await CopyResponseStatusAndHeadersAsync(httpContextBase, request, errorResponse, exceptionLogger,
                cancellationToken))
            {
                // Don't rethrow the original exception unless explicitly requested to do so. In this case, the
                // exception handler indicated it wanted to handle the exception; it simply failed create a stable
                // response to send.
                return true;
            }

            // The error response may return a null content if content negotiation
            // fails to find a formatter, or this may be an HttpResponseException without
            // content.  In either case, cleanup and return a completed task.

            if (errorResponse.Content == null)
            {
                errorResponse.Dispose();
                return true;
            }

            CopyHeaders(errorResponse.Content.Headers, httpContextBase);

            await WriteErrorResponseContentAsync(httpResponseBase, request, errorResponse, cancellationToken,
                exceptionLogger);
            return true;
        }
开发者ID:quentez,项目名称:aspnetwebstack,代码行数:66,代码来源:HttpControllerHandler.cs

示例4: AssertDelegatesTo

        private static void AssertDelegatesTo(Mock<IExceptionHandler> expected, IExceptionHandler actual)
        {
            Assert.NotNull(actual);

            ExceptionHandlerContext context = new ExceptionHandlerContext(new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer));
            CancellationToken cancellationToken = CancellationToken.None;

            expected
                .Setup((l) => l.HandleAsync(It.IsAny<ExceptionHandlerContext>(), It.IsAny<CancellationToken>()))
                .Returns(CreateCanceledTask());

            Task task = actual.HandleAsync(context, cancellationToken);

            Assert.NotNull(task);
            task.WaitUntilCompleted();
            Assert.Equal(TaskStatus.Canceled, task.Status);

            expected.Verify((l) => l.HandleAsync(context, cancellationToken), Times.Once());
        }
开发者ID:KevMoore,项目名称:aspnetwebstack,代码行数:19,代码来源:WebApiAppBuilderExtensionsTest.cs


注:本文中的IExceptionHandler.HandleAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。