Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Daniel 4 posts 85 karma points
    Jul 30, 2024 @ 02:19
    Daniel
    0

    Accessing DB Scope within a static class (v13+)

    Hey all!

    Sorry if this might be an obvious one, but I've seen other threads on the forums about this that were answered years back on previous versions and none of the solutions seem to be relevant any more - which I think is down to changes in how the active scopes / contexts are handled.

    Firstly, am I right in thinking that I can't simply inject IScopeProvider into a static class - and that it only works with the built in Umbraco controller types (RenderController / UmbracoApiController)? I had tried this originally but it didn't seem to work.

    Ideally, I'm just looking to hook up some helper classes that I want to run independant from the Umbraco controllers where I still have access to the database, to query custom non-Umbraco tables.

    If anyone has any suggestions for the best way to go about this, it would be greatly appreciated. I can get it working with some rather awkward scope injection (passing around the scope object in a way that it's eventually injected into the helper class) but it feels very cumbersome and restrictive. I could also just go totally outside the realm of Umbraco/NPoco and just grab the existing raw connection string from appsettings.json and handle the database interaction separately but that feels very weird too - and I'm worried about opening multiple connections within the same application context.

    P.S. I'm very new to all things .NET core, dependency injection scopes, etc. Last Umbraco version I used was v8, which was prior to a lot of the major framework changes. Been doing my best to learn as much as I can and catch up.

    Thanks!

Please Sign in or register to post replies

Write your reply to:

Draft