public Task HandleCallAsync(HttpContext httpContext) { if (GrpcProtocolHelpers.IsInvalidContentType(httpContext, out var error)) { // This might be a CORS preflight request and CORS middleware hasn't been configured if (HttpMethods.IsOptions(httpContext.Request.Method)) { GrpcServerLog.UnhandledCorsPreflightRequest(Logger); GrpcProtocolHelpers.BuildHttpErrorResponse(httpContext.Response, StatusCodes.Status405MethodNotAllowed, StatusCode.Internal, "Unhandled CORS preflight request received. CORS may not be configured correctly in the application."); httpContext.Response.Headers[HeaderNames.Allow] = HttpMethods.Post; return(Task.CompletedTask); } else { GrpcServerLog.UnsupportedRequestContentType(Logger, httpContext.Request.ContentType); GrpcProtocolHelpers.BuildHttpErrorResponse(httpContext.Response, StatusCodes.Status415UnsupportedMediaType, StatusCode.Internal, error); return(Task.CompletedTask); } } if (httpContext.Request.Protocol != GrpcProtocolConstants.Http2Protocol && httpContext.Request.Protocol != GrpcProtocolConstants.Http20Protocol) { GrpcServerLog.UnsupportedRequestProtocol(Logger, httpContext.Request.Protocol); var protocolError = $"Request protocol '{httpContext.Request.Protocol}' is not supported."; GrpcProtocolHelpers.BuildHttpErrorResponse(httpContext.Response, StatusCodes.Status426UpgradeRequired, StatusCode.Internal, protocolError); httpContext.Response.Headers[HeaderNames.Upgrade] = GrpcProtocolConstants.Http2Protocol; return(Task.CompletedTask); } var serverCallContext = new HttpContextServerCallContext(httpContext, MethodInvoker.Options, typeof(TRequest), typeof(TResponse), Logger); httpContext.Features.Set <IServerCallContextFeature>(serverCallContext); GrpcProtocolHelpers.AddProtocolHeaders(httpContext.Response); try { serverCallContext.Initialize(); var handleCallTask = HandleCallAsyncCore(httpContext, serverCallContext); if (handleCallTask.IsCompletedSuccessfully) { return(serverCallContext.EndCallAsync()); } else { return(AwaitHandleCall(serverCallContext, MethodInvoker.Method, handleCallTask)); } } catch (Exception ex) { return(serverCallContext.ProcessHandlerErrorAsync(ex, MethodInvoker.Method.Name)); }
public Task HandleCallAsync(HttpContext httpContext) { if (GrpcProtocolHelpers.IsInvalidContentType(httpContext, out var error)) { Log.UnsupportedRequestContentType(Logger, httpContext.Request.ContentType); GrpcProtocolHelpers.BuildHttpErrorResponse(httpContext.Response, StatusCodes.Status415UnsupportedMediaType, StatusCode.Internal, error); return(Task.CompletedTask); } if (httpContext.Request.Protocol != GrpcProtocolConstants.Http2Protocol && httpContext.Request.Protocol != GrpcProtocolConstants.Http20Protocol) { Log.UnsupportedRequestProtocol(Logger, httpContext.Request.Protocol); var protocolError = $"Request protocol '{httpContext.Request.Protocol}' is not supported."; GrpcProtocolHelpers.BuildHttpErrorResponse(httpContext.Response, StatusCodes.Status426UpgradeRequired, StatusCode.Internal, protocolError); httpContext.Response.Headers[HeaderNames.Upgrade] = GrpcProtocolConstants.Http2Protocol; return(Task.CompletedTask); } var serverCallContext = new HttpContextServerCallContext(httpContext, ServiceOptions, Logger); httpContext.Features.Set <IServerCallContextFeature>(serverCallContext); GrpcProtocolHelpers.AddProtocolHeaders(httpContext.Response); try { serverCallContext.Initialize(); var handleCallTask = HandleCallAsyncCore(httpContext, serverCallContext); if (handleCallTask.IsCompletedSuccessfully) { return(serverCallContext.EndCallAsync()); } else { return(AwaitHandleCall(serverCallContext, Method, handleCallTask)); } } catch (Exception ex) { return(serverCallContext.ProcessHandlerErrorAsync(ex, Method.Name)); }