public async Task Invoke(HttpContext context, IRedisManager redisManager) { try { var requestMessage = new HttpRequestMessage(); var requestMethod = context.Request.Method; if (!HttpMethods.IsGet(requestMethod) && !HttpMethods.IsHead(requestMethod) && !HttpMethods.IsDelete(requestMethod) && !HttpMethods.IsTrace(requestMethod)) { var streamContent = new StreamContent(context.Request.Body); requestMessage.Content = streamContent; } // All request headers and cookies must be transferend to remote server. Some headers will be skipped foreach (var header in context.Request.Headers) { if (!requestMessage.Headers.TryAddWithoutValidation(header.Key, header.Value.ToArray()) && requestMessage.Content != null) { requestMessage.Content?.Headers.TryAddWithoutValidation(header.Key, header.Value.ToArray()); } } string uriString = GetUri(context, redisManager); //recreate remote url requestMessage.RequestUri = new Uri(uriString); requestMessage.Method = new HttpMethod(context.Request.Method); var responseMessage = await redisManager.GetConnection().SendAsync(requestMessage); context.Response.StatusCode = (int)responseMessage.StatusCode; foreach (var header in responseMessage.Headers) { context.Response.Headers[header.Key] = header.Value.ToArray(); } foreach (var header in responseMessage.Content.Headers) { context.Response.Headers[header.Key] = header.Value.ToArray(); } //tell to the browser that response is not chunked //context.Response.Headers.Remove("transfer-encoding"); await responseMessage.Content.CopyToAsync(context.Response.Body); } catch (Exception e) { Debug.WriteLine(e.Message); } }
public async Task Register(string authCode, LoginRequest request) { var keyName = LoginRedisKey.Create(authCode); using (var c = await _redisManager.GetConnection()) { var requestKey = new RedisKey <LoginRequest>(keyName, c); await requestKey.SetAsync(request); #if DEBUG bool setExpResult = await requestKey.SetExpirationAsync(TimeSpan.FromSeconds(15)); #else bool setExpResult = await requestKey.SetExpirationAsync(TimeSpan.FromMinutes(1)); #endif if (!setExpResult) { throw new InvalidOperationException("Can't set expiration") .AndFactIs("request", request) .AndFactIs("key", keyName); } } }