|
Introduction
Everyone knows it's a hassle to register the COM component in COM+
applications when the COM component is changed. The steps typically
involve shutting down the COM+ application, deleting the existing
components (refresh does not work if the binary compatibility
changes), overwriting the existing DLL with the new DLL, and finally
registering the DLL in the COM+ application. This means that every
developer must be a part-time administrator. Since administration is
far less exciting than writing the actual code for your components,
this article shows you how to automate this process.
COMAdmin Library
COM+ Automation is possible because of a set of system-supplied
components known as the Component Services Administration (COMAdmin)
Library. This article will show how to create a COM component, which
does the trick, and show how to call it from an ASP page.
To program using COMAdmin components from Visual Basic set the
project reference to "Com+ 1.0 Admin Type Library." If you look at
the COMAdmin type library using the object browser, you will see that
it contains only three objects, the COMAdminCatalog, the
COMAdminCatalogCollection, and the COMAdminCatalogObject. The
COMAdminCatalog component represents the COM+ Catalog Manager, while
the other two components represent every object in the hierarchy.
The COMAdmin object model contains quite few logical collections,
such as Applications, Components, and Roles. However, the object
model is a bit tricky when you first start programming because all of
these collections map to the COMAdminCatalogCollection component.
Similarly, there are several logical object types such as
Application, Component, Role, and User that all map to the
COMAdminCatalogObject component.
COM+ Administration
Let's see how we can we use this object to program. First, assume
that the COM+ application is already created.
You must begin any administrative session by establishing a
connection to a Catalog manager on the local computer.
Dim objCatalog As COMAdminCatalog
Set objCatalog = New COMAdminCatalog
If you want to administer a remote computer, then you can establish a
connection on the remote computer using
objCatalog.Connect "RemoteServer"
After creating the Catalog Manager object, our first task is to shut
down the application, which can be achieved by
objCatalog.ShutDownApplication "COM+ Application Name"
Once the application is shut down, we have to remove all the
components in that application. Removing the components is tricky.
For that we have to use the COMAdminCatalogCollection object.
Remember, this object is used to store the Application collection as
well as the Component collection.
In order to get the current Application object we have to get all the
COM+ applications in the server into the COMAdminCatalogCollection
object and iterate through them to the application we want. Obtaining
all the applications is achieved by
Dim objCatalog As COMAdminCatalog
Dim objCatalogApplicationCollection As COMAdminCatalogCollection
Set objCatalog = New COMAdminCatalog
'Getting all the Applications in the server
Set objCatalogApplicationCollection =
objCatalog.GetCollection("Applications")
objCatalogApplicationCollection.Populate
The Populate method must be used to actually retrieve all the
application objects from RegDB. Once we have all the Application
objects we have to iterate through each object to the application in
which we have to remove all the components. (There is no other way to
get the Application object directly from Visual Basic.)
Securing the Current Application Object. (Assume AppName contains the
name of the current application.)
For iCounter = 0 To objCatalogApplicationCollection.Count - 1
If objCatalogApplicationCollection.Item(iCounter).Name = AppName Then
' application is found
'remove components code goes here
End If
Next
After finding the current application, we have to use the same logic
to get all the components in that application and we have to delete
them.
Set objCatalogComponentCollection =
objCatalogApplicationCollection.GetCollection _
("Components", objCatalogApplicationCollection.Item(iCounter).Key)
objCatalogComponentCollection.Populate
Once the Component collection is populated, we then iterate though
the collection and delete them.
For iComponentCounter = 0 To
objCatalogComponentCollection.Count - 1
'remove the component, which is always the top one
objCatalogComponentCollection.Remove 0
Next
When all the components are deleted, the collection should be saved.
This can be done using
objCatalogComponentCollection.SaveChanges
The complete code for removing the Components in an application is below:
Public Function RemoveComponent(ByVal AppName As String) As Boolean
'removes the component installed in the Application
On Error GoTo Err_RemoveComponent
Dim objCatalog As COMAdminCatalog
Dim objCatalogApplicationCollection As COMAdminCatalogCollection
Dim objCatalogComponentCollection As COMAdminCatalogCollection
Dim iCounter As Integer
Dim iComponentCounter As Integer
Dim bRemoved As Boolean
Set objCatalog = New COMAdminCatalog
'getting all the Applications in the server
Set objCatalogApplicationCollection =
objCatalog.GetCollection("Applications")
objCatalogApplicationCollection.Populate
'looping through all the Applications to get the current Application
bRemoved = False
For iCounter = 0 To objCatalogApplicationCollection.Count - 1
'Debug.Print objCatalogApplicationCollection.Item(iCounter).Name
If objCatalogApplicationCollection.Item(iCounter).Name =
AppName Then ' if application is found
'Shutdown the application first
objCatalog.ShutDownApplication AppName
'getting the current collection
Set objCatalogComponentCollection =
objCatalogApplicationCollection.GetCollection _
("Components",
objCatalogApplicationCollection.Item(iCounter).Key)
objCatalogComponentCollection.Populate
'check if the remove option is enabled
If objCatalogComponentCollection.RemoveEnabled Then
For iComponentCounter = 0 To
objCatalogComponentCollection.Count - 1
'remove the component, which is always the top one
objCatalogComponentCollection.Remove 0
Next
Else
'raise an error
Err.Raise vbObjectError, "Method:RemoveComponent", _
"RemoveEnabled property returned false"
End If
'saves changes
objCatalogComponentCollection.SaveChanges
bRemoved = True
Exit For
End If
Next
RemoveComponent = bRemoved
Exit Function
Err_RemoveComponent:
RemoveComponent = False
Err.Raise Err.Number, Err.Source, Err.Description
End Function
Once all the components are removed we have to install the
components. This is easy and straightforward. The code that does the
installation is:
objCatalog.InstallComponent "COM+ Application Name", DLLPath, "", ""
Putting It All Together
Since we've seen the code that does the COM+ administration, let's
look at the user interface. First, create an Active X DLL called
COMPlusAdmin, and it needs a class in it called Admin where I place
all the administration code. I quickly create a Web interface (see
figure below).
I used ASPUpload (http://www.aspupload.com) to
upload the DLL from the developer machine to the server. I created
another class in the COMPlusAdmin DLL project called Register that
has only one method, RegisterCOMPlus. Below demonstrates how this
method works.
Public Function RegisterCOMPlus(ByVal AppName As String, ByVal
DLLPath As String, _
ByVal CurrentPath As
String, ByVal User As String) As Boolean
'calls the admin functions in a sequence
On Error GoTo Err_RegisterCOMPlus
Dim sMessage As String
'creating the ObjectContext object and Response Object
Dim objCxt As ObjectContext
Dim objResponse As Response
Set objCxt = GetObjectContext
Set objResponse = objCxt.Item("Response")
'creating a reference to the Admin class
Dim objCOMAdmin As ComPlusAdmin.Admin
Set objCOMAdmin = New ComPlusAdmin.Admin
'checking if the Application exists
If objCOMAdmin.IsApplicationExists(AppName) Then
'send a message before replacing
objResponse.Write "Sending out a replacing message to all the
connected users" & "<br>"
objResponse.Flush
sMessage = "Replacing the application '" & AppName & "' -- " & User
SendNetMessage sMessage
'shut down the application
objResponse.Write "Shutting down the application
""<b>" & AppName & "</b>""<br>"
objResponse.Flush
objCOMAdmin.ShutDownApplication AppName
objResponse.Write "Removing the components in the application
""<b>" & _
AppName &
"</b>""<br>"
objResponse.Flush
'remove the components in the current application
objCOMAdmin.RemoveComponent AppName
'copying the DLL from the Current Path to its path
'if file exists delete the file
objResponse.Write "Overwriting the DLL with the uploaded
DLL<br>"
objResponse.Flush
If Dir(DLLPath) <> "" Then
Kill DLLPath
End If
FileCopy CurrentPath, DLLPath
'install the component
objResponse.Write "Installing the components in to the
application ""<b>" & _
AppName &
"</b>""<br>"
objResponse.Flush
objCOMAdmin.InstallComponent AppName, DLLPath
'sending the completing message
objResponse.Write "Sending out a Completion message to all
the connected users" & "<br>"
objResponse.Flush
sMessage = "Successfully registered the application '" &
AppName & "' -- " & User
SendNetMessage sMessage
Else
Err.Raise vbObjectError, "Method:RegisterCOMPlus",
"Application does not exist"
End If
RegisterCOMPlus = True
Exit Function
Err_RegisterCOMPlus:
RegisterCOMPlus = False
Err.Raise Err.Number, Err.Source, Err.Description
End Function
This method takes four input parameters, as follows:
AppName - Name of the COM+ application
DLLPath - The physical path where the DLL resides and registers into COM+
CurrentPath - The current path of the DLL to be registered. This is
important because you can only rewrite the DLL after the application
is shut down.
User - Name of the user who is replacing the COM+ application, which
is used to send messages
I also used ASP objects in this method to send status to the user.
This is an outline of how the method works. This method checks
whether the application exists by the isApplicationExists method in
the Admin object. If the application exists, then Shut Down the
application and remove all the Components in that application.
Overwrite the existing DLL, and finally, install the DLL. This method
is called from the ASP page, which does the registration work.
Conclusion
This article has covered the basic and day-to-day COM+
administration. The attached code contains some other COM+
administration modules like creating and deleting an application,
which are not discussed in this article. Further, you can extend the
functionality of this COM+ administration by adding Roles,
Attributes, and Events, etc. For further reference see
http://msdn.microsoft.com/library/periodic/period00/instincts0900.htm.
About the Author
Prasanna Pattam is a senior architect at Qwest Communications
International, Inc., and has been working in the ASP technologies for
the last four years. He can be reached at Prasanna.Pattam@qwest.com.
|