Skip to content
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

Does not work in .net 6 #182

Open
JasonRodman opened this issue Aug 22, 2022 · 9 comments
Open

Does not work in .net 6 #182

JasonRodman opened this issue Aug 22, 2022 · 9 comments

Comments

@JasonRodman
Copy link

I spent a full day trying to implement this library into an existing .net 6 application with no luck. I kept running into this error #157 and then a host of subsequent errors related to dependencies of IActionResultExecutor that were not registered. Upgrading to your latest nuget package 6.5.1 did not help. It added the registration for ObjectResultExecutor but none of its dependencies such as OutputFormatterSelector and IHttpResponseStreamWriterFactory. The dependency chain needed for this to work in a simple minimal api application is not there due the coupling to mvc. I was able to prove this by creating a brand new .net6 project, unchecking the option for controllers, and pasting your program.cs file from your minimal api example right in and running it. It fails on the builder.Build() line with this error:

System.AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Mvc.Infrastructure.IActionResultExecutor1[Microsoft.AspNetCore.Mvc.ObjectResult] Lifetime: Singleton ImplementationType: Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor': Unable to resolve service for type 'Microsoft.AspNetCore.Mvc.Infrastructure.OutputFormatterSelector' while attempting to activate 'Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor'.) ---> System.InvalidOperationException: Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Mvc.Infrastructure.IActionResultExecutor1[Microsoft.AspNetCore.Mvc.ObjectResult] Lifetime: Singleton ImplementationType: Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor': Unable to resolve service for type 'Microsoft.AspNetCore.Mvc.Infrastructure.OutputFormatterSelector' while attempting to activate 'Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor'.
---> System.InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Mvc.Infrastructure.OutputFormatterSelector' while attempting to activate 'Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor'.
trucated for brevity

I tried adding these dependencies myself, but get stuck when adding one for IHttpResponseStreamWriterFactory since the class used by mvc is private and sealed. At that point I gave up. I am hoping you have some simple fix for this since I like your library. But honestly I think you may need to think about splitting your library, one for older mvc and the other for .net6+ minimal api and remove all mvc dependencies from it. The way things are going with .net6/7 much of what you have in your library is becoming obsolete or no longer the recommended best practice. Hope this helps guys.

@kushal2901
Copy link

can you share code?

@JasonRodman
Copy link
Author

JasonRodman commented Aug 24, 2022

I will try to put together a sample project for you as soon as I can. In the meantime if you want to try it yourself, just do what I did to create a blank aspnet core web api project and uncheck the controllers option. I then just copied and pasted the program.cs from your minimal api sample over the one it created and ran it.

image

@TimothyMakkison
Copy link
Contributor

Good attempt at fixing it, pretty sure I tried the exact same thing. You can resolve the error by adding:

// ProblemDetails requires the registration of IActionResultExecutor<ObjectResult>
// this is done by invoking either AddMvcCore() or AddControllers()
builder.Services.AddControllers();

Calling either AddMvcCore() or AddControllers() will add IActionResultExecutor<ObjectResult> and the required dependencies to construct ObjectResultExecutor.

@TimothyMakkison
Copy link
Contributor

It appears that when solving #157, commit 1857120 registered ObjectResultExecutor, this broke the sample after commit 5d5257e removed AddControllers which would normally resolve these dependencies.

A couple of solutions:

  • Commit 5d5257e could be temporarily reverted so the sample works
  • Attempt to register the dependencies for ObjectResultExecutor (no idea how to add IHttpResponseStreamWriterFactory) inside AddProblemDetails
  • Replace ObjectResultExecutor with MinimalApiResultExecutor from 0b8552b, I don't know enough about the internals of MVC to know if this is a good replacement

@JasonRodman
Copy link
Author

I would highly recommend the last option. The goal of minimal apis is to avoid adding all the bloat from mvc/controllers when we don't need it. You could always go the route of detecting whether ObjectResultExecutor is registered and fall back to the other when its not available.

@khellang
Copy link
Owner

Replacing ObjectResultExecutor with MinimalApiResultExecutor will just bring back the problem I initially tried to solve (#180). I have an idea on how to fix this, but I need to do some experimentation. In the meantime, you can just revert back to v6.5.0, which uses the MinimalApiResultExecutor.

@zyofeng
Copy link

zyofeng commented Oct 3, 2022

Replacing ObjectResultExecutor with MinimalApiResultExecutor will just bring back the problem I initially tried to solve (#180). I have an idea on how to fix this, but I need to do some experimentation. In the meantime, you can just revert back to v6.5.0, which uses the MinimalApiResultExecutor.

Having the same problem Im refactoring a new project from mvc to minimalapi. Any ETAs?

@LodewijkSioen
Copy link

I was looking at your code as an inspiration to render a Fhir OperationOutcome instead of ProblemDetails when I ran into the same issue. I was able to fix it by subclassing ObjectResult and use that as the generic parameter of IActionResultExecutor in the implementation of MinimalApiResultExecutor.

This might solve the issue here as well.

@filipoff2
Copy link

filipoff2 commented Oct 1, 2024

Same one .NET8 "web api" console app after update 6.5.0 to 6.5.1..
I have to 2 project they have different references one works other don't

I had to add
services.AddControllers();
to not working project to make it work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants