Shared Properties


Even though statelessness is a common goal and often a requirement for many enterprise applications, especially those making use of serviced components, sometimes state needs to be maintained.

A COM+ application can have its own state in the form of shared property groups and shared properties. This state is scoped for the entire COM+ application. This means that if the COM+ application is library-activated, the state is available to any code within the process that started the application. If the COM+ application is server-activated, the state is available to any client. In fact, in a server-activated COM+ application, clients can actually shut down and the state will be waiting for them when they start back up again (provided the server application hasn't restarted).

To see shared properties in action, this next sample will illustrate using shared properties to store the last assigned order number. If COM+ can maintain information like this in-memory, shared, and available to all COM+ components within an application, that can be a tremendous savings of time and resources by not requiring the components to make a connection to the database just to receive the last assigned order number.

You have already seen how to create and register these components, so that information won't be repeated here. Basically all you need to do is create a class library that has the following Enterprise Services attributes:

[assembly: ApplicationName("Shared Property Demonstration")] [assembly: ApplicationActivation(ActivationOption.Server)] 


Then, add a class called SharedPropComponent to the library and enter the code shown in Listing 40.2.

Listing 40.2. A ServicedComponent That Makes Use of Shared Properties

using System; using System.Collections.Generic; using System.Text; using System.EnterpriseServices; namespace SharedPropLib { public class SharedPropComponent : ServicedComponent { public SharedPropComponent() { } public int GetNextOrderNumber() {     bool groupExists;     bool propExists;     int lastOrderNumber;     PropertyLockMode propLock = PropertyLockMode.SetGet;     PropertyReleaseMode propRelease = PropertyReleaseMode.Standard;     SharedPropertyGroupManager spgm = new SharedPropertyGroupManager();     SharedPropertyGroup spGroup = spgm.CreatePropertyGroup("Orders", ref propLock,         ref propRelease, out groupExists);     SharedProperty orderNumber = spGroup.CreateProperty("OrderNumber", out propExists);     if (!propExists)         orderNumber.Value = 1;     lastOrderNumber = (int)orderNumber.Value;     orderNumber.Value = lastOrderNumber + 1;     return lastOrderNumber; } } } 

This code uses a property group called "Orders", and a property within that group called "OrderNumber". Remember that all of the code that accesses property groups is scoped to the current COM+ application, so you don't need to waste an entire group segregating your application from other COM+ applications manually. After registering the application manually using regsvcs.exe (because server-activated applications can't be registered automatically), use the following code as a test harness for the new component:

using System; using System.Collections.Generic; using System.Text; using SharedPropLib; namespace SharedPropertyDemo { class Program {     static void Main(string[] args)     {         SharedPropComponent sProp = new SharedPropComponent();         // obtain a couple order numbers         for (int x = 0; x < 10; x++)         {             Console.WriteLine(sProp.GetNextOrderNumber().ToString());         }         Console.ReadLine();     } } } 


What you should expect to see is that the first time you run the application it will count from 1 to 10.

Tip

You might need to disable authentication checks on the application before this will work. Various development environments are often not configured to allow the developer's account to do things such as work with shared property groups. To disable security and authentication checks, use the Properties dialog for the COM+ application itself, not the component.


What's more interesting is that if you shut down the test harness application and then start it back up again, it will count from 11 to 20. The timeout is configurable; you can set the timeout period or leave the server application running indefinitely. If the COM+ server application has not shut down due to idle timeout, all of the information contained within the shared property groups and shared properties will remain intact, even though there isn't a single client consuming a component within the application.

Shared properties can be used for all kinds of things. If your application wants to maintain relatively transient state information but doesn't want to incur the overhead of using a database to do so, the COM+ shared property system is definitely the technology to use.



Microsoft Visual C# 2005 Unleashed
Microsoft Visual C# 2005 Unleashed
ISBN: 0672327767
EAN: 2147483647
Year: 2004
Pages: 298

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