Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace Microsoft.AspNetCore.Http

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?

like image 411
user122222 Avatar asked Aug 26 '26 15:08

user122222


1 Answers

If you want to use ASP.NET Core parts in a class library project then you need to add <FrameworkReference Include="Microsoft.AspNetCore.App"/> to the .csproj file as explained in the docs:

<Project Sdk="Microsoft.NET.Sdk">
  <!--... rest of file-->

  <ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
  </ItemGroup>

  <!--... rest of file-->
</Project>
like image 62
Guru Stron Avatar answered Aug 29 '26 04:08

Guru Stron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!