S
Simon Woods
Hi
I am working on a web service whose business layer has a hybrid of COM
and .Net business objects. I can get the data from the .net business
objects fine. I have tests which prove that the COM business objects
work fine when accessing them from VS's test suite. However, when I try
and access them via thew web service, an error is generated. (Exception
from HResult: 0x800A0062). I managed to replicate this problem using
various testing scenarios and got rid of the problem by explicitly
setting the threading to STA.
So I figured that, when invoking any of my COM methods, I first need to
switch to an STA Thread.
I am struggling to know how to do this. I have tried 2 methods.
a) Getting hold of the current thread and setting its apartment state
to STA, but that generates an error on the development server
b) Explicitly trying to create a new STA thread. Here's my code ... but
be warned I'm a threading newbie!
public class STATaskExecutor
{
static Thread _thread;
static MyCOMDLL.CFactory _factory;
static MyCOMDLLTypes.IMyCOMType _myCOMObject;
static string _var1;
static string _var2;
static string _var3;
static bool _created;
public static MyCOMDLLTypes.IMyCOMType
CreateCOMObject(MyCOMDLL.CFactory p_factory, string p_param1, string
p_param2, string p_param3)
{
_factory = p_factory;
_var1 = p_param1;
_var2 = p_param2;
_var3 = p_param3;
_thread = new Thread(CreateCOMObjectInstance);
_thread.SetApartmentState(ApartmentState.STA);
_thread.Start();
while (!_created){ }
return _COMObject;
}
[STAThread]
private static void CreateCOMObjectInstance()
{
try
{
_COMObject = _factory.CreateIMyInterface(_var1, _var2,
_var3;
}
catch (Exception ex)
{
}
finally
{
_created = true;
_thread = null;
}
}
I am finding that the CreateCOMObjectInstance executes, but it is
failing with the same error.
a) Could it be failing for some other reason?
b) Where am I going wrong in relation to the implementation of creating
an new STA thread
(finally but non-essentially since it doesn't work)
c) Is there a more elegant ways of passing around the COM function (e.g.
as a first class function). I don't know if you can pass round COM
methods as first class functions!
Many thanks
Simon
I am working on a web service whose business layer has a hybrid of COM
and .Net business objects. I can get the data from the .net business
objects fine. I have tests which prove that the COM business objects
work fine when accessing them from VS's test suite. However, when I try
and access them via thew web service, an error is generated. (Exception
from HResult: 0x800A0062). I managed to replicate this problem using
various testing scenarios and got rid of the problem by explicitly
setting the threading to STA.
So I figured that, when invoking any of my COM methods, I first need to
switch to an STA Thread.
I am struggling to know how to do this. I have tried 2 methods.
a) Getting hold of the current thread and setting its apartment state
to STA, but that generates an error on the development server
b) Explicitly trying to create a new STA thread. Here's my code ... but
be warned I'm a threading newbie!
public class STATaskExecutor
{
static Thread _thread;
static MyCOMDLL.CFactory _factory;
static MyCOMDLLTypes.IMyCOMType _myCOMObject;
static string _var1;
static string _var2;
static string _var3;
static bool _created;
public static MyCOMDLLTypes.IMyCOMType
CreateCOMObject(MyCOMDLL.CFactory p_factory, string p_param1, string
p_param2, string p_param3)
{
_factory = p_factory;
_var1 = p_param1;
_var2 = p_param2;
_var3 = p_param3;
_thread = new Thread(CreateCOMObjectInstance);
_thread.SetApartmentState(ApartmentState.STA);
_thread.Start();
while (!_created){ }
return _COMObject;
}
[STAThread]
private static void CreateCOMObjectInstance()
{
try
{
_COMObject = _factory.CreateIMyInterface(_var1, _var2,
_var3;
}
catch (Exception ex)
{
}
finally
{
_created = true;
_thread = null;
}
}
I am finding that the CreateCOMObjectInstance executes, but it is
failing with the same error.
a) Could it be failing for some other reason?
b) Where am I going wrong in relation to the implementation of creating
an new STA thread
(finally but non-essentially since it doesn't work)
c) Is there a more elegant ways of passing around the COM function (e.g.
as a first class function). I don't know if you can pass round COM
methods as first class functions!
Many thanks
Simon