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


C# Request.CreateErrorResponse方法代码示例

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


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

示例1: ProcessRequest

        public override async Task<Response> ProcessRequest(Request request)
        {
            var fanoutRequest = request.HasInputBody ? await request.ReadInputBody<TieredRequest>() : null;
            if (request.HasInputBody && fanoutRequest == null)
            {
                // This indicates failed deserialization
                return request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not read input body");
            }

            // aggregation servers should fanout automagically. we honor filters for machine function and datacenter.
            if ((fanoutRequest == null || fanoutRequest.Sources == null || fanoutRequest.Sources.Count == 0) && this.server.EnableQueryAggregation)
            {
                if (fanoutRequest == null)
                {
                    fanoutRequest = new TieredRequest();
                }

                fanoutRequest.Sources = this.SelectSourceServers(request.QueryParameters).ToList();
            }

            var queryParameters = new DimensionSpecification(request.QueryParameters);
            var endOfName = request.Path.LastIndexOf('/');
            if (endOfName < 0)
            {
                return request.CreateErrorResponse(HttpStatusCode.BadRequest, "No command provided.");
            }

            var counterName = request.Path.Substring(0, endOfName);
            if (string.IsNullOrEmpty(counterName))
            {
                return request.CreateErrorResponse(HttpStatusCode.BadRequest, "No counter pattern provided.");
            }

            var commandName = request.Path.Substring(endOfName + 1);

            if (RestCommands.CounterInfoCommand.Equals(commandName, StringComparison.OrdinalIgnoreCase))
            {
                return await this.Info(request, fanoutRequest, counterName, queryParameters);
            }

            if (RestCommands.CounterQueryCommand.Equals(commandName, StringComparison.OrdinalIgnoreCase))
            {
                var response = await this.Query(counterName, fanoutRequest, queryParameters);
                return Response.Create(request, (HttpStatusCode)response.HttpResponseCode, response);
            }

            return request.CreateErrorResponse(HttpStatusCode.BadRequest, "Unknown command: " + commandName);
        }
开发者ID:darting,项目名称:MetricSystem,代码行数:48,代码来源:Counters.cs

示例2: ProcessRequest

        public override async Task<Response> ProcessRequest(Request request)
        {
            if (!request.HasInputBody)
            {
                return request.CreateErrorResponse(HttpStatusCode.BadRequest, "no input body provided.");
            }

            var registrationMessage = await request.ReadInputBody<ServerRegistration>();
            if (registrationMessage != null)
            {
                var server = this.serverList.InsertOrUpdate(registrationMessage);
                server.ProcessRegistrationMessage(registrationMessage);
                return new Response(request, HttpStatusCode.OK, "Registered.");
            }

            return request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid data.");
        }
开发者ID:darting,项目名称:MetricSystem,代码行数:17,代码来源:Register.cs

示例3: ProcessRequest

            public override Task<Response> ProcessRequest(Request request)
            {
                if (this.RequestReceived != null)
                {
                    return Task.FromResult(this.RequestReceived(request));
                }

                return Task.FromResult(request.CreateErrorResponse(HttpStatusCode.Gone, "Gone fishin'."));
            }
开发者ID:darting,项目名称:MetricSystem,代码行数:9,代码来源:ServerTests.cs

示例4: ProcessRequest

        public override Task<Response> ProcessRequest(Request request)
        {
            // The '.Servers' property actually returns a new list so this is totally reasonable.
            var response = new ListServerResponse {Servers = this.serverList.Servers};
            if (response.Servers.Count == 0)
            {
                return Task.FromResult(request.CreateErrorResponse(HttpStatusCode.NotFound, "No known servers."));
            }

            return Task.FromResult(Response.Create(request, HttpStatusCode.OK, response));
        }
开发者ID:darting,项目名称:MetricSystem,代码行数:11,代码来源:ListServers.cs

示例5: ProcessRequest

        public override async Task<Response> ProcessRequest(Request request)
        {
            if (!request.HasInputBody)
            {
                return request.CreateErrorResponse(HttpStatusCode.BadRequest, "No input body provided for write.");
            }

            if (string.IsNullOrEmpty(request.Path))
            {
                return request.CreateErrorResponse(HttpStatusCode.BadRequest, "Counter name must be specified.");
            }

            var counter = this.server.DataManager.GetCounter<Counter>(request.Path);
            if (counter == null)
            {
                return request.CreateErrorResponse(HttpStatusCode.NotFound, "Unknown counter name provided.");
            }

            var input = await request.ReadInputBody<CounterWriteRequest>();
            if (input == null)
            {
                return request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not deserialize input.");
            }
            if (input.Writes.Count == 0)
            {
                return request.CreateErrorResponse(HttpStatusCode.BadRequest, "No write operations were provided.");
            }

            // We want to do two passes here because if any of the operations are invalid we want to reject the entire
            // transaction.
            if (input.Writes.Any(operation => operation.Count < 1))
            {
                return request.CreateErrorResponse(HttpStatusCode.BadRequest,
                                                   "Operation counts must be greater than zero.");
            }

            foreach (var operation in input.Writes)
            {
                RunOperation(counter, operation);
            }

            return new Response(request, HttpStatusCode.Accepted, "Accepted");
        }
开发者ID:darting,项目名称:MetricSystem,代码行数:43,代码来源:Write.cs

示例6: ProcessRequest

        public override async Task<Response> ProcessRequest(Request request)
        {
            var counterName = request.Path;
            if (string.IsNullOrEmpty(counterName))
            {
                return request.CreateErrorResponse(HttpStatusCode.BadRequest, "Path or command is invalid.");
            }

            if (request.QueryParameters.Count != 2
                || !request.QueryParameters.ContainsKey(ReservedDimensions.StartTimeDimension)
                || !request.QueryParameters.ContainsKey(ReservedDimensions.EndTimeDimension))
            {
                return request.CreateErrorResponse(HttpStatusCode.BadRequest,
                                                   "Invalid query parameters. Must specify only start and end time.");
            }

            DateTime start, end;
            if (!DateTime.TryParse(request.QueryParameters[ReservedDimensions.StartTimeDimension], out start))
            {
                return request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid start time.");
            }
            if (!DateTime.TryParse(request.QueryParameters[ReservedDimensions.EndTimeDimension], out end))
            {
                return request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid end time.");
            }

            var message = new TransferRequest();
            if (request.HasInputBody)
            {
                message = await request.ReadInputBody<TransferRequest>();
            }

            var ms = request.GetStream();

            // This is a tiered aggregation request, handle it separately.
            if (message.Sources != null && message.Sources.Count > 1)
            {
                try
                {
                    using (var aggregator =
                        PersistedDataProtocol.CreateAggregatorForSampleType(message.DataType, counterName,
                                                                            message.Sources, start, end,
                                                                            dataManager.MemoryStreamManager))
                    {
                        if (message.Timeout > 0)
                        {
                            aggregator.Timeout = TimeSpan.FromSeconds(message.Timeout);
                        }
                        if (message.MaxFanout > 0)
                        {
                            aggregator.MaxFanout = (int)message.MaxFanout;
                        }

                        // This shouldn't happen, given that one of the servers we want to talk to is.. us.
                        if (!await aggregator.Run())
                        {
                            ms.Dispose();
                            return request.CreateErrorResponse(HttpStatusCode.InternalServerError,
                                                               "All child requests failed.");
                        }
                        if (!aggregator.WriteToStream(ms))
                        {
                            // TODO: If we have no results but none of our queries failed we can definitively 404,
                            //       for now lazy.
                            ms.Dispose();
                            return request.CreateErrorResponse(HttpStatusCode.InternalServerError,
                                                               "No cntent matched.");
                        }
                    }
                }
                catch (ArgumentException)
                {
                    ms.Dispose();
                    return request.CreateErrorResponse(HttpStatusCode.BadRequest, "Request parameters are invalid.");
                }
            }
            else
            {
                var counter = this.dataManager.GetCounter<Counter>(counterName);
                if (counter == null || !counter.SerializeData(start, end, ms))
                {
                    ms.Dispose();
                    return request.CreateErrorResponse(HttpStatusCode.NotFound, "The requested data is not available.");
                }
            }

            // after writing the response the server will handle disposing the stream for us.
            return new Response(request, HttpStatusCode.OK, ms) {ContentType = ResponseType};
        }
开发者ID:darting,项目名称:MetricSystem,代码行数:89,代码来源:Transfer.cs

示例7: Info

        private async Task<Response> Info(Request request, TieredRequest fanoutRequest, string counterPattern,
                                          DimensionSpecification queryParameters)
        {
            var localResponseData = new CounterInfoResponse();
            foreach (var c in this.server.DataManager.Counters)
            {
                if (c.Name.MatchGlob(counterPattern))
                {
                    localResponseData.Counters.Add(BuildCounterInfo(c, queryParameters));
                }
            }

            // If there's no lower tier we can bounce out now.
            if (fanoutRequest == null)
            {
                return localResponseData.Counters.Count == 0
                           ? request.CreateErrorResponse(HttpStatusCode.NotFound, "No matching counters are defined.")
                           : Response.Create(request, HttpStatusCode.OK, localResponseData);
            }

            var distributedResponse =
                await
                this.server.CreateQueryClient(fanoutRequest)
                    .CounterInfoQuery(counterPattern, fanoutRequest, queryParameters);

            CounterInfoSampleCombiner.Merge(distributedResponse, localResponseData);
            // add details from this server if needed
            if (fanoutRequest.IncludeRequestDiagnostics && distributedResponse.RequestDetails != null)
            {
                distributedResponse.RequestDetails.Add(new RequestDetails
                                                       {
                                                           Server = this.server.ServerInfo,
                                                           IsAggregator = true,
                                                           HttpResponseCode = (localResponseData.Counters.Count > 0
                                                                                   ? (short)HttpStatusCode.OK
                                                                                   : (short)HttpStatusCode.NotFound),
                                                           Status = RequestStatus.Success,
                                                       });
            }

            return Response.Create(request, HttpStatusCode.OK, distributedResponse);
        }
开发者ID:darting,项目名称:MetricSystem,代码行数:42,代码来源:Counters.cs

示例8: ProcessRequest

 public override Task<Response> ProcessRequest(Request request)
 {
     return Task.FromResult(request.CreateErrorResponse(HttpStatusCode.NotFound, "Unknown command."));
 }
开发者ID:darting,项目名称:MetricSystem,代码行数:4,代码来源:UnknownRequest.cs


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