System.EnterpriseServices.ServiceDomain gives you an easy to implement distributed transactions. However, we saw a following exception happened intermittently when the ASP.Net pages are switched to run under .Net 2.0.
System.Runtime.InteropServices.COMException was unhandled
Message="There is no MTS object context (Exception from HRESULT: 0x8004E004)"
Source="mscorlib"
ErrorCode=-2147164156
StackTrace:
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
at System.EnterpriseServices.ServiceDomain.Leave()
Source code has the issue:
public class AspPage: System.Web.UI.Page
{
protected AspPage()
: base()
{
ServiceConfig config = new ServiceConfig();
config.Transaction = TransactionOption.Required;
ServiceDomain.Enter(config);
}
protected override void OnUnLoad(EventArgs e)
{
base.OnLoad(e);
ServiceDomain.Leave(); //Throws "There is no MTS object context" exception
}
}
We did some experiments and found that once we move ServiceDomain.Enter into OnLoad, the problem dispears. It seems that .Net 2.0 has a bug or has changed some threading behaviors. A rule of thumb is only call ServiceDomain.Enter from a method or one of the Page events (for example, Page_Load, Page_Init, and so on), and do not call it at construction time.
Problem solved:
public class AspPage: System.Web.UI.Page
{
protected AspPage()
: base()
{
}
protected override void OnLoad(EventArgs e)
{
ServiceConfig config = new ServiceConfig();
config.Transaction = TransactionOption.Required;
ServiceDomain.Enter(config);
base.OnLoad(e);
}
protected override void OnUnLoad(EventArgs e)
{
base.OnUnLoad(e);
ServiceDomain.Leave();
}
}
THIS POST IS PROVIDED "AS-IS" WITH NO WARRANTIES AND CONFERS NO RIGHTS. Build time: Sun 03/30/2008 . ©2007 Dalun Software. All rights reserved. Back to Article List