6

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?

6
  • See this answer: stackoverflow.com/a/70847316/1064169 Commented Aug 7, 2023 at 8:04
  • hmm Microsoft.AspNetCore.App is also deprecated @MartinCostello Commented Aug 7, 2023 at 8:05
  • 2
    Please read the answer again - note that it does not use a PackageReference. Commented Aug 7, 2023 at 8:06
  • @user122222 no, it is not, you need to add FrameworkReference not package reference Commented Aug 7, 2023 at 8:06
  • @user122222 Yes, it has been deprecated because it's been rolled in to ASP.NET Core. Commented Aug 7, 2023 at 8:06

1 Answer 1

18

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>
Sign up to request clarification or add additional context in comments.

5 Comments

Unfortunately deprecated as well
@TaylorMaxwell no, it's not. Please check answer carefully, it is not a package reference it is FrameworkReference
This does not work for Blazor in .Net 8. You get an error: There was no runtime pack for Microsoft.AspNetCore.App available for the specified RuntimeIdentifier 'browser-wasm'.
@Josh I believe that is wasm problem, not .net 8. At least some packs like that work only on blazor server projects
God bless you!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.