Copied to clipboard

Flag this post as spam?

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


  • Martino 14 posts 135 karma points c-trib
    Feb 26, 2021 @ 08:27
    Martino
    0

    Using IScopeProvider inside UmbracoAuthorizedApiController

    Hi, I'm building a custom content app with a custom table inside Umbraco's database:

        [PluginController("MyApi")]
        [UmbracoApplicationAuthorize(Constants.Applications.Content)]
        public class MyApiController : UmbracoAuthorizedApiController
        {
            private readonly IUmbracoContextAccessor _umbracoContext;
            private readonly IContentTypeService _contentTypeService;
            private readonly IDataTypeService _dataTypeService;
            private readonly IPublishedContentCache _contentCache;
    
            private readonly IScopeProvider _scopeProvider;
    
    
            public MyApiController() { }
    
            public MyApiController(IUmbracoContextAccessor umbracoContext,
                IContentTypeService contentTypeService,
                IDataTypeService dataTypeService,
                IPublishedContentCache contentCache)
            {
                _umbracoContext = umbracoContext;
                _contentTypeService = contentTypeService;
                _dataTypeService = dataTypeService;
                _contentCache = contentCache;
            }
    
            private IEnumerable<MyModel> DoSomethingThatMightBeUseful(int nodeId)
            {
                IEnumerable<MyModel> result = null;
                try
                {
                    using (var scope = Current.ScopeProvider.CreateScope())
                    {
                        result = scope.Database.Fetch<MyModel>("SELECT * FROM table");
    
                        scope.Complete();
                    }
    
                } catch {}
    
                return result;
            }
        }
    

    Everything was going fine until I stumbled upon this comment: https://our.umbraco.com/forum/umbraco-8/96075-alternative-solution-to-use-applicationcontextcurrentdatabasecontextdatabase-in-umbraco-8#comment-303960

    So I switched from Current.ScopeProvider.CreateScope() to injecting IScopeProvider inside the constructor, like so:

        [PluginController("MyApi")]
        [UmbracoApplicationAuthorize(Constants.Applications.Content)]
        public class MyApiController : UmbracoAuthorizedApiController
        {
            private readonly IUmbracoContextAccessor _umbracoContext;
            private readonly IContentTypeService _contentTypeService;
            private readonly IDataTypeService _dataTypeService;
            private readonly IPublishedContentCache _contentCache;
    
            private readonly IScopeProvider _scopeProvider;
    
    
            public MyApiController() { }
    
            public MyApiController(IUmbracoContextAccessor umbracoContext,
                IContentTypeService contentTypeService,
                IDataTypeService dataTypeService,
                IPublishedContentCache contentCache,
                IScopeProvider scopeProvider)
            {
                _umbracoContext = umbracoContext;
                _contentTypeService = contentTypeService;
                _dataTypeService = dataTypeService;
                _contentCache = contentCache;
                _scopeProvider = scopeProvider;
            }
    
            private IEnumerable<MyModel> DoSomethingThatMightBeUseful(int nodeId)
            {
                IEnumerable<MyModel> result = null;
                try
                {
                    using (var scope = _scopeProvider.CreateScope())
                    {
                        result = scope.Database.Fetch<MyModel>("SELECT * FROM table");
    
                        scope.Complete();
                    }
    
                } catch {}
    
                return result;
            }
        }
    

    And that's when the problems began: something was not working properly. Went into debug mode, and discovered that _scopeProvider is null.

    So since it was not a random person that made the comment that made me switch to DI, clearly i'm missing something.

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Feb 26, 2021 @ 08:39
    Nik
    100

    Hi Martino,

    Your controller has 2 constuctors on it. I suspect the issue is that the constructor with zero parameters is being called so try removing that and see if it works for you.

    Thanks

    Nik

  • Martino 14 posts 135 karma points c-trib
    Feb 26, 2021 @ 08:56
    Martino
    1

    Hi Nik! Thanks for the ultra-fast reply. That worked for me, thanks a lot!

Please Sign in or register to post replies

Write your reply to:

Draft