Creating the Event Object

team lib

Based on our previous discussions, you know that the first piece of code you should create when creating a publish/subscribe model application is the event object. You need to use this object when creating both the publisher and subscriber. Remember that the event object is the middlemanthe go-between for the publisher and subscriber. While the publisher doesnt need to know about the subscriber, and vice versa, they both need to know about the event object. The following sections show how to design and install an event object component.

Tip 

Sometimes resolving an error requires you to look at configuration issues rather than check your code. In some cases, a developer might have two copies of the same component on a machine. If both components have the same program ID, Windows might choose the older component as part of a client request rather than the new one. Any errors in this older component will give the developer a false impression . Always make sure you unregister and remove older versions of components. Use the OLE/COM Object Viewer (available from the Visual Studio .NET Tools menu) to check for older versions of the component if you suspect an error isnt actually part of your code.

Designing the Component

The event object is the simplest part of the application. Creating the event object is more a matter of configuration than coding. Youll begin by creating a Class Library project with the name SimpleEventObject. Make sure you give your namespace and the default class a name . The example uses a namespace of SimpleEventObject and a class name of SendMsg . Listing 10-1 shows the source code for the event object. You can find the complete source code in the Chapter10\SimpleEventObject folder of the books companion content. This content is available from the books Web site at: http://www.microsoft.com/mspress/books/6426.asp .

Listing 10-1: Simple event object
start example
 namespace SimpleEventObject { ///<summary> ///AninterfaceusedtoaccesstheSendMsgfunctions. ///</summary> [Guid("0E4DB900-60F3-4354-A89A-C90A962F468C"), InterfaceType(ComInterfaceType.InterfaceIsDual)] publicinterfaceISendMsg { voidFireBroadcastMsg(StringstrMsg); } ///<summary> ///Thisclassshowshowtocreateaverysimpleevent ///objectusedtocreateabridgebetweenapublisher ///andasubscriber. ///</summary> [Guid("6A121716-471A-479f-B1DE-97858FE95483"), ClassInterface(ClassInterfaceType.None)] publicclassSendMsg:ServicedComponent,ISendMsg { publicSendMsg() { } ///<summary> ///Thismethodactsasatemplatefortheactual ///eventobject. ///</summary> ///<paramname="strMsg">Thedatawewanttosend ///asabroadcastmessage.</param> publicvoidFireBroadcastMsg(StringstrMsg) { } } } 
end example
 

The code in Listing 10-1 looks shortperhaps too short and too easy. If youre thinking theres an error in the book, youre wrong. All you need to provide for an event object is the prototype of each event you plan to support. COM+ will take care of the implementation of each event, so coding is extremely simple in this case. Notice that the FireBroadcastMsg() method has no implementationthe implementation is actually part of the subscriber code. The event object only provides a prototype. Make sure you compile and register the event object component before you leave this section because youll need to reference it in other areas of this example.

Installing the Event Object

In Chapter 2, we discussed the method for creating a COM+ application. You use the same process as normal to create the event object application. I named the example SimpleEventApp. Installing the component, however, is different from the process we discussed in Chapter 2. The following steps will show you how to install an event component:

  1. In Component Services, create a new COM+ application named SimpleEventApp.

  2. Right click the SimpleEventApp\Components folder, and choose New and then Component from the context menu. Youll see a Welcome To The COM Component Install Wizard dialog box.

  3. Click Next. Youll see the Import Or Install A Component page shown in Figure 10-2. Notice the Install New Event Class(es) button near the bottom of this dialog box.

    click to expand
    Figure 10-2: The Import Or Install A Component dialog box allows you to install a number of component types.

  4. Click Install New Event Class(es). Youll see a Select Files To Install dialog box.

  5. Locate the event object type library you want to install (SimpleEventObject.TLB for this example), highlight it, and then click Open. Youll see an Install New Components page like the one shown in Figure 10-3.

    click to expand
    Figure 10-3: The Install New Components page will allow you to check the event object you want to install.

  6. Verify the dynamic-link library (DLL) you want to install, and then click Next. Youll see a success page.

  7. Click Finish.

The interesting thing about this installation is the result doesnt look much different than the installation we performed in Chapter 2. However, youll learn later that installing the component as an event class makes a significant difference in how COM+ views the component. Once you create the application on the server, export it as you normally would and install it on the client as a proxy.

 
team lib


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

Similar book on Amazon

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