<p>We have a third party dll that is used in our web service hosted in IIS6. The problem is that once this dll is loaded into memory, the exception <a href="http://msdn.microsoft.com/en-us/library/system.accessviolationexception.aspx" rel="nofollow noreferrer">AccessViolationException</a> gets thrown if a thread different then the one that created it tries to execute any code within the dll. The worker process is multi threaded and each call to the web service will get a random thread from the pool. We tried to unload it from memory and reload it each time we needed it, but I guess only the front end is .Net and the rest is unmanaged so it never actually gets completely unloaded from memory. We are using VB and .Net 2.0. Any suggestions?</p>

<p>(Response to Rob Walker)</p>

<p>We thought about creating a new thread and using it to call the dll, but how do we make the thread sit and wait for calls? How do you delegate the call to the thread without having the Dispatcher class supplied by .Net 3.0? Creating a hidden form and putting it in a message loop might work. And then we could call the Invoke() method of the form. But I can see many problems occurring if we create a form inside an IIS hosted web service.</p>

[{'answer_id': 44523, 'author': 'Krucible', 'author_id': 2835, 'author_profile': 'https://Stackoverflow.com/users/2835', 'pm_score': 1, 'selected': False, 'text': '<p>I have read about a class in .net 3.0 called <a href="http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.aspx" rel="nofollow noreferrer">Dispatcher</a> that allows you to put a thread in a loop and then call the method Invoke() using a delegate to execute a method using the thread. But this solution will not work if you cannot update to .Net 3.0.  Another solution would be to host the third party dll in another application on the server and use some form of <a href="http://msdn.microsoft.com/en-us/library/system.runtime.remoting.aspx" rel="nofollow noreferrer">Remoting</a> to access it.  But you may still have a problem with the Remoting because it behaves similar to IIS and will also pick a random thread to execute the code . To get around this, you could put a wrapper around the dll and use it to delegate the calls to the UI thread by using the Invoke() method of the form.</p>\n'}, {'answer_id': 44552, 'author': 'Rob Walker', 'author_id': 3631, 'author_profile': 'https://Stackoverflow.com/users/3631', 'pm_score': 1, 'selected': False, 'text': '<p>I think you need to look at using a wrapper thread that handles all calls to the DLL, and deals with the serialization.</p>\n\n<p>This thread is outside of the managed thread pool, so you control its lifetime.  But even this would not be foolproof unless you can prevent IIS from restarting the app domain your web service is in.</p>\n\n<p>You also need to worry about what happens when two web service requests come in at the same time.  Is each call into the DLL standalone, or do you have to group together all the calls associated with a single web service request before allowing any other request to be serviced?</p>\n'}, {'answer_id': 174399, 'author': 'GvS', 'author_id': 11492, 'author_profile': 'https://Stackoverflow.com/users/11492', 'pm_score': 0, 'selected': False, 'text': '<p>You could create a service that hosts the extra DLL. Via remoting you access the service, this will dispatch the calls the the thread that manages the DLL.</p>\n\n<p>This way you have control over the thread that calls the DLL, and over the lifetime of the thread.</p>\n'}, {'answer_id': 177540, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>I'm a bit rusty, but you might try wrapping calls to the DLL in a single threaded apartment COM object.  This would ensure that all calls go through the COM object's windows messaging thread.  I think you would have to register the component in a server application within Component Services to do this.</p>\n"}, {'answer_id': 256491, 'author': 'Josh', 'author_id': 32963, 'author_profile': 'https://Stackoverflow.com/users/32963', 'pm_score': 0, 'selected': False, 'text': "<p>Can you run the dll inside different threads as different instances? Like thread1 creates an instance of this third party dll, and thread2 also does, but as long as thread1 doesn't try to use thread2's instance it won't throw that exception? If thats the case, .Net never unloads any code once its loaded, if you load an assembly and then remove it, it still sits in that application pool. If you can create more than one instance at a time, you could load it up in a separate app pool you control per a request, then unload the app pool. Performance might drop though.</p>\n"}]
['https://Stackoverflow.com/questions/44522', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2835/']