.NET Web服务

编程入门 行业动态 更新时间:2024-10-12 20:20:20
.NET Web服务 - 如何调用非托管C dll(.NET Web Service - How to call unmanaged C dll)

我需要从.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.

更多推荐

本文发布于:2023-08-05 23:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1440912.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:NET   Web

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!