Previously I used Microsoft.AspNetCore.Http to handle HTTP requests:
public async Task HandleRequest(HttpContext context)
{
await HandleInternal(context, async (_, ct) =>
{
version = context.GetRouteParameter<long>("version");
await Read(context.Response.Body, 0, ct);
});
}
private async Task HandleInternal(HttpContext context, Func<IService, CancellationToken, Task> action)
{
var format = GetAcceptedFormat(context.Request, _serv.Keys);
if (!Started)
{
await context.FailWith(HttpStatusCode.ServiceUnavailable);
}
else if (_serv.TryGetValue(format, out IService serv))
{
try
{
context.Response.ContentType = format;
using (var cancellationSource = CancellationTokenSource.CreateLinkedTokenSource(_hostCancellationToken, context.RequestAborted))
{
await action(_, cancellationSource.Token);
}
}
catch (ParameterException e)
{
await context.FailWith(HttpStatusCode.BadRequest, e.Message);
}
catch (KeyNotFoundException)
{
await context.FailWith(HttpStatusCode.NotFound, "Not found");
}
catch (Exception e)
{
_log.LogError(e, "An exception occurred while writing the response");
await context.FailWith(HttpStatusCode.InternalServerError, e.ToString());
}
}
else
{
await context.FailWith(HttpStatusCode.NotImplemented, $"Requested Content-Type {format} is not supported with current configuration.");
}
}
public Task Read(Stream target, long version, CancellationToken ct)
{
return Task.Run(() =>
{
_cache.InTransaction(() =>
{
using var reader = _cache.GetReader();
var (lastVersion, items) = (reader.GetVersion(), ReadByType(reader, version));
_responseBuilder.WriteFrom(target, items, lastVersion, ct);
});
}, ct);
}
however this lib is deprecated and HttpContext is no longer available. What do you change it with?
PackageReference.FrameworkReferencenot package reference