Copied to clipboard

Flag this post as spam?

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


  • AbsolutelyN 89 posts 437 karma points
    Nov 11, 2022 @ 18:29
    AbsolutelyN
    0

    MediaService.Save - without raising events? Umbraco 10.

    I'm converting a Umbraco 8 site to 10. It uses azure to analyse media as they are uploaded.

    To do this I'm now using MediaSavedNotificationHandler. However I need to save the changes back to the image. Previously there was a raiseEvents=false option.

    It even refers to this in the docs for v10: "NOTE: It can be skipped completely if the parameter "raiseEvents" is set to false during the Save method call (true by default)." https://our.umbraco.com/documentation/reference/Notifications/MediaService-Notifications/

    However the MediaManager in v10 doesn't have an overload accepting raiseEvents that I can find.

    public Attempt<OperationResult?> Save(IMedia media, int userId = Constants.Security.SuperUserId)
    
    public Attempt<OperationResult?> Save(IEnumerable<IMedia> medias, int userId = Constants.Security.SuperUserId)
    

    https://github.com/umbraco/Umbraco-CMS/blob/b69afe81f3f6fcd37480b3b0295a62af44ede245/src/Umbraco.Core/Services/MediaService.cs

    How do you prevent events triggering (and infinite loop) when you save media within a media event handler?

  • MrFlo 159 posts 403 karma points
    Nov 14, 2022 @ 14:16
    MrFlo
    0

    Same question for me. I can't find anywhere that information.

    It seems it was deleted from the code: https://github.com/umbraco/Umbraco-CMS/commit/113b6598edacce78f828c0bff9a8dc514caadf49

  • Patrick de Mooij 73 posts 623 karma points MVP 3x c-trib
    Nov 14, 2022 @ 15:02
    Patrick de Mooij
    1

    There is still a way to prevent notifications (the new events) from being called. You have to suppress the notifications so that they aren't send to the listeners.

    There are some tests here showing you how to use it: https://github.com/umbraco/Umbraco-CMS/blob/b69afe81f3f6fcd37480b3b0295a62af44ede245/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Scoping/SupressNotificationsTests.cs#L35

    But it basically comes down to something like this:

    using var scope = ScopeProvider.CreateScope(autoComplete: true);
    using var _ = scope.Notifications.Suppress();
    
    var contentType = ContentTypeBuilder.CreateBasicContentType();
    ContentTypeService.Save(contentType);
    

    ContentTypeService will then not throw the content type save notification due to the .Suppress()

  • AbsolutelyN 89 posts 437 karma points
    Nov 14, 2022 @ 15:04
    AbsolutelyN
    0

    In the old code I was upgrading it was calling a service that had media service injected into it. I changed the code so that it only saved the media from within the notification handler class itself and it now all seems to work fine.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies