开发者

.NET Web Service - How to call unmanaged C dll

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 trad开发者_高级运维itional 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?


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜