in

ASP.NET Weblogs

Christian Weyer: Smells like service spirit

What's first?

Databases, MSMQ, [you name a resource manager] and Services Without Components

Just another quick note from today's work:
as a lot of other people and well-known gurus have already stated - SWC (services without components) is maybe one of the most powerful and yet unknown features of.NET Enterprise Services (yeah, this COM+-y thing ...). No need for ServicedComponent and registration in the registry and COM+ catalog.

All the love and goodness can be found in the System.EnterpriseServices namespace - namely the ServiceConfig and ServiceDomain classes. E.g. to both update a SQL Server database and place a message in a transactional queue in MSMQ is as easy as some lines of code without the burden of a configured COM+ component.

using System.EnterpriseServices;
using System.Messaging;
using System.Data.SqlClient;

...

public void StoreData()
{
  ServiceConfig sc = new ServiceConfig();
  sc.Transaction = TransactionOption.Required;
  ServiceDomain.Enter(sc);
 
  try
  {
    using(SqlConnection myConnection = new SqlConnection("..."))
    {
      SqlCommand myCommand = new SqlCommand();

      MessageQueue myQueue = new MessageQueue(@".\MyQueue");
      myQueue.Send("1", "CW", MessageQueueTransactionType.Automatic);
  
      myConnection.Open();
      myCommand.Connection = myConnection;
      myCommand.CommandText = "INSERT INTO ...";
      myCommand.ExecuteNonQuery();
  
      ContextUtil.SetComplete();
    }
 
 }
  catch(System.Exception ex)
  {
    ContextUtil.SetAbort();
  }
  finally
  {
   
ServiceDomain.Leave();
  }
}

This feature works with Windows Server 2003 and Windows XP SP2, officially.
But, there is a tweak to get it working before XP SP2: Hotfix 828741. You won't see any single word about SWC mentioned there, but believe me, with this hotfix all things are good :) Thanks to Christian for this last hint.

Comments

No Comments