November Happy Hour will be moved to Thursday December 5th.

'EventRegistry' does not contain a definition for 'Instance'

Vote:
 

In EPiServer CMS 11.20.5, I tried to define an Event as in the "Using the Event API" documentation. (world.episerver.com/documentation/developer-guides/CMS/event-management/Event-Management-API/)
(I can't add the link since I have to Edit profile with company, but Edit profile in this site doesn't work for me, anyway it's a seperate issue.)

However for EventRegistry, I get to import this namespace:

using EPiServer.Events.Clients.Internal;

and it says: "Unsupported INTERNAL API!"

and also: 'EventRegistry' does not contain a definition for 'Instance'.

Am I missing something here?

#252310
Apr 01, 2021 8:15
Vote:
 

Hi,

I suspect to get the event registry you should be fetching it via dependency injection rather than calling EventRegistry.Instance. If you're in an IInitializableModule, you can do it like this

public class MyInitialisationModule : IInitializableModule
{
    public void Initialize(InitializationEngine context)
    {
        var eventRegistry = context.Locate.Advanced.GetInstance<EPiServer.Events.Clients.IEventRegistry>();
        // Do stuff here
    }

    public void Uninitialize(InitializationEngine context)
    {
        //Add uninitialization logic
    }
}
#252370
Apr 01, 2021 10:07
Vote:
 

The documentation is outdated, I will let the CMS Core/Doc team know about it.

Paul's suggestion is the right one, you should be using IEventRegistry instead

#252372
Apr 01, 2021 10:39
Vote:
 

Thanks for the replies. 

Getting an instance of IEventRegistry from InitializationEngine inside the Initialize method works ok to define what should be done once the event is raised. 

But it is not clear to me, how to get the IEventRegistry in the public method that we call to trigger/raise the event? 
I mean in the RaiseEvent() in the documentation. 

public void RaiseEvent(string message) { 

// without InitializationEngine context, how to get the IEventRegistry here, to raise the event?

}
#252374
Edited, Apr 01, 2021 11:15
Vote:
 

Without the InitializationEngine there are a few ways. The easiest (though not best) route would be to use ServiceLocator, for example:

var eventRegistry = ServiceLocator.Current.GetInstance<IEventRegistry>();

A better option would be to use constructor injection though that's not available in all situations. For example:

public class MyController : PageController<MyPageType>
{
    private readonly IEventRegistry _eventRegistry;
    public MyController(IEventRegistry eventRegistry)
    {
        _eventRegistry = eventRegistry;
    }

    // Everything else goes here...
}
#252376
Apr 01, 2021 11:35
Helani Wanniarachchi - Apr 01, 2021 11:56
Thank you very much. I can use the ServiceLocator.
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.