views:

96

answers:

2

I have a requirement to call a dll (unmanaged c) from a .NET web service (asmx or WCF).

Calling the dll from the web service is straightforward and works as expected.

However, issues emerge when I load test the web service. (error code 0xC0000374 - "an internal error, typically involving heap corruption”).

I've been informed by the owner of the dll that the dll isn't reliable in a multi-threaded environment if 2 or more calls are sent at the same time.

In a traditional windows app, I'd deal with this by implementing a singleton class to protect the dll. Is there a recommended approach for achieving this in a web service implementation?

+2  A: 

If you only need to ensure that only one thread at a time can call your dll, you can wrap any access to it in lock statements:

public static class MyDllCalls
{
    private static object _lockObject = new object();

    public static int SomeCall()
    {
        lock (_lockObject)
        {
            return CallSomeFunctionInYourDll();
        }
    }
}

Only one thread can hold the lock at a given time, so this way you can prevent several threads from making calls in parallel.

Fredrik Mörk
A: 

To prevent from multiple threads calling the same method in parallel you could use a locking mechanism.

Darin Dimitrov