我需要从.NET Web服务(asmx或WCF)调用dll(非托管c)。
从Web服务调用dll很简单,并按预期工作。
但是,当我加载测试Web服务时出现问题。 (错误代码0xC0000374 - “内部错误,通常涉及堆损坏”)。
d4的所有者告诉我,如果同时发送2个或更多呼叫,则dll在多线程环境中不可靠。
在传统的Windows应用程序中,我通过实现单例类来保护dll来处理这个问题。 是否有推荐的方法在Web服务实现中实现此目的?
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?
最满意答案
如果你只需要确保一次只有一个线程可以调用你的dll,你可以在lock语句中包含对它的任何访问:
public static class MyDllCalls { private static object _lockObject = new object(); public static int SomeCall() { lock (_lockObject) { return CallSomeFunctionInYourDll(); } } }只有一个线程可以在给定时间保持锁定,因此这样可以防止多个线程并行进行调用。
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.
更多推荐
发布评论