4

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

Below is the code I am using. Initially, for the first user, the InitInstance() is called but when the user signs off the ExitInstance() of the com is not called.

If any other user signs on the InitInstance() is not called again because the same instance of com object is used for all the users. ExitInstance() is called only when an iisreset is performed or the w3wp process is terminated.

Is this the default behavior of how com interop works with asp.net, or is there something I am missing to do to completely dispose 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

1 Answer 1

2

You don't mention what the scope of the variable containing the ComFacade instance is. If the variable is static then this would be the behavior I would expect.

I would suggest you understand the ASP.NET page lifecyle and the implications of that with variables of different scopes. Unless the COM reference is supposed to be a singleton then you will need to create a new instance each time the page loads and dispose of it as appropriate (probably when the page is rendered).

Update (based on comment)

Note: This answer applies to any object in .NET that you try to keep around longer that a single page request. Eventually all objects are disposed / garbage collected.

You mention that the object is created when the user logs in and disposed when they log off. The only way you could do this is to cache the object in something static to keep reference to it. Keep in mind that every time the user does something in their browser a request goes from the browser back to IIS/ASP.NET for processing and invokes a page life-cycle (over-simplification, but good enough). Each time the user does this the page may be handled by a different thread in the App Pool each time. If more than one user is interacting with the site then over a period of time the same thread may (and most likely will) get used by more than one user. In short this is why with ASP.NET/IIS you must be extremely cautious with using singletons / static members.

On a side note, my question would be why do you need a reference to the COM object for more than a single page request?

Sign up to request clarification or add additional context in comments.

5 Comments

The ComFacade instance is not static, it is created when the user signson and the same instance is used for the user for all his operations with the com until the user signsoff, then the ComFacade instence is disposed. In this case i think the com should also be disposed when the user signsoff but this does not happen.
@Arvind unless the object is static, it won't persist between requests, meaning that it has to be reinstantiated on every page load. Also, it is the responsibility of the process that uses the object to call .Dispose() - it doesn't happen automatically.
@Ken & David Thank you for the explanation, the com object we use creates message queues for each client and this is used to interact with host services since all the operations in the application use this the object is kept alive until signoff. The Comfacade is initialized in a object which is kept in session, when the user signsoff or the browser is closed I dispose the com object and then the ComFacade and the object is removed from the session and the session is cleared and abandoned to try fully dispose the com object. This anyway did not work.
Sorry for my silly question, I am still learning these things. Why is that the com object does not initialize for each user, after the first user signs on InitInstance() is never called, can only one instance of the com be created and this has to be used by all the users.
@Arvind we'll need to see more code to be able to answer your questions. Without knowing more about how your app is designed these questions are difficult to answer. Assuming the message queue is a queue of things to display to the user then I would normally design this as a DB table somewhere and have the service check for new message every time the page is loaded. It sounds like you're using something already available so you'll have to adjust how it's used to fit the paradigm of a web application (stateless).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.