Umbraco-cms: MacroService.GetAll(Guid[]) throws an error

Created on 19 Jul 2019  路  5Comments  路  Source: umbraco/Umbraco-CMS

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.

Reproduction

Bug summary

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.

Specifics

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

Steps to reproduce

  • Create a Macro in the back-office, make note of the GUID
  • Then, wherever you want to try the C#, controller or Razor view, whichever, try this code...
var guid = new Guid("INSERT-MACRO-GUID-HERE");
var macros = Services.MacroService.GetAll(guid);
foreach (var macro in macros)
{
    // do stuff
}
  • As soon as the enumerable is evaluated, the exception will be thrown.

Expected result

  • I expected a list of Macro objects (by given GUID) to be returned.

Actual result

  • The exception, see above.
communitpr typbug

All 5 comments

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

Was this page helpful?
0 / 5 - 0 ratings