Services Without Components


Starting with Windows Server 2003 and Windows XP (with Windows XP you either need SP2 or SP1 with the Windows XP hotfix 828741) you can create a COM+ context without a component configuration. This makes it easy to make use of transaction and synchronization services without deriving from the base class ServicedComponent and without the need to configure the assembly. The context can be created within a method, there's no need to define context requirements with a class because different methods can have different context requirements, and the class can have any base class.

The heart of Services without Components are the ServiceConfig and the ServiceDomain classes. The ServiceConfig class is used to configure the context that is required for the actions that should be done. The ServiceDomain class is used to create a context that is described with the ServiceConfig class.

The following table lists some of the properties of the ServiceConfig class.

ServiceConfig Property

Description

Inheritance

With the Inheritance property you can define if the newly created context should be based on the existing context, or if a completely new context should be created. By default the new context is based on the existing context with the enu- meration value InheritanceOption.Inherit. If the Inheritance property is set to InheritanceOption. Ignore, the existing context is not used.

Transaction

With the Transaction property you can define the transactional requirements with the TransactionOption enumeration.

TransactionDescription

With the TransactionDescription property you can set a descriptive string for the transaction.

TransactionTimeout

With the TransactionTimout property you can configure how long a transaction may last before a timeout occurs.

IsolationLevel

With the IsolationLevel property you can set a value from the enumeration TransactionIsolationLevel.

By using the ServiceDomain and ServiceConfig classes the previously created OrderControl class can be changed as shown in the following code. The class OrderControl no longer derives from the base class ServicedComponent, and there are no attributes defined with the class. Instead, within the method NewOrder() the requirements of the context are defined with the ServiceConfig class. Here the requirements are set that a new transaction is required by setting the Transaction property to TransactionOption.RequiresNew. Next, ServiceDomain.Enter() creates a new context that includes a transaction. You can read the transaction and context ids by reading the values with ContextUtil.TransactionId and ContextUtil.ContextId. The transactional outcome is defined with the help of the class ContextUtil, as was already shown with serviced components. ServiceDomain.Leave exits the context:

 public class OrderControl { public void NewOrder(Order order) { ServiceConfig serviceConfig = new ServiceConfig(); serviceConfig.Transaction = TransactionOption.RequiresNew; ServiceDomain.Enter(serviceConfig); try { OrderData data = new OrderData(); data.Insert(order); ContextUtil.SetComplete(); } catch (Exception) { ContextUtil.SetAbort(); throw; } finally { ServiceDomain.Leave(); } } } 

The class OrderData from before is also changed, so that it is uses Services without Components. The database connection string can no longer be read from the construction string, because the Construct method is available only with serviced components. Instead, the construction string is read from an application configuration file:

 public class OrderData : IOrderUpdate { private string connectionString = null; public OrderData() { this.connectionString = ConfigurationSettings.AppSettings["NorthwindConnection"]; } 

The Insert() method of the class OrderData shows that the transactional outcome can not only be defined with the ContextUtil class; instead, the [AutoComplete] attribute is used. This is similar to serviced components. When an exception occurs, the consistent bit is set to false; without an exception the consistent bit is set to true.

Within the Insert() method again a context is created with ServiceDomain.Enter(). Here the transactional requirements are set to TransactionOption.Required with the ServiceConfig class. Because the method Insert() is invoked from the method NewOrder() where a transaction already existed, here the same transaction is used:

 [AutoComplete()] public void Insert(Order order) { ServiceConfig serviceConfig = new ServiceConfig(); serviceConfig.Transaction = TransactionOption.Required; serviceConfig.TransactionDescription = "Demo Transaction"; ServiceDomain.Enter(serviceConfig);    SqlConnection connection = new SqlConnection(connectionString);    try    {       SqlCommand command = connection.CreateCommand();       command.CommandText = "INSERT INTO Orders (CustomerId, OrderDate, " +              "ShipAddress, ShipCity, ShipCountry)" +              "VALUES(@CustomerId, @OrderDate, @ShipAddress, @ShipCity, " +              "@ShipCountry)";       command.Parameters.AddWithValue("@CustomerId", order.CustomerId);       command.Parameters.AddWithValue("@OrderDate", order.OrderDate);       command.Parameters.AddWithValue("@ShipAddress", order.ShipAddress);       command.Parameters.AddWithValue("@ShipCity", order.ShipCity);          command.Parameters.AddWithValue("@ShipCountry", order.ShipCountry);          connection.Open();          command.ExecuteNonQuery();          command.CommandText = "SELECT @@IDENTITY AS 'Identity'";          object identity = command.ExecuteScalar();          order.SetOrderId(Convert.ToInt32(identity));          OrderLineData updateOrderLine = new OrderLineData();          foreach (OrderLine orderLine in order.OrderLines)          {             updateOrderLine.Insert(order.OrderId, orderLine);          }       }       catch       {          throw;       }       finally       {          connection.Close(); ServiceDomain.Leave();       }    } }

The class OrderControl can now be used like a simple .NET class — without the need to configure the application as is required with serviced components. Of course, not all features of Enterprise Services are available with Services without Components.




Professional C# 2005
Pro Visual C++ 2005 for C# Developers
ISBN: 1590596080
EAN: 2147483647
Year: 2005
Pages: 351
Authors: Dean C. Wills

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