Import error while calling from IIS hosted .NET Core API

Question:

I am facing the below issue while running a python script from a .NET Core api which is hosted in windows server IIS .I am using pyrfc 1.9.5 SAP connector in this script. Here is the code below

from pyrfc import Connection
def GetConnection(connmeta):
   return Connection(**connmeta)

Here is the error which I got –

from pyrfc import Connection
  File "<C:Program FilesPython35libsite-packagespyrfc-1.9.5-py3.5-win-amd64.eggpyrfc_init_.py>", line 22, in 
    from pyrfc._pyrfc import get_nwrfclib_version, Connection, TypeDescription, FunctionDescription, Server
ImportError: DLL load failed: The specified module could not be found

It’s working fine from the command prompt, python IDLE, Powershell etc. Previously I got this error when the visual c++ redistributable package was not installed. But now it is installed properly as the same script is working from IDLE and Powershell.

Here are the installed software and server details –

  • windows server 2016 64 bit
  • Visual C++ 2013 redistributable 64 bit
  • Python 3.5 64 bit,
  • pyrfc 1.9.5 for python 3.5 64 bit (amd64)
  • SAP NW RFC SDK 7.5.0 64 bit

Python executable and the path to the lib folder of the SDK is already added to the Environment variable. I have also tried to execute the sample rfcexec.exe program from the bin directory it’s working fine. I am only getting the error while running the application from IIS. The app pool identity has full permission on the python scripts.

The script is working fine from IIS if I comment the pyrfc import part.
Please help .

Asked By: Niladri

||

Answers:

Finally I have found the solution after debugging for atleast 1 week. The problem was access related issue. the SAP NW RFC SDK was installed in the path C:nwrfcsdklib as per the documentation.

link : Installation documentation

But the user account under which the .NET Core API was running in the application pool of IIS didn’t have any read/write access to this nwrfcsdk folder. Hence pyrfc was not able to import the DLL (SAPNWRFC.dll) when the python script was being called from the .NET Core API.I have provided read/write access to that particular account and the script is running fine now .

I have followed the below steps to debug the diagnose the issue –

  • I have used profiling with Procmon.exe from sysinternals to monitor w3wp.exe IIS worker process and python.exe after calling the API

  • I have also used dependency walker to track the dependent dll.

Hope this helps someone who is facing a similar type of issue.

Answered By: Niladri