COM+ 1.5 Services Without Components
Forse non tutti sanno che con .NET 1.1 e Windows 2003 (COM+ 1.5) è possibile sviluppare dei componenti che utilizzano i servizi di COM+, ma che non serve che siano "configurati" in COM+.
Ci sono da qualche tempo delle API per fare queste cose (CoEnterServiceDomain e CoLeaveServiceDomain nella comsvcs.dll) e con .NET 1.1 e Windows 2003 abbiamo anche delle classi managed (ServiceConfig e ServiceDomain) che ci permettono di utilizzarli.
Ecco un esempio di codice C# che le utilizza:
[WebService(Namespace="http://schemas.devleap.com/SWCService")]
public class SWCService : System.Web.Services.WebService
{
[WebMethod()]
public Boolean DeleteCustomer(String customerID)
{
// Preparo la configurazione da utilizzare
ServiceConfig svcConfig = new ServiceConfig();
// Attivo il tracking in COM+ delle attività
svcConfig.TrackingEnabled = true;
svcConfig.TrackingAppName = "Demo SWC Web Service";
svcConfig.TrackingComponentName = "SWCService";
// Imposto l'IsolationLevel della transazione
svcConfig.IsolationLevel = TransactionIsolationLevel.ReadCommitted;
svcConfig.Transaction = TransactionOption.RequiresNew;
// "Entro" in COM+
ServiceDomain.Enter(svcConfig);
// Immaginiamo di voler cancellare il record da SQL
DeleteSQLCustomerRecord(customerID);
// e di voler cancellare il record da un CRM
DeleteCRMCustomerRecord(customerID);
// "Esco" da COM+
TransactionStatus status = ServiceDomain.Leave();
// Comunico al chiamante "come è andata..."
return(status == TransactionStatus.Commited);
}
private void DeleteCRMCustomerRecord(String customerID)
{
OleDbConnection cn = new OleDbConnection(@"File Name=d:\NWindCRM.udl");
OleDbCommand cmd = new OleDbCommand("spCRMDeleteCustomer", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@CustomerID", OleDbType.VarWChar, 5).Value = customerID;
try
{
using (cn)
{
cn.Open();
cmd.ExecuteNonQuery();
}
}
catch (OleDbException exOleDb)
{
ContextUtil.SetAbort();
}
}
private void DeleteSQLCustomerRecord(String customerID)
{
SqlConnection cn = new SqlConnection("server=localhost;database=Northwind;integrated security=SSPI;");
SqlCommand cmd = new SqlCommand("spDeleteCustomer", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@CustomerID", SqlDbType.NChar, 5).Value = customerID;
try
{
using (cn)
{
cn.Open();
cmd.ExecuteNonQuery();
}
}
catch (SqlException exSql)
{
ContextUtil.SetAbort();
}
}
}