Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
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:
Current.ScopeProvider.CreateScope()
IScopeProvider
[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.
_scopeProvider
So since it was not a random person that made the comment that made me switch to DI, clearly i'm missing something.
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
Hi Nik! Thanks for the ultra-fast reply. That worked for me, thanks a lot!
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
Using IScopeProvider inside UmbracoAuthorizedApiController
Hi, I'm building a custom content app with a custom table inside Umbraco's database:
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 injectingIScopeProvider
inside the constructor, like so: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.
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
Hi Nik! Thanks for the ultra-fast reply. That worked for me, thanks a lot!
is working on a reply...