Object Construction Sample

Object Construction Sample

    (Windows Forms Application)

This document contains the release information for the .NET Framework Object Construction sample. You will find the following sections below:

Overview

This sample demonstrates the use of Object contructor strings in a .Net environment. Object Constructor strings are initialization strings that can be specified or edited administratively (through the Component Services administrative tool), eliminating the need to hard-code configuration information within components.

When object construction is enabled for a component, COM+ will call the IObjectContruct::Construct method that it implements immediately after instantiating the object. The object construction string specified in the component's activation properties is passed as an argument to the method. This, in combination with component code that uses the object string in a meaningful way, effectively provides parameterized object construction.

You can specify a default string value for the construction string in the Default property of the ConstructionEnabledAttribute, in the COM+ catalog, or both.

A component developer might use this feature, for example, to write a component that holds a generic ODBC connection. The object construction string would hold an exact DSN that could be changed through the Component Services admin tool.

Object Contruction strings can be useful in combination with COM+ objection pooling services. See the Platform SDK help for more information.

The .NET Object Constructor sample creates a COM+ component with object construction enabled and a default construction string. On creating an object instance, various component methods display message boxes to demonstrate call order. The user can modify the object construction string by means of the Component Services admin tool and re-run the sample to observe that the object construction string passed to the component instance has changed.

Topics

The following bullets briefly describe the technologies used by this sample.

Location

This sample is located in the Technologies\ComponentServices\ObjectConstruction subdirectory of the .NET Framework SDK samples directory.

For Example:

C:\Program Files\Microsoft.NET\SDK\v1.1\Samples\Technologies\ComponentServices\ObjectConstruction

Building the Sample

In order to properly build and run this sample, the following steps need to be performed.

Type BuildAll.bat from the command line.

For Example:

C:\Program Files\Microsoft.NET\SDK\v1.1\Samples\Technologies\ComponentServices\ObjectConstruction>buildall.bat

If you're building the sample from Visual Studio 7, you will need to install the OCDemoSvr.dll assembly to the Global Assembly Cache before running the sample. Note: The gacutil.exe program is located in the SDK\v1.1\Bin directory.

For Example:

C:\Program Files\Microsoft.NET\SDK\v1.1\Samples\Technologies\ComponentServices\ObjectConstruction\CS>gacutil -i OCDemoSvr.dll

The sample builds a C# and VB version of the client application and COM+ component. Source code for each language is stored in subdirectories named \CS and \VB. The C# component has 'CS' appended to its name as viewed in the Component Services administrative tool, 'OCDemoServerCS.ObjectConstructionTest'. The VB version is named 'OCDemoServerVB.ObjectConstructionTest'. When working with the sample code, be sure to observe and/or modify the version of the COM+ component that corresponds to the client application you launch.

Review comments in the source code and build.bat files for more information on sample binary creation and registration steps.

Running the Sample

In order to run this sample, follow the instructions listed below.

  • NOTE: The COM+ .NET samples require Windows 2000 or higher in order to build and run.
  • Build the Object Construction sample code following the instructions above.
  • Type OCDemo.exe from the command line. Note whether you are executing the C# (located in the \CS subdirectory) or VB (located in \VB) version of the sample application, as this will determine which COM+ component attributes you will edit administratively in a subsequent step.

    For Example:

    C:\Program Files\Microsoft.NET\SDK\v1.1\Samples\Technologies\ComponentServices\ObjectConstruction\CS>ocdemo.exe

  • Press the button labeled 'Create Object'.
  • A message box will appear at each stage of activation. Note the object construction string value reported in the call to IObjectConstruct::Construct.
  • Launch the Component Services administrative tool (also called the 'COM+ explorer'), by clicking 'Start\Settings\Control Panel' then double-clicking 'Administrative Tools\Component Services'.
  • Locate the OCDemoSvr COM+ application by expanding Component Services\Computers\My Computer\COM+ Applications in the left-hand COM+ explorer pane.
  • Locate the OCDemoServerCS.ObjectConstructionTest or OCDemoServerVB.ObjectConstructionTest component by expanding 'OCDemoSvr\Components'
  • Right-Click the 'OCDemoServerCS.ObjectConstructionTest' or 'OCDemoServerVB.ObjectConstructionTest' component and select 'Properties' from the popup menu.
  • Click the 'Activation' tab and enter a new text value in the 'Constructor string' edit field. Click 'OK' to accept the change and dismiss the properties dialog.
  • Return to the OCDemo application and click 'Create Object' again. Note the new value which is reported by the IObjectConstruct method (it should match the newly edited string from the COM+ explorer component properties page.)

Uninstalling the Sample

In order to uninstall this sample, follow the instructions listed below.

  • To uninstall this sample, run buildall.bat -u. or build.bat -u. The batch file will then call regsvcs and gacutil with the /u switch in order to remove the sample files from the COM+ catalog and Global Assembly Cache.

Notes

For the .Net Framework samples to function properly, your Path, Include, and Lib environment variables must be set correctly. In many cases, the SDK install program updates these environment variables for you. However, if you installed the .Net Framework SDK using Visual Studio .NET, or unselected "register environment variables" during setup, these environment variables will not be updated. You can set the Path, Include and Lib environment variables by running SDKVars.bat, located in the <SDKRoot>\Bin directory. SDKVars.bat must be executed in every command shell.