Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which HttpRequest class to use in .netstandard 2?

I'm working in a .netstandard library, because it should work both in .NetFramework 4.7 and .NetCore. In that library, I have a method that receives a HttpRequest object, process the request using querystring, content, contenttype etc and returns a HttpResponse.

I've tried to use HttpRequestMessage and HttpResponseMessage, but in .NetCore API, we do not have those classes. Which is the best class to use in this situation for HttpRequest and HttpResponse?

Example:

public interface IOcspResponder
{
    Task<HttpResponseMessage> Respond(HttpRequestMessage httpRequest);
}

The client will implement a controller and use the library the following way:

[RoutePrefix("api/ocsp")]
public class OcspController : Controller
{
    [Route("{encoded}"]
    public Task<HttpResponseMessage> Get(string encoded)
    {
        return await OcspResponder.Respond(Request);
    }

    [Route("")]
    public Task<HttpResponseMessage> Post()
    {
        return await OcspResponder.Respond(Request);
    }

    private IOcspResponder OcspResponder { get; }

    public OcspController(IOcspResponder ocspResponder)
    {
        OcspResponder = ocspResponder;
    }
}
like image 607
Gabriel Calegari Avatar asked Jun 21 '18 14:06

Gabriel Calegari


2 Answers

Why would you need to handle HttpResponseMessage type in IOcspResponder?

You may use types as HttpResponseMessage or HttpResponse in your Controller only and then use serialisation to handle request/response data in data structures of your definition.

Then, your Controller's methods won't have to deal with HTTP specific libraries, but data structures you may define and use in your IOcspResponder definition.

Check out this answer for further details about this setup.

Check out this answer for further details and examples about serialisation.

like image 127
smn.tino Avatar answered Sep 28 '22 11:09

smn.tino


I solved this creating the classes OcspHttpRequest and OcspHttpResponse. After this, I created extension methods to easily convert those classes to the native classes for both .NET Framework and .NET Core.

Example for .NET Framework:

public static async Task<OcspHttpRequest> ToOcspHttpRequest(this HttpRequestMessage requestMessage)
{
    var httpRequestBase = new OcspHttpRequest
    {
       HttpMethod = requestMessage.Method.Method,
       MediaType = requestMessage.Content.Headers.ContentType.MediaType,
       RequestUri = requestMessage.RequestUri,
       Content = await requestMessage.Content.ReadAsByteArrayAsync()
     };

     return httpRequestBase;
 }

Example for .NET Core:

public static async Task<OcspHttpRequest> ToOcspHttpRequest(this HttpRequest request)
{
    var ocspHttpRequest = new OcspHttpRequest();
    ocspHttpRequest.HttpMethod = request.Method;
    ocspHttpRequest.MediaType = request.ContentType;
    ocspHttpRequest.RequestUri = request.GetUri();
    ocspHttpRequest.Content = await request.GetRawBodyBytesAsync();

    return ocspHttpRequest;
}
like image 21
Gabriel Calegari Avatar answered Sep 28 '22 11:09

Gabriel Calegari