0

I am using external 32bit c++ library which communicates with some hardware, that library is used as managed assembly inside my code.

When I use it with console application, it works without any issues. When code is hosted on IIS I have several issues.

First, I was getting stackoverflow exception continuously, until I set Application Pool on IIS to enable 32bit application and changed my code to increase stack size. I used example from here How to change stack size for a .NET program?.

Here is the example code after change:

   public class SomeExternalLibraryClient
   {
       [DllImport("SomeExternalLibrary.dll")]
       internal static extern int open();

       public delegate void OnEventCallback(int reserved1, int eventType, int eventData1, int eventData2, string dataStr, int reserved2);

       [DllImport("SomeExternalLibrary.dll", CallingConvention = CallingConvention.Cdecl)]
       static extern double subscribeToEvents([MarshalAs(UnmanagedType.FunctionPtr)]OnEventCallback func);

       static OnEventCallback callback = new OnEventCallback(OnEvent);

       static void OnEvent(int reserved1, int eventType, int eventData1, int eventData2, string dataStr, int reserved2)
       {
           callback(reserved1, eventType, eventData1, eventData2, dataStr, reserved2);
       }

       public int Open()
       {
           int openValue = -1;

           var t = new Thread(() =>
           {
               openValue = OpenCommunication();
           }, 4194304);
           t.Start();
           t.Join();

           return openValue;
       }

       private static int OpenCommunication()
       {        
           return open();            
       }      

       public void SubscribeToEvents(OnEventCallback func)
       {
           callback = func;
           subscribeToEvents(OnEvent);
       }
   }

I tested it again with console application and it worked, callback worked as expected.

In IIS hosted application, I stopped getting stackoverflow exception and same value as in console applicatrion is returned from OpenCommunication() method. Unfortunately, my callback function isn't invoked as it is in console application.

Any suggestions?

Community
  • 1
  • 1
Milan Stojanovic
  • 307
  • 1
  • 14
  • You are probably try to do this in some website hosted in IIS, the reason for callback method not being called could be due to finished execution of web method. The web method class object wont be available for callback method call. – Adil Feb 21 '17 at 10:13
  • @Adil you mean Milan should start a new thread? – GôTô Feb 21 '17 at 10:41
  • Did you compile console application in Debug mode but IIS in release mode? And your IIS must be 64bit as your .dll is. – Ruslan Skaldin Feb 21 '17 at 10:48
  • @GôTô, I mean he would wait for callback before response is returned back e.g by using async - await or AutoResetEvent.WaitOne – Adil Feb 22 '17 at 04:10
  • I've identified a potential problem that has to do with the actual implementation of the hardware component - basically as a workaround I'm polling for hardware events every 100ms. However, when doing this, I've noticed that t.Join() (which should block the thread) actually executes immediately when run from IIS, but when running from a console app (using the exact same libraries and setup) it actually blocks there for maybe 10-12 seconds until it starts the hardware properly. That might be the core problem here. Does IIS load external DLLs differently than when doing it with a console app? – Milan Stojanovic Feb 23 '17 at 10:13

0 Answers0