AspNetCore.Docs
AspNetCore.Docs copied to clipboard
.NET 7: 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.
- ID: 3f6504cd-a059-c5d4-73fb-61222ef1c6e0
- Version Independent ID: 1b42352c-a34f-7703-aa3e-d7e3156bd0bd
- Content: Handle errors in ASP.NET Core web APIs
- Content Source: aspnetcore/web-api/handle-errors.md
- Product: aspnet-core
- Technology: aspnetcore-webapi
- GitHub Login: @Rick-Anderson
- Microsoft Alias: riande
Would work on this @Rick-Anderson
Fantastic
@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
@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 aProblemDetailsContext
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?
@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.
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'
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 });
}
ooh sorry, this actually
problemDetailsService.WriteAsync(new { HttpContext = context });
So this code problemDetailsService.WriteAsync(new { HttpContext = context });
is not compiling? Are you using .NET 7 Preview 7?
I'm using this @brunolins16
Version: 7.0.100-preview.7.22377.5
@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 });
@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.
@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
Look like it is complaining about the Required Property. Can you share a simple repro? I can try to understand what you are missing.
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
I think that is expected when the required
is used.
I think that is expected when the
required
is used.
but the constructor is marked as obsolete.
I think the latest compilers handle this.
problemDetailsService.WriteAsync(new ProblemDetailsContext{ HttpContext = context });
alright thanks .. would be waiting for it
@sammychinedu2ky go with Bruno's recommendation. If you like, create an issue to track this.
@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.
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?
here is a sample repo @brunolins16 https://github.com/sammychinedu2ky/problemdetails.git
Currently that is the issue I'm having
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?
dotnet build -bl
okay, have made an update , the new recent commit has the msbuild.binlog file https://github.com/sammychinedu2ky/problemdetails.git
@sammychinedu2ky is it building successfully? It that just about the red squiggly?
ooh I'm sorry it is building successfully, it is just the squiggly sign.
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
Are you using VS Code? Maybe the extensions is not updated yet, not sure about it.