Windows service with COM initialization is not working any more after upgrading from .NET Framework 4.5.2 to 4.8

0

I have a working windows service made with framework 4.5.2 upgraded to framework 4.8. But now the service is not working any more because of the failing with Error: System.ApplicationException: CoIntializeSecurity failed w/err 0x80010119

This is the code that works under 4.5.2 but not any more under 4.8

public Service()
{
    // Initialize COM security
    UInt32 hResult = COMNative.CoInitializeSecurity(
    IntPtr.Zero,    // Add your security descriptor here
    -1,
    IntPtr.Zero,
    IntPtr.Zero,
    COMNative.RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
    COMNative.RPC_C_IMP_LEVEL_IDENTIFY,
    IntPtr.Zero,
    COMNative.EOAC_DISABLE_AAA | COMNative.EOAC_SECURE_REFS | 
    COMNative.EOAC_NO_CUSTOM_MARSHAL,
    IntPtr.Zero);

    if (hResult != 0)
        throw new ApplicationException(
            "CoIntializeSecurity failed w/err 0x" + hResult.ToString("X"));
}

/// <summary>
/// Called when [start].
/// </summary>
/// <param name="args">The arguments.</param>
public void OnStart(string serviceName)
{
    ILogger logger = LoggerService.GetLogger();
    logger.Info("*** START OF SERVICE ***");
    logger.Info(serviceName + " (Version " + Assembly.GetExecutingAssembly().GetName().Version.ToString() + ") starting ....");

    Guid clsidSQLEventObj = new Guid(ComponentClassId);

    // Register the SQLEvent class object on start
    UInt32 hResult = COMNative.CoRegisterClassObject(
                    ref clsidSQLEventObj,
                    new SQLEventClassFactory(),
                    COMNative.CLSCTX_LOCAL_SERVER,
                    COMNative.REGCLS_MULTIPLEUSE,
                    out _cookieSQLEventObj);

   if (hResult != 0)
       throw new ApplicationException(
                "CoRegisterClassObject failed w/err 0x" + hResult.ToString("X"));
    
}

We already tried multiple security settings without any luck.

Has anyone got an idea what is the difference between 4.5.2 and 4.8 in this case why it's not working any more?

c#
.net-4.5
.net-4.8
asked on Stack Overflow Nov 3, 2020 by Patrick • edited Nov 3, 2020 by Patrick

1 Answer

0

I finally managed to solve this issue. I moved the CoInitialize from the service class constructor to the Main method and set this to be the first thing to do when starting and that solved the issue.

It looks like there is a timing difference in the initialization of the service constructor when using 4.5.2 and 4.8. I searched a lot but couldn't find what exactly is changed in this between these two versions.

If someone has knowledge of this I'm still interested to get this known ;-)

answered on Stack Overflow Nov 4, 2020 by Patrick

User contributions licensed under CC BY-SA 3.0