Creating a Component Subscriber

team lib

The easiest type of subscriber to create for this example is a component. Using a component lets you place the subscriber on the server for immediate response. The component will output a message box in this case, which is definitely the incorrect thing to do because no one is usually at the server to see the message box. The example uses this simple approach so that you can obtain instant feedback on the usability of the subscriber. The following sections show you how to design, install, and test this type of subscriber.

Designing the Subscriber Component

Remember that the publisher is firing the event and that the event object manages the event. So far, we dont have anything that actually implements the event. How an application handles an event, even under COM+, is unique for every application. When a user clicks a button on a form, the event handler for that button performs some actionthe button component doesnt care what that action is or even that the code has handled it. The same holds true for subscribers. Each subscriber is unique and will handle the event in a unique way. Listing 10-3 shows the code for this subscriber. Youll find the complete listing in the Chapter 10\SubscriberComponent folder of the source code.

Listing 10-3: Component style subscriber
start example
 namespaceSubscriberComponent { ///<summary> ///Thisclasssubscribestotheeventobjectthatholds ///themessagecreatedbythepublisher. ///</summary> [Guid("3162ED10-C74D-4967-BA51-C8EBD10A8D03"), ClassInterface(ClassInterfaceType.None)] publicclassSendMsg:ServicedComponent,ISendMsg { publicSendMsg() { } ///<summary> ///Thismethodactsasatemplatefortheactual ///eventobject. ///</summary> ///<paramname="strMsg">Thedatawewanttosend ///asabroadcastmessage.</param> publicvoidFireBroadcastMsg(StringstrMsg) { MessageBox.Show(strMsg,  "PublisherMessage", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } 
end example
 

You should notice something almost immediately about the subscriberit contains no interface description. When you look at the project, youll notice a reference to the event object, SimpleEventObject . The event object provides the interface in this case. Using the interface in this way ensures that the subscriber matches the event object so that the two are compatible.

Note that the namespace for this subscriber is different than the event object. This isnt required, but using a different namespace does make it easier to identify the subscriber. The use of a different GUID for the SendMsg class is required. The event object class is different than the subscriber class. In fact, you dont have to use the same names the only requirement is that the two support the same interface.

The FireBroadcastMsg() method contains a simple implementation. The only thing that happens, in this case, is that the event handler displays a message box containing the string passed from the publisher. Obviously, you could set this up to do anything that any other event handler would do.

Installing and Testing the Subscriber Component

Youll create an application and register the subscriber just as you always do when working with COM+. Chapter 2 contains complete instructions for this process. The example uses an application name of SimpleSubscriber. However, when working with a subscriber component, you must create the subscription. The following steps show how:

  1. In Component Services, create a COM+ component named SimpleSubscriber and install the SubscriberComponent.

  2. Right click the SimpleSubscriber\ Components \SubscriberComponent.SndMessage\Subscriptions folder, and choose New and then Subscription from the context menu. Youll see a Welcome To The COM+ New Subscription Wizard dialog box.

  3. Click Next. Youll see a Select Subscription Method(s) page like the one shown in Figure 10-5. This is where you choose the methods that you want to use to accept events from a publisher. Notice that all the interfaces and methods the component supports appear in the list. This differs from unmanaged components, where you often see just the interface supported by the component itself.

    click to expand
    Figure 10-5: The first step is to select the methods you want to receive events.

  4. Select just the FireBroadcastMsg() method, as shown in Figure 10-5, and then click Next. Windows will search the COM+ catalog for components that publish events that might fulfill your components needs. Once this search process is complete, youll see a Select Event Class page like the one shown in Figure 10-6. Notice that the event object we created appears in this dialog box. If you dont see the event object, you need to stop the procedure now and check the previous steps to create the SimpleEventApp application listed earlier in this chapter. Make sure you see the correct event object listed here; otherwise , the example wont work (and neither will your application in real life).

    click to expand
    Figure 10-6: Make sure the event object appears in the dialog.

  5. Select the SimpleEventObject.SendMsg class, and then click Next. Youll see the Subscription Options page shown in Figure 10-7. This is where youll provide a name for your subscription and enable it. Always enable the subscription unless you dont want to receive events right away.

    click to expand
    Figure 10-7: Always enable the subscription as part of the configuration process.

  6. Type a subscription name (the example uses My Subscription), select the Enable This Subscription Immediately option, and then click Next. Youll see a success message.

  7. Click Finish. At this point, you should see a new subscription added to the Subscriptions folder. Figure 10-8 shows a typical example of a subscription.

    click to expand
    Figure 10-8: The subscription should appear in the Subscription folder once you create it successfully.

Youll need to export the application from the server and install it as a proxy application on the client as normal. At this point, everything is in place to test the first publish/subscribe setup. Start the Publisher application, type a message, and then click Publish. After a few seconds, the server should display a message box containing the text that you typed in the publisher. If you dont see the message box, make sure you enabled the subscriber. Also, verify that both the event object and the subscriber applications are active.

 
team lib


COM Programming with Microsoft .NET
COM Programming with Microsoft .NET
ISBN: 0735618755
EAN: 2147483647
Year: 2006
Pages: 140

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net