0

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
2
Contributors
6
Replies
29
Views
3 Years
Discussion Span
Last Post by pritaeas
0

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.

0

Thanks a lot for you reply Pritaeas.

I will try that post back the results soon!

0

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?

0

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();

    // ...
}

Edited by pritaeas

0

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??

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.