-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow using the DefaultProblemDetailsFactory
without adding MVC Core
#49982
Comments
@Carl-Hugo Thanks for filing this issue! The newer
Based on some of what I'm seeing about how your interacting with the problem details type here this might be the best option? |
Hi @Carl-Hugo. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time. |
Hi @captainsafia, thanks for the suggestion, which sounds great to globally extend what .NET handles automatically or for a user of my library to extend it. However, in my case, I want to align with the default experience, hence tapping into the existing pieces, including the new The "issue" with the An example of the convenience of using the factory is that the The Please let me know if you want more info. |
I would also like to see this situation improve. We use a global exception handling filter, which I want to use to generate a problem details response to the caller. So I have to inject a ProblemDetailsFactory, except this is breaking my unit tests. Mocking the factory makes no sense, yet I can't get an instance of it either to use in my test because it's internal. |
@Carl-Hugo Apologies for the delay in my response to you! This got bumped up in my inbox following @jeremy-allocate's comment. As a tactical change, we can consider bringing in an API proposal to apply the following:
IMO, this seems like the most appealing API choice compared to our others one. I recognize the bifurcation of ProblemDetails-related types in the HTTP-layer and the MVC-layer. Would a new API following the above solve your problems? |
Hi @captainsafia, That sounds like a quick fix that would allow us to use the default factory outside of MVC (and without registering all MVC "Core" dependencies). So yes, I believe it would solve my problem. However, the problem details space would remain confusing and scattered and could still benefit from a bit of refactoring. This second larger conversion could probably live better as part of its own GitHub issue, though; what do you think? Thanks |
We can start it in the GitHub issues for now since that's the most accessible.
As you noticed here, one of the problems with Particularly, in the A combination of these factors are why we have situations like:
The building blocks aren't reusable without type-forwarding or considerable modifications (as in the case of It's definitely not an ideal situation, but I think a good path forward is improving the new lower-level primitives and making it relatively painless for people to move between the two patterns in the case that they want to. I hope this explainer provides some insights into how we got here and the priorities I think we are trying to balance. 😅 |
I understand that you want to limit the number of breaking changes and have limited capacity. Thanks for the insights, @captainsafia. As I mentioned before, adding a new |
Thanks for the understanding here! Hopefully, this will provide some insight to others as well as to how we got here.
Great! Would you be interested in opening a new API proposal for this using the template. I can champion it at our next API review meeting, which will likely occur after the holidays. |
Certainly, I'll try to do that during the holidays. |
Summary
I want to use the
DefaultProblemDetailsFactory
class without calling theAddMvcCore
method.Motivation and goals
I aim to leverage a
ProblemDetailsFactory
implementation (say theDefaultProblemDetailsFactory
class) without adding MVC services to a Minimal API project.More context: I'm updating older code to support .NET 7-8 that uses the
ProblemDetailsFactory
to generateProblemDetails
instances. The workaround was to callAddMvcCore
then, which was kinda ok since there was only MVC before. However, now that we have Minimal APIs, adding MVC dependencies to a non-MVC project makes no sense.Here are a few facts:
DefaultProblemDetailsFactory
class isinternal
. See DefaultProblemDetailsFactory.cs for more info.DefaultProblemDetailsFactory
to the container is by calling theAddMvcCore
method. See MvcCoreServiceCollectionExtensions.cs for more info.AddMvcCore
method adds a lot of dependencies for no reason.AddProblemDetails
method does not register anyProblemDetailsFactory
with the container. See ProblemDetailsServiceCollectionExtensions.cs for more info.IProblemDetailsService
interface does not directly use theProblemDetailsFactory
, yet leveraging the factory to create an instance of theProblemDetails
class before setting theProblemDetailsContext
would allow consumers to reuse .NET building blocks, making the creation of the objects more "standard" and in line with other parts of .NET (a.k.a. reusing the registeredProblemDetailsFactory
implementation). For example, adding thetraceId
and leveraging theApiBehaviorOptions.ClientErrorMapping
property to get/set some default values.ProblemDetailsFactory
andProblemDetails
classes are part of MVC while their reach now goes beyond MVC.ProblemDetailsContext
class references theProblemDetails
class.IProblemDetailsService
interface uses theProblemDetailsContext
class.ProblemDetailsContext
andIProblemDetailsService
are not part of MVC.In scope
Allow registering the
DefaultProblemDetailsFactory
with the container without calling theAddMvcCore
method.DefaultProblemDetailsFactory
class visibility topublic
and letting the non-MVC consumers add the binding themselves.AddDefaultProblemDetailsFactory
that adds the binding and haveAddMvcCore
call this new method instead.TryAddSingleton<ProblemDetailsFactory, DefaultProblemDetailsFactory>()
call in theAddProblemDetails
method to register theDefaultProblemDetailsFactory
on top of theIProblemDetailsService
.Or any other ways you think would be best. I am ok with creating a PR if there is some interest.
Out of scope
Risks / unknowns
I don't see that many risks, if any.
The text was updated successfully, but these errors were encountered: