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