0

I am new to .NET Core and we make a client request from our middleware and I am wondering what is the right way to extract httprequestcontext from httpcontext and set it in HttpRequestMessage.

.NET Framework has a method setrequestcontext() to set request context in HttpRequestMessage and I don't see similar method in .NET Core.

Any help would be appreciated.

middleware:

    public async Task InvokeAsync(HttpContext context, RequestDelegate next)
    {
        await next.Invoke(context).ConfigureAwait(false);
    
        // create a HTTPMessageRequest from context.
        HttpRequestMessage httpRequestMessage = context.CreateHttpRequestMessage();
        
        // make a remote call.
    }
    
    public static HttpRequestMessage CreateHttpRequestMessage(this HttpContext context)
            {
                _ = context ?? throw new ArgumentNullException(nameof(context));
    
                var request = context.Request;
    
                var requestMessage = new HttpRequestMessage();
                var requestMethod = request.Method;
                if (!HttpMethods.IsGet(requestMethod) &&
                    !HttpMethods.IsHead(requestMethod) &&
                    !HttpMethods.IsDelete(requestMethod) &&
                    !HttpMethods.IsTrace(requestMethod))
                {
                    var streamContent = new StreamContent(request.Body);
                    requestMessage.Content = streamContent;
                }
    
                requestMessage.RequestUri = new Uri(context.Request.GetDisplayUrl());
    
                // Copy the request headers
                foreach (var header in request.Headers)
                {
                    if (!requestMessage.Headers.TryAddWithoutValidation(header.Key, header.Value.ToArray()) && requestMessage.Content != null)
                    {
                        requestMessage.Content?.Headers.TryAddWithoutValidation(header.Key, header.Value.ToArray());
                    }
                }
    
                requestMessage.Method = new HttpMethod(request.Method);
                return requestMessage;
            }
Vy Do
  • 46,709
  • 59
  • 215
  • 313
starkk92
  • 5,754
  • 9
  • 43
  • 59
  • Could you please show us your middleware you're talking about? – CodeNotFound Jun 06 '21 at 20:32
  • Possible duplicate: [Convert Microsoft.AspNetCore.Http.HttpRequest to HttpRequestMessage](https://stackoverflow.com/questions/45759417/convert-microsoft-aspnetcore-http-httprequest-to-httprequestmessage). – D M Jun 06 '21 at 20:39
  • Another possible duplicate: [Copy HttpContext request content data to new request](https://stackoverflow.com/questions/52506417/copy-httpcontext-request-content-data-to-new-request). – D M Jun 06 '21 at 20:41
  • @DM While I used the above links to form the rest of the HttpRequestMessage, they don't really talk about RequestContext. – starkk92 Jun 06 '21 at 20:43
  • 1
    @starkk92 Did you see [this answer specifically](https://stackoverflow.com/a/65302841/14956277) about [Microsoft.ReverseProxy](https://www.nuget.org/packages/Microsoft.ReverseProxy)? "`IHttpProxy` serve[s] as the core proxy adapter between incoming `AspNetCore` and outgoing `System.Net.Http` requests. It handles the mechanics of creating a `HttpRequestMessage` from a `HttpContext`, sending it, and relaying the response." – D M Jun 06 '21 at 20:46
  • That's a great answer! – davidfowl Jun 07 '21 at 06:06

0 Answers0