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.
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)
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.
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()
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.
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.
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?
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
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:
ContentTypeService will then not throw the content type save notification due to the .Suppress()
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.
is working on a reply...