I have a continuous web job that listens for requests containing diagnostic information.
In order to test connectivity I try to hit a health check in my web job but am unable to make requests to localhost per azure app services documentation.
The code below is what I use to verify that I can connect from the application I deploy:
var uri = new Uri("http://localhost:8989/ping");
var response = await client.GetAsync(uri);
I get this exception:
System.Net.Http.HttpRequestException: An error occurred while sending the request.
---> System.Net.WebException: Unable to connect to the remote server
---> System.Net.Sockets.SocketException: An attempt was made to access a socket in a way forbidden by its access permissions 127.0.0.1:8989
The web job is installed via a site extension install script through Kudu (SCM), which means that the web job is ultimately a child process of Kudu (SCM). The web job application on startup binds itself to port 8989. Starting the application locally on windows, I am able to hit my health check with no problems.
The azure app services documentation says that requests to localhost will fail unless an application within the same sandbox binds to the port (https://github.com/projectkudu/kudu/wiki/Azure-Web-App-sandbox#local-address-requests).
The azure app services documentation states that Kudu runs in the same sandbox as the main application (https://github.com/projectkudu/kudu/wiki/Kudu-architecture#security-model).
How do I enable communication with my web job via http?
Preferably it would be something that I could do from a site extension install process, but any options are good.
Update 12-26-2019:
I have attempted to force SCM and the main application to run in the same sandbox with WEBSITE_DISABLE_SCM_SEPARATION=true
(https://github.com/projectkudu/kudu/wiki/Configurable-settings#use-the-same-process-for-the-user-site-and-the-scm-site).
The documentation states that they already run in the same sandbox and that if a process listens on a port in the same sandbox, those requests should work. Of note, the actual SCM w3wp.exe process has been able to hit localhost with http for my web job. This setting did not seem to improve the situation though.
Update 04-02-2020:
I officially abandoned the idea of using a web job and I now start the process as a child of the main application instance. This allows me to communicate with localhost:8989
with no issue.
Though I now need to manage my own keep alive logic.
I'd still love to know if there is a way to communicate via TCP with a web job if that is ever possible.
WebJobs
since they run inside anIHost
container. Additional details are available in my answer. – Leonilaleonine