Which HttpRequest class to use in .netstandard 2?
Asked Answered
S

4

5

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;
    }
}
Solenne answered 21/6, 2018 at 14:30 Comment(4)
Why return .NET objects at all? Make your own and return them instead.Yggdrasil
it sounds your app is actually a web service to handle HTTP requests. Is your method actually defined in a Controller? Could you provide some code examples?Thedrick
@smn.tino I added example of use.Solenne
@Yggdrasil It works, but does complex the use.Solenne
S
2

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;
}
Solenne answered 25/6, 2018 at 14:6 Comment(1)
Could you please provide a body for OcspHttpRequest class?Lutyens
T
2

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.

Thedrick answered 21/6, 2018 at 16:19 Comment(3)
Because OcspRequests are different. Either the ContentType in the HttpRequest should be processed by the OcspResponder. Then, I cannot serialize the request.Solenne
@GabrielCalegari why can't you perform serialization?Thedrick
Because I need to validate the HttpRequest not only the HttpContent.Solenne
S
2

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;
}
Solenne answered 25/6, 2018 at 14:6 Comment(1)
Could you please provide a body for OcspHttpRequest class?Lutyens
A
1

There is no common base class that you can use. Even if you create your own interfaces or wrappers, the properties have different names, different types and some that used to be e.g. writable are now only readable (and set another way).

You are going to have to create two separate codebases to do the same job sadly.

Autarch answered 21/6, 2018 at 16:8 Comment(0)
I
1

Does System.Net.WebRequest match your requirement?

var request = WebRequest.Create(YOUR_URL);

It returns an instance of the listed types which derived System.Net.WebRequest:

System.Net.FileWebRequest System.Net.FtpWebRequest System.Net.HttpWebRequest

depending on the protocol specified from the given url. And you can call its GetResponseAsync or GetResponse method to get a System.Net.WebResponse which is also derived by:

System.Net.FileWebResponse System.Net.FtpWebResponse System.Net.HttpWebResponse

An alternative is to use System.Net.Http.HttpClient. It works with System.Net.Http.HttpRequestMessage and System.Net.Http.HttpResponseMessage that you have mentioned. According to the official API document, it is surely in .NET Core API even from .NET Core 1.0.

Microsoft has moved its document from MSDN to learn.microsoft.com and the .NET Core and .NET Standard things are only on this site.

Intact answered 21/6, 2018 at 17:11 Comment(1)
That's the opposite of his question; he wants to return a response for an MVC action.Exergue

© 2022 - 2024 — McMap. All rights reserved.