AspNetCore.Docs icon indicating copy to clipboard operation
AspNetCore.Docs copied to clipboard

.NET 7: New problem details service

Open Rick-Anderson opened this issue 1 year ago • 33 comments

New problem details service

  • [x] Create sample middleware in https://github.com/dotnet/AspNetCore.Docs.Samples/tree/main/fundamentals/middleware from https://github.com/dotnet/AspNetCore.Docs.Samples/pull/25

Sample at https://github.com/dotnet/AspNetCore.Docs.Samples/tree/main/fundamentals/middleware/problem-details-service

Also document #25107


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Rick-Anderson avatar Aug 10 '22 00:08 Rick-Anderson

Would work on this @Rick-Anderson

sammychinedu2ky avatar Aug 14 '22 23:08 sammychinedu2ky

Fantastic

Rick-Anderson avatar Aug 15 '22 03:08 Rick-Anderson

@Rick-Anderson for some reason the problemDetailsService.WriteAsync(new { HttpContext = httpContext }); doesn't accept an anonymous type as they said in the docs and it also doesn't resolve a ProblemDetailsContext class due to the fact that it is marked obsolete. There is also a current issue with the feature https://github.com/dotnet/aspnetcore/issues/43261

sammychinedu2ky avatar Aug 20 '22 08:08 sammychinedu2ky

@Rick-Anderson for some reason the problemDetailsService.WriteAsync(new { HttpContext = httpContext }); doesn't accept an anonymous type as they said in the docs and it also doesn't resolve a ProblemDetailsContext class due to the fact that it is marked obsolete. There is also a current issue with the feature dotnet/aspnetcore#43261

@brunolins16 please review. When should we start the documentation?

Rick-Anderson avatar Aug 22 '22 00:08 Rick-Anderson

@sammychinedu2ky The issue is related to something different. Can you share your example? I very weird because the ProblemDetailsContext should not be marked as obsolete.

brunolins16 avatar Aug 24 '22 17:08 brunolins16

This code below raises the error if (context.RequestServices.GetService<IProblemDetailsService>() is { } problemDetailsService) { problemDetailsService.WriteAsync((new { HttpContext = context });); }

The error I get is : Argument 1: cannot convert from '<anonymous type: Microsoft.AspNetCore.Http.HttpContext HttpContext>' to 'Microsoft.AspNetCore.Http.ProblemDetailsContext'

sammychinedu2ky avatar Aug 24 '22 17:08 sammychinedu2ky

Is there a typo in your comment or it is exactly the code your are using? I am asking because it seems to have some addition parentheses

if (context.RequestServices.GetService<IProblemDetailsService>() is { } problemDetailsService) { 
-   problemDetailsService.WriteAsync((new { HttpContext = context }););
+   problemDetailsService.WriteAsync(new { HttpContext = context });
}

brunolins16 avatar Aug 24 '22 17:08 brunolins16

ooh sorry, this actually problemDetailsService.WriteAsync(new { HttpContext = context });

sammychinedu2ky avatar Aug 24 '22 17:08 sammychinedu2ky

So this code problemDetailsService.WriteAsync(new { HttpContext = context }); is not compiling? Are you using .NET 7 Preview 7?

brunolins16 avatar Aug 24 '22 17:08 brunolins16

I'm using this @brunolins16
Version: 7.0.100-preview.7.22377.5

sammychinedu2ky avatar Aug 24 '22 17:08 sammychinedu2ky

@sammychinedu2ky I just tried, and it looks like some compiler configuration because I can build the same code in aspnetcore repo but not outside. However, I was able to build this without the obsolete error warning:

problemDetailsService.WriteAsync(new ProblemDetailsContext{ HttpContext = context });

brunolins16 avatar Aug 24 '22 18:08 brunolins16

@sammychinedu2ky actually the blog post missed a parentheses, it should be:

if (context.RequestServices.GetService<IProblemDetailsService>() is { } problemDetailsService) { 
-   problemDetailsService.WriteAsync(new { HttpContext = context });
+   problemDetailsService.WriteAsync(new (){ HttpContext = context });
}

Sorry about that.

brunolins16 avatar Aug 24 '22 18:08 brunolins16

@sammychinedu2ky actually the blog post missed a parentheses, it should be:

if (context.RequestServices.GetService<IProblemDetailsService>() is { } problemDetailsService) { 
-   problemDetailsService.WriteAsync(new { HttpContext = context });
+   problemDetailsService.WriteAsync(new (){ HttpContext = context });
}

Sorry about that.

thanks, @brunolins16 . I tried restarting my system and added the parenthesis. but I still get the error below ProblemDetailsContext.ProblemDetailsContext()' is obsolete: 'Constructors of types with required members are not supported in this version of your compiler.' Please is there a step I'm missing for the compiler to work properly

sammychinedu2ky avatar Aug 24 '22 19:08 sammychinedu2ky

Look like it is complaining about the Required Property. Can you share a simple repro? I can try to understand what you are missing.

brunolins16 avatar Aug 24 '22 19:08 brunolins16

When I check class definition I see this

namespace Microsoft.AspNetCore.Http
{
    //
    // Summary:
    //     Represent the current problem details context for the request.
    [RequiredMember]
    public sealed class ProblemDetailsContext
    {
        [CompilerFeatureRequired("RequiredMembers")]
        [Obsolete("Constructors of types with required members are not supported in this version of your compiler.", true)]
        public ProblemDetailsContext();

        //
        // Summary:
        //     The Microsoft.AspNetCore.Http.ProblemDetailsContext.HttpContext associated with
        //     the current request being processed by the filter.
        [RequiredMember]
        public HttpContext HttpContext { get; init; }

@brunolins16

sammychinedu2ky avatar Aug 24 '22 19:08 sammychinedu2ky

I think that is expected when the required is used.

brunolins16 avatar Aug 24 '22 19:08 brunolins16

I think that is expected when the required is used.

but the constructor is marked as obsolete.

sammychinedu2ky avatar Aug 24 '22 19:08 sammychinedu2ky

I think the latest compilers handle this.

brunolins16 avatar Aug 24 '22 19:08 brunolins16

problemDetailsService.WriteAsync(new ProblemDetailsContext{ HttpContext = context });

alright thanks .. would be waiting for it

sammychinedu2ky avatar Aug 24 '22 19:08 sammychinedu2ky

@sammychinedu2ky go with Bruno's recommendation. If you like, create an issue to track this.

Rick-Anderson avatar Aug 24 '22 19:08 Rick-Anderson

@sammychinedu2ky go with Bruno's recommendation. If you like, create an issue to track this.

Thanks @Rick-Anderson , I took note of his recommendations but still got the same issue, he said the latest compiler would fix it.

sammychinedu2ky avatar Aug 24 '22 20:08 sammychinedu2ky

The weird thing is that you should be in the latest compiler version, right? Because i cannot repro here. Can you share your csproj? Or a github Repo?

brunolins16 avatar Aug 24 '22 20:08 brunolins16

here is a sample repo @brunolins16 https://github.com/sammychinedu2ky/problemdetails.git

sammychinedu2ky avatar Aug 24 '22 20:08 sammychinedu2ky

image

Currently that is the issue I'm having

sammychinedu2ky avatar Aug 24 '22 20:08 sammychinedu2ky

here is a sample repo @brunolins16 Bruno Oliveira FTE https://github.com/sammychinedu2ky/problemdetails.git

I cloned your repo and it is building correctly? It looks like you have something misconfigured on your side. Can you try run dotnet build -bl and share the msbuild.binlog file with me?

brunolins16 avatar Aug 24 '22 20:08 brunolins16

dotnet build -bl

okay, have made an update , the new recent commit has the msbuild.binlog file https://github.com/sammychinedu2ky/problemdetails.git

sammychinedu2ky avatar Aug 24 '22 20:08 sammychinedu2ky

@sammychinedu2ky is it building successfully? It that just about the red squiggly?

brunolins16 avatar Aug 24 '22 20:08 brunolins16

ooh I'm sorry it is building successfully, it is just the squiggly sign.

sammychinedu2ky avatar Aug 24 '22 20:08 sammychinedu2ky

seems its from Omni sharp. please is there a reference you have for the dotnet build -bl command, as I don't understand what it is for. I could read up and learn about it @brunolins16

sammychinedu2ky avatar Aug 24 '22 20:08 sammychinedu2ky

Are you using VS Code? Maybe the extensions is not updated yet, not sure about it.

brunolins16 avatar Aug 24 '22 20:08 brunolins16