Answered by:
How to call DLL code from a Web Service

Question
-
Hello,have to create a web service that allows me to access some local data from a remote computer. To do this, I have to call some unmanaged code from a third-party DLL. I cannot recompile the DLL; I can only use it as it is. It contains only basic C code, no COM. I created a wrapper class where I "imported" all the functions from the DLL using P/Invoke [DllImport]. Then I created the classes that access that data through the DLL. If I use those classes in a Console Application everything works well. However, when I created a Web Service that uses those classes, I get an exception at each point where some unmanaged code is called.System.TypeInitializationException: The type initializer for 'MyWebService' threw an exception. ---> System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.Are there any security restrictions that do not allow me to call functions from the DLL? Is there a way to make the ASP.NET Development Server / IIS ignore these kind of issues?Thanks in advance.Cristian PatrasciucWednesday, November 19, 2008 2:11 PM
Answers
-
The console application is in a single threaded environment, possibly one that the DLL was tested with. It's a different environment with the web service, and it appears that the DLL does not support that environment, since it can't even run when there's only a single request.
You may need to talk to the creator of that DLL to see if they mean for it to work in a web service. It's quite possible that it just will not work at all.
John Saunders | Use File->New Project to create Web Service Projects- Proposed as answer by edhickey Monday, December 1, 2008 9:39 PM
- Marked as answer by Lucian BaciuMicrosoft employee, Moderator Thursday, December 4, 2008 11:28 AM
Thursday, November 20, 2008 2:46 AMModerator
All replies
-
That exception means what it says - that DLL is attempting to read or write memory incorrectly.
Does this happen 100% of the time, even when there is only one request to the web service? I ask because I had a similar experience with a piece of unmanaged code. It turned out that this library was not thread-safe, even though it claimed it was. Every request to a web service arrives on a different thread. Code that is not written to understand this (especially old unmanaged code) may very well not work.
I resolved the problem by ensuring that only a single thread could ever access the unmanaged code at a time. All access went through a singleton class, and that class used the C# "lock" statement to make sure that only one thread could access the DLL at a time. This was slower than we would have liked, but a lot faster than having the unmanaged code write all over the C runtime heap.
BTW, I found the solution by accident. I was using the AQTime tool from Automated QA, to profile what we thought was a memory leak. The tool happens to have a window that shows unmanaged exceptions, debug output and other events. I happened to notice that the C runtime library had been complaining bitterly about the heap being trashed, but of course, nothing except AQTime was listening. You might try a free trial of their product, just so you can watch the Events window while your code is running.
John Saunders | Use File->New Project to create Web Service ProjectsWednesday, November 19, 2008 8:44 PMModerator -
There is only one request. I just start debugging my project, the ASP.NET Developer service is started and then I use the web service's web page to Invoke a method. It doesn't matter which function I try to call from the DLL. I always receive the same error. It's strange, because in the Console Application it works as expected.Wednesday, November 19, 2008 11:14 PM
-
The console application is in a single threaded environment, possibly one that the DLL was tested with. It's a different environment with the web service, and it appears that the DLL does not support that environment, since it can't even run when there's only a single request.
You may need to talk to the creator of that DLL to see if they mean for it to work in a web service. It's quite possible that it just will not work at all.
John Saunders | Use File->New Project to create Web Service Projects- Proposed as answer by edhickey Monday, December 1, 2008 9:39 PM
- Marked as answer by Lucian BaciuMicrosoft employee, Moderator Thursday, December 4, 2008 11:28 AM
Thursday, November 20, 2008 2:46 AMModerator -
In the console application I have the main thread that calls some initialization and setup functions from the DLL, then I wait for the user to press Enter. Meanwhile, the application has another thread that fetches some data from the DLL and displays it to the console. If this works I think that the DLL supports a multi-threaded environment. Anyway, I'll try to contact the creator of the DLL to make sure this is true.Saturday, November 22, 2008 9:12 PM
-
To be clear, there is more than one issue involved here. A console application at least has a window. Your DLL may be depending on that. There is no window in a web service.
John Saunders | Use File->New Project to create Web Service ProjectsSaturday, November 22, 2008 9:21 PMModerator