A ServicedComponent Class Example

team lib

A ServicedComponent Class Example

Components based on the ServicedComponent class are the most flexible method of working with COM+. Using this component strategy usually means a little more coding on your part. In addition, youll find the resulting component is larger and uses more resources. However, if you want to interact with COM+ fully, this is the only way to go because this method also provides you full access to all COM+ functionality. The vast majority of the COM+ programming examples in this book subclass the ServicedComponent class.

This example is the first one in the book to show you the power of the EnterpriseServices namespace. This namespace is extremely important to the COM+ application developer because it provides access to most of the COM+ application properties. For example, youll use the [ApplicationName] attribute included with this namespace to assign a name to your application.

One attribute youll normally include with your application is the [ApplicationAccessControl] attribute. This attribute determines whether the COM+ application uses access control checks. If you dont tell the RegSvcs utility that you want to use access control checks, it will assume you do and set them. Heres an example of the [ApplicationAccessControl] attribute as used in this example to make access easier:

 [assembly:ApplicationAccessControl(false)] 

In this case, the sample component will perform a simple task that demonstrates a powerful COM+ application featurethe constructor string. The developer can use constructor strings within an application to help the administrator configure a COM+ application. For example, you might use the constructor string to indicate the location of a database on the network or allow specialized configuration when the administrator doesnt want to use all the features a component can provide. Another use of the constructor string is registration information. Some companies use this field to hold the product registration number, although there are simpler methods of performing this particular task (such as using the registry to hold the information).

Deriving from the ServicedComponent Class

When you derive a component from the ServicedComponent class, you need to add the additional attributes mentioned in the introduction to this section. Most of the principles for creating the code are the same. Youll still create an interface that describes the class methods, and youll still use the various attributes weve discussed in the past such as the [Guid] attribute. However, there are subtle differences, as you can see in Listing 8-5:

Listing 8-5: Obtaining the constructor string
start example
 [Guid("BB924921-68D6-4b14-8771-878EDCFEC8B2"), InterfaceType(ComInterfaceType.InterfaceIsDual)] publicinterfaceICheckString { stringGetConstructorString(); } [Guid("93707E2D-7672-4d3f-A8F3-4FB934DB9BE1"), ClassInterface(ClassInterfaceType.None), ConstructionEnabled(Default="Helloworld")] publicclassCheckString:ServicedComponent,ICheckString { privatestring_ConstructorValue; publicCheckString() { // //TODO:Addconstructorlogichere // } protectedoverridevoidConstruct(stringconstructString) { //Savethevalueoftheconstructorstringina //privatevariable. _ConstructorValue=constructString; } publicstringGetConstructorString() { //Returnthecurrentvalueoftheconstructorstring. return_ConstructorValue; } } 
end example
 

The first thing you should notice is the addition of the [ConstructionEnabled] attribute to this example. This attribute tells COM+ to check the Enable object construction option and give this entry the default value specified by the attribute constructor (Hello world for this example). Also notice that the class itself subclasses ServicedComponent and implements the ICheckString interface.

The code defines a private variable named _ConstructorValue . This variable holds the value of the constructor string. When working with constructor strings, you must override the Construct() method, as shown in the listing. In some places in the current documentation, it shows the Construct() method as publicthe actual implementation is protected as shown. Normally, youd place code for creating the object in the Construct() method. In this case, we simply save the string to the private _ConstructorValue variable. The GetConstructorString() method returns this string when the client application requests it.

Performing the ServicedComponent Class Setup

You can set up this component by using either the GUI or RegSvcs utility methods described earlier in the chapter. If you use the GUI method, youll obtain the benefits of using COM+ 1.5 under Windows XP. On the other hand, using RegSvcs will create a COM+ 1.0 application. However, using RegSvcs also means that all attributes you define for the component are automatically checked and added to the COM+ application. Figure 8-6 shows the results of using the RegSvcs utilitythe results of using the GUI method are the same. The only difference is that the RegSvcs utility application creates a library application (which you can manually change to a server application).

click to expand
Figure 8-6: Components based on the ServicedComponent class show a minimum of six interfaces.

Depending on the method you use to create the COM+ application, youll want to check and optionally modify the object construction string value for this application. Figure 8-7 shows the Constructor String value that the RegSvcs utility added automatically to the Activation tab of the ConstructString.CheckString Properties dialog box. You can also set this value manually when using the GUI method.

click to expand
Figure 8-7: You should define a constructor string for this class to see the functionality this feature can provide.

Creating the Client

The client for this example is very simple. All we need to do is create the object and query it for the constructor string value, as shown in Listing 8-6. Youll find this sample in the Chapter08\ConstructStringTest folder in the books companion content. Notice that in this case all additional code appears in the component. The client code remains simple.

Listing 8-6: Constructor string
start example
 voidCConstructStringTestDlg::OnBnClickedTest() { ICheckString*pConstString;//PointertoConstStringobject. _bstr_tOutput;//Temporarydatastring. //InitializetheCOMenvironment. CoInitialize(NULL); //Createtheobject. CoCreateInstance(CLSID_CheckString, NULL, CLSCTX_ALL, IID_ICheckString, (void**)&pConstString); //Performtheoperation. Output=pConstString->GetConstructorString(); //Displaytheresult. m_Output.SetWindowText(Output); //UninitializetheCOMenvironment. CoUninitialize(); } 
end example
 

When you run this application the first time, youll see the Hello world output shown in Figure 8-8, unless you use some other value for the Constructor String field. In fact, youll want to test this field by modifying it with other values. The interesting element is that you dont need to stop and start the application to see a change in the constructor string value. The value will change with each request to reflect the actual value of the Constructor String field.


Figure 8-8: This example returns the COM+ application constructor string.
 
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