Activator.CreateInstance fails in Windows Service

Posted by kbe on Stack Overflow See other posts from Stack Overflow or by kbe
Published on 2010-03-22T11:31:13Z Indexed on 2010/03/22 11:41 UTC
Read the original article Hit count: 338

Filed under:
|

I have an issue with a Windows Service which throws a NullReference exception whenever I try to use

var myType = Activator.CreateInstance(typeof(MyType))

There is no problem whenever I run the exact same code in a console window - and after debugging the obvious "which user is running this code" I doubt that it is a mere fact of changing the user running the service as I've tried to run the service using the computer's Administrator account as well as LocalSystem. I'm suspecting a Windows Update fiddling with default user rights but that's a bit of a desperate guess I feel.

Remember: The type and assembly exist and works fine - otherwise I wouldn't be able to run the code in a console window. It is only when running in the context of a Windows Service I get an error.

The question is: Can I in any way impersonate i.e. LocalSystem in a unittest by creating a GenericPrincipal (how would that GenericPrincipal look)?

© Stack Overflow or respective owner

Related posts about c#

Related posts about reflection