Copied to clipboard

Flag this post as spam?

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


  • Martin Rud 261 posts 1022 karma points c-trib
    Nov 26, 2022 @ 05:46
    Martin Rud
    0

    Error using 'ContentPublishingNotification' ('cannot be used as type parameter...')

    Hi forum,

    I want a property value to be filled in and saved automatically if it's empty.

    This works, but it triggers AFTER publish which is not what I need:

    public void Handle(ContentPublishedNotification notification)
    {
        foreach (var node in notification.PublishedEntities)
        {
            if (node.HasProperty("hashValue"))
            {
                var hashValue = node.GetValue<string>("hashValue");
                if (hashValue == null)
                {
                    node.SetValue("hashValue", RandomString(6));
                }
            }
        }
    }
    

    Full code can be seen here: https://goonlinetools.com/snapshot/code/#iejbo6av3cdcw77czwmimq

    This (ContentPublishedNotification changed to ContentPublishingNotification) is what I need, but it gives the error:

    public void Handle(ContentPublishingNotification notification)
    {
        foreach (var node in notification.PublishedEntities)
        {
            if (node.HasProperty("hashValue"))
            {
                var hashValue = node.GetValue<string>("hashValue");
                if (hashValue == null)
                {
                    node.SetValue("hashValue", RandomString(6));
                }
            }
        }
    }
    

    Full error is:

    cannot be used as type parameter 'TNotificationHandler' in the generic type or method 'UmbracoBuilderExtensions.AddNotificationHandler<TNotification, TNotif 
    icationHandler>(IUmbracoBuilder)'. There is no implicit reference conversion from 'MrContentService.DontShout' to 'Umbraco.Cms.Core.Events.INotificationHand 
    ler<Umbraco.Cms.Core.Notifications.ContentPublishedNotification>'. [C:\Users\marti\OneDrive - Martin Rud\Dev2.0\wallscreen_umbracoHeadless\wal 
    lscreen_umbracoHeadless.csproj]
    
  • Bo Jacobsen 610 posts 2409 karma points
    Nov 26, 2022 @ 15:51
    Bo Jacobsen
    100

    Hi Martin.

    It seems the error is thrown where you add the notification handler.

    Maybe you forgot to change from ContentPublishedNotification to ContentPublishingNotification?

    So either:

    public class DontShout : INotificationHandler<ContentPublishedNotification>
    to 
    public class DontShout : INotificationHandler<ContentPublishingNotification>
    

    or

    builder.AddNotificationHandler<ContentPublishedNotification, DontShout>();
    to
    builder.AddNotificationHandler<ContentPublishingNotification, DontShout>();
    
  • Martin Rud 261 posts 1022 karma points c-trib
    Nov 26, 2022 @ 20:47
    Martin Rud
    0

    Cool, thanks. You were right; I haven't updated the Startup.cs correctly:

    It was

    public void ConfigureServices(IServiceCollection services)
            {
                services.AddUmbraco(_env, _config)
                    .AddBackOffice()
                    .AddWebsite()
                    .AddComposers()
                    .AddNotificationHandler<ContentPublishedNotification, AddHashValue>()
                    .Build();
            }
    

    But should be:

    public void ConfigureServices(IServiceCollection services)
            {
                services.AddUmbraco(_env, _config)
                    .AddBackOffice()
                    .AddWebsite()
                    .AddComposers()
                    .AddNotificationHandler<ContentPublishingNotification, AddHashValue>()
                    .Build();
            }
    

    (have also changed "DontShout" to "AddHashValue"), but that's only for making more sense. :)

  • 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