How to: Publish Events

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies.
This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
To create client business applications using current Microsoft technologies, see patterns & practices' Prism.

You use the Add Event Publication recipe to publish an event from a Composite UI Application Block module or the Shell project. This recipe defines an event topic identifier and adds code to an existing C# class to publish the event.

You must have an existing smart client solution.

You execute the Add Event Publication recipe on an existing C# class.

To add an event publication

  1. Using Visual Studio, open an existing smart client solution.
  2. In Solution Explorer, right-click a C# class, point to Smart Client Factory, and then click Add Event Publication, as shown in Figure 1.


    Figure 1

    Add Event Publication recipe menu item

The recipe displays a wizard page that prompts you to enter the following information:

  • The event topic name. The recipe will create a constant definition for the name you enter here. The recipe creates the constant in the class EventTopicNames in the Infrastructure.Interface class library project.
  • The publication scope. You can choose Global, WorkItem, or Descendants. The default is Global.
    Note: If you create an event topic with a publication scope of WorkItem or Descendants, you can choose to define the event topic name in the module that publishes that event. To do this, move the constant definition from the EventTopicNames class in the Infrastructure.Interface project to the EventTopicNames class in the module's project. When you do this, the event topic will not be visible to all projects that reference the Infrastructure.Interface project.

  • The event argument type. The default is System.EventArgs. The event broker system uses .NET Framework events and delegates in the underlying code. You can use the arguments within the event to pass information between the publisher and subscriber.
  • Ff699217.172958ea-da09-4bfa-b497-5a4bae951988(en-us,PandP.10).png

    Figure 2

    Add Event Publication Wizard
Note: You should define unique event topic names when you create an event with this recipe. It is possible to use this recipe to define the same event topic name in different modules. However, this is not recommended. The event broker uses only the event topic name to distinguish events.

The recipe will modify the following items in your solution.

  • The EventTopicNames class in the Infrastructure.Interface project. The recipe will modify this file to create a new constant definition for the event topic name.
  • The C# class that is selected when you launch the recipe. The recipe will add an event definition with an EventPublication attribute and a method that raises the event. The following code is an example of the recipe output for the event topic UpdatesAvailable.
    [EventPublication(EventTopicNames.UpdatesAvailable, PublicationScope.Global)]
    public event System.EventHandler<System.EventArgs> UpdatesAvailable;

    The following is an example of the recipe out for the method that raises the event.

    protected virtual void OnUpdatesAvailable(System.EventArgs eventArgs)
      if (UpdatesAvailable != null)
        UpdatesAvailable(this, eventArgs);

The following are typical tasks that you perform after you run the Add Event Publication recipe:

  • Write your event publication business logic. Call the method generated by the recipe to raise the event.
  • Write event handlers. To do this, use the Add Event Subscription recipe. For information about how to run it, see How to: Subscribe to Events.