Hello All,

I have searched every forum and google etc but nothing directed me in right path.

Requirement: I am using a COM object which can be initialized only once. But, when I try with webapi the first run goes fine and the rest fails as the COM object does not allow initialization for second time. With the below code, I notice that the new COMClass(); is being triggered everytime there is a GET request. Is there a way I can define the COMClass object to be executed only once and can be re-used by all the GET requests?

   namespace MvcApplication3.Controllers
    {
        public class ValuesController : ApiController
        {
            COMClass objCOM = new COMClass();

            public string Get()
            {
                while(objCOM.getResult() != string.Empty)
                {
                    Thread.Sleep(1000);
                }

                return objCOM.getResult();
            }

        } // API controller

        public class COMClass
        {

            private InteropCOMClass IClass = new InteropCOMClass();
            private InteropCOMInterface IFace;

            private string Result;

            public COMClass()
            {
                try
                {
                    //InitialSettings
                    IClass.OnEvent += CustEvent;
                }
                catch(Exception l_Exception)
                {
                    Result = l_Exception.Message;
                }
            }

            public void CustEvent()
            {
                Result = IClass.something; //something returns the new value; 
            }

            public string getResults()
            {
                return Result;
            }

        } // COMClass

    } // Namespace

You can probably use the Session_Start method in global.asax.cs.

If you need only one instance for all sessions, you can turn your COM instance into a singleton, to make sure it's only instantiated just once.

Thanks a lot for you reply Pritaeas.

I will try that post back the results soon!

Nope, that didnt help.... Everytime I call the GET service line 5 is executed where I call the singleton instance(New instance of ValuesController will be initiated too).

Maybe it requires a method to define object globally(Global.asax) and access it in ValuesController?

Create a static class with one static method that returns a private static ComClass instance on demand, and creates/initializes it if it's null (singleton).

public static class ComClassSingleton
{
    private static COMClass comClass;

    public static COMClass GetComClass()
    {
        if (comClass == null)
        {
            comClass = new COMClass();
        }

        return comClass;
    }
}

Then use:

public class ValuesController : ApiController
{
    COMClass objCOM = ComClassSingleton.GetComClass();

    // ...
}

I have tried exactly the same method. When I try to debug keeping the breakpoint at Line 3 of your code 'COMClass objCOM = ComClassSingleton.GetComClass();' works fine for first time but when I try GET again without restarting the application, it creates a new object of COMClass.

Ideally when the web-api GET request is executed, is it not supposed to call GET function directly without re-initializing the ValuesController again??

Not sure what's wrong then. Hard to say without being able to debug.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.