Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9092851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:56:27+00:00 2026-06-16T22:56:27+00:00

I need to integrate an ASP.NET MVC website with a 3rd party COM application.

  • 0

I need to integrate an ASP.NET MVC website with a 3rd party COM application. The code looks something like this:

Type type = Type.GetTypeFromProgID(progID);
dynamic obj = Activator.CreateInstance(type);
if (!obj.DBOpenedStatus)
{
    obj.InitApplicationContext();
    //do stuff with other COM objects
}

The problem is that after a while I start getting COMExceptions most probably because I never close that context using obj.ReleaseApplicationContext(). So I’m thinking about writing a wrapper implementing IDisposable so that I can initialize the context in the constructor and release it when it’s disposed. But then I would need a lock so that another thread would not close the context while the current thread is still working. The code would look like this:

lock (_locker)
{
    using (MyComContextWrapper comContext = new MyComContextWrapper())
    {
        //do stuff with other COM objects
    }
}

I know very little about thread safety so what I’m asking you is the following:

  1. Is there anything wrong with my approach?
  2. Is there anything else I should consider (like deadlocks or something else)?
  3. Would you recommend a different approach?

Edit 1:
I looked up the progID of the COM class in the registry and found that ThreadingModel=Apartment so the COM application uses a Single-Threaded Apartment type.

Edit 2:
The COM application uses a system to generate ids for objects so that when these objects are persisted in the database they already have an Id. This system involves writing some information in the registry. After a while something goes wrong with this system and I start getting violation of primary key constraint errors.

Edit 3:
I sometimes get a System.ArgumentNullException: Value cannot be null. Parameter name: type at Activator.CreateInstance(Type type). Could this be because I’m trying to create a new STA object from another thread?

Edit 4:
Someone asked me to post the full the full stack of the COMExceptions, but I’m afraid this will make my question too localized and useless for anyone else but me. Furthermore, I actually found the cause of the problem: there were some very rare cases when the context was not initialised before using some com objects and this messed up the system, but the errors only showed up at a later time. So I made sure that the context is always initialized prior to using any com object and the problem went away.

I’m still interested in an answer about thread safety and my proposed solution. Or maybe the question should be closed as too localized?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T22:56:29+00:00Added an answer on June 16, 2026 at 10:56 pm

    You shouldn’t need to use a lock around your objects as the whole purpose of COM Threading is making your life easier and automatic (once you understand its use, and if the registry is correct) in multi-threading environments.

    In standard managed .NET scenarios, you don’t need to do anything specific either, even releasing the COM object should be done automatically by the garbage collector. So you shouldn’t need a using pattern.

    That being said, it may be possible that the underlying unmanaged COM object works better when it’s released once used, or maybe your application can’t really wait for the GC to happen for some reason (because of a hi usage for example). In these scenarios, you can use Marshal.ReleaseComObject or Marshal.FinalReleaseComObject to force the release of the COM object once used, and wraps it using a wrapper object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my asp.net application i need to integrate a jquery content slider with next,
What do I need to do to integrate an existing ASP.NET application into SharePoint?
I have the need to integrate some blob storage into an existing ASP.NET Mvc
My ASP.NET MVC 3 website was working fine, and then this afternoon I started
I currently have an ASP.NET MVC application that exists. I want to add a
I'm new to ASP.NET MVC, but I need to perform a search for articles
Currently i am working on one custom asp .net application, client wants to integrate
I have a website running on .NET 3.5 and ASP.NET MVC 2.0. I am
I have a little weird behavior with my ASP.NET MVC 2 application. I'm using
I have asp.net mvc 2 application. since couple of days I was trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.