Resolving a Com Object in ASP.net Application

I have a com dll built into .net and named it as interop.mycomlib.dll in my ASP.NET application. I initialize the class in the com object, then call some functions, and finally, when the user signs up or closes the browser, I release the com object.

Below is the code I'm using. InitInstance() is InitInstance() called for the first user, but when the user signs the ExitInstance() of the com command, it is not called.

If any other user character on InitInstance() not called again, because the same instance of the com object is used for all users. ExitInstance() is called only when iisreset is running or the w3wp process is terminating.

Is this the default behavior, how com interop works with asp.net, or is there something I can't see to completely destroy the com object?

 public class ComFacade : IDisposable { public ComFacade() { myComObj_ = new MyCOMLib.MyClientClass(); } .............................. public void Dispose() { Dispose(true); myComObj_ = null; GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (!this.disposed) { if (disposing) { try { Marshal.ReleaseComObject(myComObj_); } catch (Exception ex) { throw; } } this.disposed = true; } } } 

thanks

+4
source share
1 answer

You do not indicate the scope of the variable containing the ComFacade instance. If the variable is static, then this will be the behavior that I would expect.

I would suggest you understand the life cycle of an ASP.NET page and the implications of this with variables from different areas. If the COM link should not be single, then you will need to create a new instance each time the page loads and deletes it as needed (possibly when the page is displayed).

Update (based on comment)

Note. This answer applies to any object in .NET that you are trying to save longer than a single page request. In the end, all objects are removed / garbage collected.

You note that an object is created when a user logs in and is deleted when they log out. The only way to do this is to cache the object in something static in order to reference it. Keep in mind that every time a user does something in his browser, the request goes from the browser back to IIS / ASP.NET for processing and causes the page life cycle (excessive simplification, but good enough). Each time the user does this, the page can be processed by a different thread in the application pool each time. If several users interact with the site, then over the course of a certain period of time the same stream can (and most likely will) be used by more than one user. In short, that's why with ASP.NET/IIS you have to be extremely careful when using singleton / static members.

My question, on the other hand, is why do you need a reference to a COM object for more than one page request?

+2
source

Source: https://habr.com/ru/post/1395422/


All Articles