Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to access HttpContext in mediatr command without installing deprecated nuget packages

I have a .NET 6 API and I want to get the HttpContext in a mediatr command, I have read on the docs it is possible to register the HttpContextAccessor with builder.Services.AddHttpContextAccessor(); which I did with no issues, but when I want to inject IHttpContextAccessor in the constructor of the command, my IDE tells it cannot resolve the symbol. I’ve looked on multiple other sources as well, they mostly point to either microsoft.aspnetcore.http or microsoft.aspnetcore.app to be installed. Sadly these nuget packages are deprecated, so I’m searching for a solution which doesn’t use these.

PS: the api and mediatr commands are not in the same project

I’ve searched on multiple other sites to find an answer, but they either don’t specify the package they are using, or they use a deprecated one.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

To use ASP.NET Core 3+ types in non-web.sdk projects, use shared framework reference.

However, note that this exists primarily to provide libraries with endpoints etc. Using this reference in business layer couples business logic with web API framework.

A more flexible approach is to provide an interface for the needed capability in the business logic project and provide implementation in the API project.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading