Invalid Cast exception on COM interop in Windows Service

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




I've got a peice of code that creates a COM object and attempts to use
it. It works fine when it is run as an application. However, it
throws an "invalid cast" exception when it is run in a Windows Service.


The code snippet:

Dim strClass As String = "PSIGPoller.CWorkManager"
Dim obj As Object

obj =
Activator.CreateInstance(Type.GetTypeFromProgID(strClass))

m_objWorkManager = CType(obj, CWorkManager) '<-- throws
'invalid cast' in service

A few more notes:
- When it runs as an application, it is using exactly the same interop
DLLs as the service is using. The app and the service .exe files are
in the same directory.

- The service works when it is run on the machine with the development
platform. If it is installed on a machine without the development
platform we get the exception. I currently have 2 development machines
that the service will run correctly on, and 2 test machines that it will
not run on.

- The .Net framework version on both machines is reported as
1.1.4322.573.


While googling around I've read several posts on the net about various
reasons that you can get an invalid cast, ranging from threading model
differences between the app/service and the DLL's to security issues
with the DLL. If this was an app/service difference it would seem more
straight forward; however, when the service runs correctly on the
development machines, it confuses things a bit. If there was a
threading model issue, why would the service run correctly on any
machine? If there was a problem with security access to the COM
object, why would the application run correctly - the service is
logging in using the same user account that I run the application
under.

This looks like it boils down the either something that is installed
with the SDK or VS7 development environment, or something that happens
when the service is built.

I'm copying everything from the bin directory when I grab the files to
install the it on the other machine. Is there any other DLL's that I
would need beyond what is in the bin directory? (The fact that the
application works makes this point more interesting...)


Any ideas would be appreciated.

Thanks,
Perry


--
pmgreen
------------------------------------------------------------------------
pmgreen's Profile: http://www.hightechtalks.com/m663
View this thread: http://www.hightechtalks.com/t2324212

.



Relevant Pages

  • D7 in the path
    ... IDE, Install and CommandLine ... use of D7 bin directory in the path. ... So, Borlanders in install group, ...
    (borland.public.delphi.non-technical)
  • Re: Adding V3 webparts in ONET.xml
    ... so just used the assembly name in the safe controls web.config entry. ... I then placed the dll in the bin directory of the port 80 site. ... but did NOT install it into the Gac. ...
    (microsoft.public.sharepoint.portalserver.development)
  • Re: Crystal Reports and created with Visual Studio 2005 wont work on server
    ... of your application or the bin directory of your ... > I tried copying over all the dlls to the bin folder of the application ... > worried that if I install the version 10 dlls some of the older crystal ... > reports wont work. ...
    (microsoft.public.vb.crystal)
  • RE: deploying webparts
    ... If your Web Part is installed in the bin directory the an iisreset is not required. ... i will just get the cab file and install using stsadm. ...
    (microsoft.public.sharepoint.portalserver.development)