Determine what is causing the error "This SqlTransaction has completed; it is no longer usable" error - asp.net

We run a proprietary web based Finance system created in ASP.NET that is throwing the following error on a regular basis:-
Exception: System.InvalidOperationException
Message: This SqlTransaction has completed; it is no longer usable.
StackTrace: at System.Data.SqlClient.SqlTransaction.ZombieCheck()
at System.Data.SqlClient.SqlTransaction.Rollback()
at Agresso.Driver.Database.ManagedConnection.RollbackTransaction()
at Agresso.Driver.Database.ManagedConnection.Close()
at Agresso.Driver.Database.ManagedConnection.Dispose(Boolean isDisposing)
at Agresso.Driver.Database.ManagedDatabase.Dispose(Boolean isDisposing)
at Agresso.Driver.Database.DatabaseBase.Finalize()
The issue has been logged with the suppliers but they believe the error is due bespoke work that we have created ourselves.
What would you recommend as a way of determining what is causing the system to get into this state? We do not have access to debug the application but can run profiler on the database. However so far this has not shed any possible clues to what is causing the problem.
We are currently just restarting the app pool, which resolves the problem for a period of time before it occurs again.
Many thanks.

Pure speculation, but:
if DatabaseBase.Finalize() is a .NET finalizer, it shouldn't be attempting to dispose managed resources - so this may well be a bug in the DatabaseBase class finalizer.
However a finalizer for an IDisposable class is normally only called if the caller has failed to Dispose an instance of the class (the Dispose method usually calls GC.SuppressFinalize to prevent the finalizer from running).
I would start by looking at your own code that accesses the database, and make sure you call Dispose for all IDisposable types, typically by wrapping in a using block. FxCop / Visual Studio Code Analysis can help you identify places in your code where you're failing to do this.
This may well clear up the problem. If not, I suggest you try to create a minimal repro for the problem and contact the supplier again.

Related

I need to call a function from a third-party library from ASP.NET, but it must not be allowed to run asynchronously

This is the situation:
I use a third-party library to do some SPF checks
The checks are done from an ASP.NET web site
The third-party library uses lots of 'await/async' code, although I am calling a non-async method to do it
Most of the time, w3wp (yes, the actual w3wp.exe) simply crashes with the following error ' Invalid token for impersonation - it cannot be duplicated'.
I am assuming this is because the await/async grabbed a Thread from the threadpool which has some sort of illegal / weird identity on it, so I am wondering if there's any way to call a bit of code and tell .Net to 'forget about' async/await and just run it synchronously, on the same thread.
I've found a solution: somewhere in the product the user was being impersonated with identity.impersonate(), but the impersonation context was never .undo()'d. After making sure the identity.impersonate() was properly matched by an .undo(), the problem stopped occurring.
I did try making my own SynchronizationContext, but that didn't work in this instance, as passing on the impersonation just passed on the 'illegal' impersonation context too.

Strange behavior of RemoteObject after application built, no fault no result

When i run it in Flash Builder (debug mode) the remote object called successfully. but whenever i build the application (AIR application), then the remote object will return no result nor fault, the busy cursor is showing about 3 seconds. then no clue at all.
Any idea how to get advance fault or something than regular fault event or result event?
or anyone have the same experience?
UPDATE:
Actually it was failed only for ONE service method, for other method (some of them took longer time to call) the service call is work fine.
CASE SOLVED
So the problem was not on the service call, but on my result conversion that cause the advanced datagrid failed to render.
Best regards
ktutnik.
Try using a software like Charles to see what happens during the network call.

Webservice unavailable

I have an ASP.NET C# 3.5 web application that consumes another ASP.NET web service as a web reference. The web service is built into some proprietary hardware device. The problem is that that device has been having troubles and not alwasy accessible. My web application is suffering brcause of it, as it takes over a minute to load. It does load, but not acceptable.
The service is instantiated in a try catch block and no exception is being throw, but the output windows displays:
A first chance exception of type 'System.Net.WebException' occurred in System.dll
I know there is a better way to handle this, but I am drawing blanks.
Any help is appreciated.
UPDATE: Still looking for an answer on how to handle webservices that become unavailable without affecting website.
After tearing it apart, I found the exception. It is a standard "Unable to connect" exception. The problem is now the timeout, I have tried setting the asyncTimeout to 5000 in the web.config under the System.Web -> Pages properties. It is still taking aroung 20 seconds to throw the exception. Any ideas?
If you saw a "first chance exception" but your exception handler didn't get it, that means that the exception was handled elsewhere (swallowed, consumed by an exception handler, etc.) Perhaps something in the .NET libraries already handled that exception, and you need not concern yourself with it in your code. Or maybe you left some exception swallowing somewhere in your code.
You ought to consider using a timeout in your web request.
Simple solution, poll the service using JavaScript after page load.
Without any details regarding frequency/usage of the service and not seeing any code, heres a thought or two.
Its most likely the web method on this hardware that giving the error, so I'd pursue any support options you have (if any), but just for giggles, try this first to see if it helps....
I noticed that some people online said that they were able to get around this (in their scenario) by setting the KeepAlive to false on the requesting object, so that way your aren't inadvertently using an old (stale) connection to the service. You may be trying to "Keep Alive" but the webserver timed out the connection on you. Worth a quick try...
Good Luck!
In addition to the above, I would use a http debugger (like fiddler2) to get a better idea of what is happening on the wire.

Previously working webservice stopped working

All of a sudden we started to get this error in our webapplication.
It's weird because it has been working for months and months and noone has ever touched the code.
Does anyone have any idea why this error could occur all of a sudden?
Server Error in '/' Application.
System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Runtime.InteropServices.COMException (0x8007203A): The server is not operational.
at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
at System.DirectoryServices.DirectoryEntry.Bind()
at System.DirectoryServices.DirectoryEntry.get_AdsObject()
at System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne)
at System.DirectoryServices.DirectorySearcher.FindOne()
at AuthTools.GetUserMemberShip(String login) in D:\IIS\WWW_reports_WebServices\App_Code\AuthTools.vb:line 35
You are using some unmanaged COM object from your managed code. Could it be that this object changed? I.e. it's not your application, it's the unmanaged library you are using. I might be wrong, but that's all I can think of when looking at the stack trace.
Heh, it's a bit embarrassing but our webservice queries our active directory to find user groups, and due to a missconfiguration where a group had a member it was also a member of, our application ended up in an endless loop.
The .Net Framework BCL uses a lot of wrapper objects around legacy COM code to interact with ActiveDirectory and other LDAP sources. This can be caused by changed settings at the AD server, or there are issues with connection management to AD (are you properly closing your connections, for example.)
I would start investigating from the server-end and determine issues from there. The diagnostics/error-messages within the .Net Framework classes, because they bubble up through COM, aren't that helpful.

How do you log errors (Exceptions) in your ASP.NET apps?

I'm looking for the best way to log errors in an ASP.NET application.
I want to be able to receive emails when errors occurs in my application, with detailed information about the Exception and the current Request.
In my company we used to have our own ErrorMailer, catching everything in the Global.asax Application_Error. It was "Ok" but not very flexible nor configurable.
We switched recently to NLog. It's much more configurable, we can define different targets for the errors, filter them, buffer them (not tried yet). It's a very good improvement.
But I discovered lately that there's a whole Namespace in the .Net framework for this purpose : System.Web.Management and it can be configured in the healthMonitoring section of web.config.
Have you ever worked with .Net health monitoring? What is your solution for error logging?
I use elmah. It has some really nice features and here is a CodeProject article on it. I think the StackOverflow team uses elmah also!
I've been using Log4net, configured to email details of fatal errors. It's also set up to log everything to a log file, which is invaluable when trying to debug problems. The other benefit is that if that standard functionality doesn't do what you want it to, it's fairly easy to write a custom appender which can process the logging information as required.
Having said that, I'm using this in tandem with a custom error handler which sends out a html email with a bit more information than is included in the standard log4net emails - page, session variables, cookies, http server variables, etc.
These are both wired up in the Application_OnError event, where the exception is logged as a fatal exception in log4net (which then causes it to be emailed to a specified email address), and also handled using the custom error handler.
First heard about Elmah from the Coding Horror blog entry, Crash Responsibly, and although it looks promising I'm yet to implement it any projects.
I've been using the Enterprise Library's Logging objects. It allows you to have different types of logging (flat file, e-mail, and/or database). It's pretty customizable and has a pretty good interface for updating your web.config for the configuration of the logging. Usually I call my logging from the On Error in the Global.asax.
Here's a link to the MSDN
I use log4net and where ever I expect an exception I log it to the appropriate level. I tend not to re-throw the exception because it doesn't really allow for as-nice user experience, there is less info you can provide at the current state.
I'll have Application_Error also configured to catch any exception which was not expected and the error is logged as a Fatal priority through log4net (well, 404's are detected and logged as Info as they aren't that high severity).
My team uses log4net from Apache. It's pretty lightweight and easy to setup. Best of all, it's completely configurable from the web.config file, so once you've got the hooks in your code setup, you can completely change the way logging is done just by changing the web.config file.
log4net supports logging to a wide variety of locations - database, email, text file, Windows event log, etc. My team has it configured to send detailed error information to a database, and also send an email to the entire team with enough information for us to determine in which part of the code the error originated. Then we know who is responsible for that piece of code, and they can go to the database to get more detailed information.
I recently built an asp.net webservice with NLog, which I use for all my desktop apps. The logging works fine when I'm debugging in Visual Studio, but as soon as I switch to IIS the log file isn't created; I've not yet determined why, but it the fact that I need to look for a solution makes me want to try something else for my asp.net needs!
We use EnterpriseLibrary.ExceptionHandling.Logging. I like it a bit better than log4net because not only do we control the logging completely, but we can control the Throw/NoThrow decision within config as well.
We use a custom homegrown logging util we wrote. It requires you to implement logging on your own everywhere you need it. But, it also allows you to capture a lot more than just the exception.
For example our code would look like this:
Try
Dim p as New Person()
p.Name = "Joe"
p.Age = 30
Catch ex as Exception
Log.LogException(ex,"Err creating person and assigning name/age")
Throw ex
End Try
This way our logger will write all the info we need to a SQL database. We have email alerts set up at the DB level to look for certain errors or frequently occurring errors. It helps us identify exactly where the errors are coming from.
This might not be exactly what you're looking for. Another approach similar to using Global.asax is to us a code injection technique like AOP with PostSharp. This allows you to inject custom code at the beginning and end of every method or on every exception. It's an interesting approach but I believe it may have a heavy performance overhead.

Resources