Reputation: 432
I've got an unmanaged dll working in asp.net running on IIS 7. Most of the time after a few request the application hangs for about a minute before continue or showing an error. Do I have to dispose, release or call the DLLdifferent?
[DllImport("Fan.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int GET_CALCULATION_FAN_ALONE_PC(ref string input, string buffer);
Any help would be appreciated!
Upvotes: 1
Views: 1202
Reputation: 11039
It seems that the unmanged code is not thread safe. Here is a good discussion on the issue you are having:
Answers From http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/thread/504cdc70-16f1-4c39-a0c0-1d47b2a64f7b/
The thing to be careful about is that the unmanaged code may not be thread-safe. In the context of a web service, each request will arrive on a different thread, and the unmanaged code may not handle that. These crashes may be signs of memory corruption caused by code that has not been tested in a multithreaded environment.
There are a couple of different threading issues with unmanaged code that you might like to call from managed code:
1. It might not be safe to call the unmanaged code on more than one thread at the same time. This issue can be solved with correct locking. I stress correct. 2. The unmanaged code may somehow depend on being called on the same single thread all the time. This is less likely, but possible. This cannot be solved directly within an ASP.NET web application or web service application, unless you somehow start up a thread when the application starts, and marshall all requests for this code to that one thread. 3. The unmanaged code may be COM code and depend on running in a particular apartment. I don't know if there's a solution to that.
There will not be a general solution to this problem, at least not that you can solve. The general solution is for the developer or vendor of the unmanaged code to make their code safe to be called from managed code on multiple threads. Until they've thoroughly tested their code in that environment, you can't really be sure it will work.
Upvotes: 1