PR https://github.com/umbraco/Umbraco-CMS/pull/5962
When I attempt to use the MacroService to get a selection of Macro's by Guid, an error is thrown.
When calling MacroService.GetAll method, passing in an array of Guids, the following exception is thrown...
Exception Details:
System.InvalidOperationException: Cannot run a repository without an ambient scope.
Umbraco version: v8.1.0
Stack Trace snippet:
[InvalidOperationException: Cannot run a repository without an ambient scope.]
Umbraco.Core.Persistence.Repositories.Implement.RepositoryBase`2.get_AmbientScope() in d:\a\1\s\src\Umbraco.Core\Persistence\Repositories\Implement\RepositoryBaseOfTIdTEntity.cs:43
Umbraco.Core.Persistence.Repositories.Implement.MacroRepository.GetBaseQuery() in d:\a\1\s\src\Umbraco.Core\Persistence\Repositories\Implement\MacroRepository.cs:109
Umbraco.Core.Persistence.Repositories.Implement.MacroRepository.Get(Guid id) in d:\a\1\s\src\Umbraco.Core\Persistence\Repositories\Implement\MacroRepository.cs:31
var guid = new Guid("INSERT-MACRO-GUID-HERE");
var macros = Services.MacroService.GetAll(guid);
foreach (var macro in macros)
{
// do stuff
}
Oh... this looks similar to something else I fixed a while back, let me dig through things and see if I can apply the fix here.
Ah... LINQ, my old friend. PR in #5962
@leekelleher on a related note: After reviewing the macro repository, I would highly recommend you use integer IDs instead of GUIDs to fetch your macros - e.g. var macros = Services.MacroService.GetAll(2, 3, 4). This method utilizes the cache layer, whereas the GUID based one seems to access the DB on every single hit.
Thanks @kjac, I appreciate it! I've been meaning to take a look at patching it myself, but it's already been on my to-do list for 2 months, doh! Thought it best to raise the bug report.
I'm using GUIDs because it's one less Deploy thing to worry about. (And we're meant to be "GUID ALL THINGS!" now, right? 馃槈)
Thanks again for the patch! #h5yr
@leekelleher I figured as much. Just thought you should know all the same 馃槃
Closing this ticket, fix supplied by @kjac in PR #5962