Hi guys,
I have tried the following proggie on a Windows Server 2003 of a
colleague of mine (I don't have this OS installed):
using System;
using System.EnterpriseServices;
using System.Data;
using System.Data.SqlClient;
namespace TestServiceDomain
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
DoDbOperation("db test");
ServiceConfig config = new ServiceConfig();
config.Transaction = TransactionOption.Required;
try
{
ServiceDomain.Enter(config);
DoDbOperation("Main");
Method1();
ContextUtil.SetComplete();
}
catch(Exception e)
{
ContextUtil.SetAbort();
}
finally
{
ServiceDomain.Leave();
}
}
public static void Method1()
{
ServiceConfig config = new ServiceConfig();
config.Transaction = TransactionOption.Supported;
try
{
ServiceDomain.Enter(config);
DoDbOperation("Method1");
Method2();
ContextUtil.SetComplete();
}
catch(Exception e)
{
ContextUtil.SetAbort();
}
finally
{
ServiceDomain.Leave();
}
}
public static void Method2()
{
ServiceConfig config = new ServiceConfig();
config.Transaction = TransactionOption.NotSupported;
try
{
ServiceDomain.Enter(config);
DoDbOperation("Method2");
throw new Exception("test rollback");
ContextUtil.SetComplete();
}
catch(Exception e)
{
ContextUtil.SetAbort();
}
finally
{
ServiceDomain.Leave();
}
}
public static void DoDbOperation(string param)
{
SqlConnection con = new
SqlConnection("Server=SERVER_IP_HERE;Database=DB_NAME_HERE;User
ID=USERNAME_HERE;Password=PASSWORD_HERE");
SqlCommand cmd = new SqlCommand("INSERT INTO Users(Username)
VALUES(@Username)", con);
cmd.Parameters.Add("@Username", param);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
But I got the following messagebox:
Application has generated an exception that could not be handled.
Process id .....
Click Ok to terminate ...
Click CANCEL to debug ...
I checked the db and it has a user record inserted with "db test"
username, so the db connection is ok. Obviously there is a problem
after that with the ServiceDomain. I am not sure if this is the
correct way to do transactions with SWC (as I want to mimic the
behaviour of an ordinary COM+ component with the only difference that
the transaction attributes are at the method level).
Anyone can help?
10x in advance,
Deyan Petrov
Johan Lindfors [MSFT] - 22 Aug 2003 07:58 GMT
Strange behaviour, since I just copied your code into a new Console
Application and it worked like a charm.
Have you tried debugging, and stepping through just to see which exception
gets thrown?
Regards

Signature
Johan Lindfors
Microsoft AB
This posting is provided "AS IS" with no warranties, and confers no rights.
> Hi guys,
>
[quoted text clipped - 128 lines]
> 10x in advance,
> Deyan Petrov