In the attached video I demonstrate a case where Python.NET code is running while ArcGIS.Core is initialized. Code fails on some commands. This is probably due to the internal implementation of ArcGISPro.Core. It is either using Python.NET or uses a different Python to C++ marshaling.
https://www.dropbox.com/s/dkndivr6p4ygqai/esr-python-collision.mp4?dl=0
The exception is of a generic undefined error.
“External component has thrown an exception. System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception.”
It seems that Host.Initialize() gets control over all the threads, while Python is attempting to do the same.
Python.Net allows users to run python code form .net environment. I use “pythonnet_netstandard_py37_win” Nuget package which is in full compatibility with ArcGIS pro 2.7 (and python 3.7 ).
The same code runs successfully end to end when Host.Initialize() is not running in the same application.
Failed Attempts:
· Creating a new thread in STA and MTA compartments prior
· Using Default and Current TaskSchedulers.
· Creating a dedicated TaskFactory before init
· Creating a dedicated TaskFactory after init
The collision is happening on C++ authorization level. Thus has to be addressed by the ESRI team.
I have created a repository to replicate the issue both in running and not running state is provided here
gis-geodata/arc-gispro-python-net-licensing-collision (github.com)
To replicate the exceptions comment out this line
Full StackTrace
System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception.
at Python.Runtime.Runtime.PyObject_Call(IntPtr pointer, IntPtr args, IntPtr kw)
at Python.Runtime.PyObject.Invoke(PyTuple args, PyDict kw)
at Python.Runtime.PyObject.InvokeMethod(String name, PyTuple args, PyDict kw)
at Python.Runtime.PyObject.TryInvokeMember(InvokeMemberBinder binder, Object[] args, Object& result)
at CallSite.Target(Closure , CallSite , Object , PyString )
at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
at Cadaster.Gateway.EnvironmentConfiguration.PythonScripts.CreateCityUser(String cityName, Object arcpy, DataBase database) in I:\Cadaster\Gateway\EnvironmentConfiguration\PythonScripts.cs:line 108