Unable to cast ComObject to EnvironmentManager in SOE

1652
1
Jump to solution
12-17-2018 04:24 AM
PetrDiviš
New Contributor III

Hi, I have a pretty uncertain error when asking for UserInfo from IServerEnvironment from EnvironmentManager class.

System.InvalidCastException: Unable to cast object of type 'System.__ComObject' to type 'ESRI.ArcGIS.esriSystem.EnvironmentManagerClass'.

EnvironmentManager environmentManager = new EnvironmentManager();
UID environmentUid = new UID { Value = "{32d4c328-e473-4615-922c-63c108f55e60}" };
var environment = (IServerEnvironment3)environmentManager.GetEnvironment(environmentUid);

It fails at the first line.

My environment is Server 10.6.1 with Portal and Windows credentials SSO from our domain.

Sometimes it works, most of the time it fails. I'm pretty desperate, I hate these uncertain COM errors.

0 Kudos
1 Solution

Accepted Solutions
pfoppe
by MVP
MVP

Hi Petr Diviš

We recently ran into this as well with a SOE developed in VB .Net for v10.4.1 (and confirmed it was also working in 10.5.1).  We found a resolution detailed below

Here was the original code functioning in 10.4.1/10.5.1:

    Friend Function GetCurrentUser() As String
        Try

            Dim myGUID As Guid = Marshal.GenerateGuidForType(GetType(IServerEnvironment))
            Dim myUID As New ESRI.ArcGIS.esriSystem.UID
            myUID.Value = myGUID.ToString("B")
            Dim pEnvMgr As IEnvironmentManager = New EnvironmentManager
            Dim pServEnv As IServerEnvironment2 = pEnvMgr.GetEnvironment(myUID)

            Dim sUser As String = ""
            sUser = pServEnv.UserInfo.Name
            Return sUser

        Catch ex As Exception
            Return "Current user not found."
        End Try
    End Function

And was throwing the following exception for line 7: 

 Unable to cast object of type 'System.__ComObject' to type 'ESRI.ArcGIS.esriSystem.EnvironmentManagerClass'

A colleague of mine found this BUG - BUG-000102447: The EnvironmentManager singleton object in the Serve.. 

And we translated that to VB .Net: 

    Friend Function GetCurrentUser() As String
        Try

            Dim myGUID As Guid = Marshal.GenerateGuidForType(GetType(IServerEnvironment))
            Dim myUID As New ESRI.ArcGIS.esriSystem.UID
            myUID.Value = myGUID.ToString("B")

            ' Issues with original 10.5.1 code implementation - https://support.esri.com/en/technical-article/000011331
            'Dim pEnvMgr As IEnvironmentManager = New EnvironmentManager()
            Dim t As Type = Type.GetTypeFromProgID("esriSystem.EnvironmentManager")
            Dim obj As System.Object = Activator.CreateInstance(t)
            Dim pEnvMgr As IEnvironmentManager = obj
            Dim pServEnv As IServerEnvironment2 = pEnvMgr.GetEnvironment(myUID)

            Dim sUser As String = ""
            sUser = pServEnv.UserInfo.Name
            Return sUser

        Catch ex As Exception
            Return "Current user not found... " & ex.ToString()

After a re-build, deploy, test/debug session we confirmed our logged in user information was available again in the SOE. 

*whew* ... we were quite worried we found a critical show stopper to our 10.6.1 upgrade efforts as getting the logged in user is critical for a few of our SOE's

Hope this helps.  

View solution in original post

1 Reply
pfoppe
by MVP
MVP

Hi Petr Diviš

We recently ran into this as well with a SOE developed in VB .Net for v10.4.1 (and confirmed it was also working in 10.5.1).  We found a resolution detailed below

Here was the original code functioning in 10.4.1/10.5.1:

    Friend Function GetCurrentUser() As String
        Try

            Dim myGUID As Guid = Marshal.GenerateGuidForType(GetType(IServerEnvironment))
            Dim myUID As New ESRI.ArcGIS.esriSystem.UID
            myUID.Value = myGUID.ToString("B")
            Dim pEnvMgr As IEnvironmentManager = New EnvironmentManager
            Dim pServEnv As IServerEnvironment2 = pEnvMgr.GetEnvironment(myUID)

            Dim sUser As String = ""
            sUser = pServEnv.UserInfo.Name
            Return sUser

        Catch ex As Exception
            Return "Current user not found."
        End Try
    End Function

And was throwing the following exception for line 7: 

 Unable to cast object of type 'System.__ComObject' to type 'ESRI.ArcGIS.esriSystem.EnvironmentManagerClass'

A colleague of mine found this BUG - BUG-000102447: The EnvironmentManager singleton object in the Serve.. 

And we translated that to VB .Net: 

    Friend Function GetCurrentUser() As String
        Try

            Dim myGUID As Guid = Marshal.GenerateGuidForType(GetType(IServerEnvironment))
            Dim myUID As New ESRI.ArcGIS.esriSystem.UID
            myUID.Value = myGUID.ToString("B")

            ' Issues with original 10.5.1 code implementation - https://support.esri.com/en/technical-article/000011331
            'Dim pEnvMgr As IEnvironmentManager = New EnvironmentManager()
            Dim t As Type = Type.GetTypeFromProgID("esriSystem.EnvironmentManager")
            Dim obj As System.Object = Activator.CreateInstance(t)
            Dim pEnvMgr As IEnvironmentManager = obj
            Dim pServEnv As IServerEnvironment2 = pEnvMgr.GetEnvironment(myUID)

            Dim sUser As String = ""
            sUser = pServEnv.UserInfo.Name
            Return sUser

        Catch ex As Exception
            Return "Current user not found... " & ex.ToString()

After a re-build, deploy, test/debug session we confirmed our logged in user information was available again in the SOE. 

*whew* ... we were quite worried we found a critical show stopper to our 10.6.1 upgrade efforts as getting the logged in user is critical for a few of our SOE's

Hope this helps.