- 
                Notifications
    You must be signed in to change notification settings 
- Fork 10.5k
Description
Summary
I want to use the DefaultProblemDetailsFactory class without calling the AddMvcCore method.
Motivation and goals
I aim to leverage a ProblemDetailsFactory implementation (say the DefaultProblemDetailsFactory 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 generate ProblemDetails instances. The workaround was to call  AddMvcCore 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:
- The visibility modifier of the DefaultProblemDetailsFactoryclass isinternal. See DefaultProblemDetailsFactory.cs for more info.
- The only way I found to add the DefaultProblemDetailsFactoryto the container is by calling theAddMvcCoremethod. See MvcCoreServiceCollectionExtensions.cs for more info.
- In a non-MVC project, the AddMvcCoremethod adds a lot of dependencies for no reason.
- The newer AddProblemDetailsmethod does not register anyProblemDetailsFactorywith the container. See ProblemDetailsServiceCollectionExtensions.cs for more info.
- The IProblemDetailsServiceinterface does not directly use theProblemDetailsFactory, yet leveraging the factory to create an instance of theProblemDetailsclass before setting theProblemDetailsContextwould 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 registeredProblemDetailsFactoryimplementation). For example, adding thetraceIdand leveraging theApiBehaviorOptions.ClientErrorMappingproperty to get/set some default values.
- I find it a little confusing that the ProblemDetailsFactoryandProblemDetailsclasses are part of MVC while their reach now goes beyond MVC.- The ProblemDetailsContextclass references theProblemDetailsclass.
- The IProblemDetailsServiceinterface uses theProblemDetailsContextclass.
- Both ProblemDetailsContextandIProblemDetailsServiceare not part of MVC.
 
- The 
In scope
Allow registering the DefaultProblemDetailsFactory with the container without calling the AddMvcCore method.
- This could be as simple as changing the DefaultProblemDetailsFactoryclass visibility topublicand letting the non-MVC consumers add the binding themselves.
- Another option would be to create a public extension method named AddDefaultProblemDetailsFactorythat adds the binding and haveAddMvcCorecall this new method instead.
- Another option would be to add a TryAddSingleton<ProblemDetailsFactory, DefaultProblemDetailsFactory>()call in theAddProblemDetailsmethod to register theDefaultProblemDetailsFactoryon 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
- Everything else.
Risks / unknowns
I don't see that many risks, if any.