How To Create a COM+ Event Transient Subscription in Visual Basic (250292)



The information in this article applies to:

  • Microsoft COM+ 1.0

This article was previously published under Q250292

SUMMARY

This article explains how to use Microsoft Visual Basic to add a transient subscription to a COM+ Event class.

MORE INFORMATION

The COM+ Events service allows you to configure an Event Class that when called, rather than executing its methods, broadcasts the call to one or more subscribers that contains the implementation to be executed. There are two main types of subscribers: persistent and transient. Persistent subscriptions are configured through the Component Services snap-in or through the COM+ Admin objects, and the information is stored on disk. Transient subscriptions have to be added in a run-time file and are lost in case of a system failure, but they allow for added flexibility of subscriber lifetime and implementation strategy.

Following are the two steps required to add a transient subscription:
  1. Create a COM+ server (an .exe, a .dll or an .ocx file) with an object that implements the interfaces or methods you want to subscribe to.
  2. When you want to create your transient subscription, call the following code passing in the CLSID of the Event class and a reference to the subscriber you created in the previous step.
    Public Function CreateTransientSubscription(ByVal clsid As String, ByVal objref As Object) As String
    
        Dim oCOMAdminCatalog As COMAdmin.COMAdminCatalog
        Dim oTSCol As COMAdminCatalogCollection
        Dim oSubscription As ICatalogObject
        
        On Error GoTo CreateTransientSubscriptionError
    
        Set oCOMAdminCatalog = CreateObject("COMAdmin.COMAdminCatalog.1")
        'Get the TRANSIENTSUBSCRIPTIONS collection
    
        Set oTSCol = oCOMAdminCatalog.GetCollection("TransientSubscriptions")
        Set oSubscription = oTSCol.Add
    
            
        oSubscription.Value("SubscriberInterface") = objref
        oSubscription.Value("EventCLSID") = clsid
        oSubscription.Value("Name") = "TransientSubscription"
        
    
    
        oTSCol.SaveChanges
    
        CreateTransientSubscription = oSubscription.Value("ID")
    
        Set oSubscription = Nothing
        Set oTSCol = Nothing
        Set oCOMAdminCatalog = Nothing
        
    Exit Function
    
    CreateTransientSubscriptionError:
    
    
         CreateTransientSubscription = ""
         Err.Raise Err.Number, "[CreateTransientSubscription]" & Err.Source, Err.Description
    
    
    End Function
    						

    This code returns a string which is a GUID and that can be used as a handle or cookie to revoke the subscription later. (To remove the subscription just call Remove on the TransientSubscriptions collection passing in the index corresponding to the subscription with the GUID you received as a cookie).
A sample for calling this function is as follows:

  1. Create an Event class that supports the interface IStockTicker, which has one method, UpdateStock.
  2. In your subscriber project, add a class that implements the IStockTicker interface.
  3. When you want to subscribe execute the following code:
        Dim oMyTicker As Object
        Dim sSubscriptionID As String
        Set oMyTicker = CreateObject("TheProject.CMyTicker")
        sSubscriptionID = CreateTransientSubscription("{..CLSID for the Event Class..}", oMyTicker)
    					
You can get the Event class CLSID from the Component Services MMC snap-in, in the General tab for the Event Class.

REFERENCES

For a Microsoft Visual Basic .NET version of this article, see 312902.
Please read the MSDN Documentation on COM+ Events for more information.

Modification Type:MajorLast Reviewed:7/22/2005
Keywords:kbCOMPlusCatalog kbCOMPlusLCE kbhowto kbSysAdmin KB250292