0

For the first time, I'm trying to develop an HTTP triggered Azure Function via VS Code. I do not altered the initial function code, it is as it was created and nothing more. I followed the Microsoft doc for this subject and I thaught I did it the same way but the function doesn't work so I can easily guess I'm wrong.

I installed VS Code extensions (Azure Tools), Azure Function Core Tools, I even tryied to reinstall Python 3.10.11 version. Normally, I have all the files required :

enter image description here

host.json params :

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[3.*, 4.0.0)"
  }
}

local.settings.json params :

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "connection_string",
    "FUNCTIONS_WORKER_RUNTIME": "python"
  }
}

function.json params :

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    }
  ]
}

When I try "Execute Function Now", I get this as an output :

10:35:24: Error: Failed to connect. Make sure your project is running locally.

When I debug it, I get this :

Found Python version 3.10.11 (py).

Azure Functions Core Tools  
Core Tools Version:       4.0.5198 Commit hash: N/A  (64-bit)  
Function Runtime Version: 4.21.1.20667

Functions:
MyFirstAzureFunction: \[GET,POST\] http://localhost:7071/api/MyFirstAzureFunction

[2023-06-09T07:22:24.140Z] 2.18s - Could not connect to 127.0.0.1: 53444
[2023-06-09T07:22:24.144Z] Traceback (most recent call last): 
[2023-06-09T07:22:24.146Z]   File "c:.\.vscode\extensions\ms-python.python-2023.10.0\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_comm.py", line 493, in start_client
[2023-06-09T07:22:24.147Z]     s.connect((host, port)) [2023-06-09T07:22:24.149Z] ConnectionRefusedError: [WinError 10061] No connection could be established because the target computer expressly refused it
[2023-06-09T07:22:24.150Z] Could not connect to 127.0.0.1: 53444 ...
[2023-06-09T07:22:24.723Z] PublishSpecializationCompleteEvent failed with System.InvalidOperationException: An invalid request URI was provided. Either the request URI must be an absolute URI or BaseAddress must  be set.
[2023-06-09T07:22:24.724Z]    at System.Net.Http.HttpClient.PrepareRequestMessage(HttpRequestMessage request)
[2023-06-09T07:22:24.725Z]    at System.Net.Http.HttpClient.CheckRequestBeforeSend(HttpRequestMessage request)
[2023-06-09T07:22:24.725Z]    at System.Net.Http.HttpClient.SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
[2023-06-09T07:22:24.726Z]    at System.Net.Http.HttpClient.SendAsync(HttpRequestMessage request)
[2023-06-09T07:22:24.726Z]    at Microsoft.Azure.WebJobs.Script.WebHost.Management.MeshServiceClient.SendAsync(IEnumerable`1 formData) in /_/src/WebJobs.Script.WebHost/Management/MeshServiceClient.cs:line 154  
[2023-06-09T07:22:24.727Z]    at Microsoft.Azure.WebJobs.Script.WebHost.Management.MeshServiceClient.NotifyHealthEvent(ContainerHealthEventType healthEventType, Type source, String details) in /_/src/WebJobs.Script.WebHost/Management/MeshServiceClient.cs:line 104
[2023-06-09T07:22:24.727Z]    at Microsoft.Azure.WebJobs.Script.WebHost.ContainerManagement.LinuxContainerActivityPublisher.PublishSpecializationCompleteEvent() in /\_/src/WebJobs.Script.WebHost/ContainerManagement/LinuxContainerActivityPublisher.cs:line 122
[2023-06-09T07:23:20.598Z] Starting worker process failed
[2023-06-09T07:23:20.600Z] Microsoft.Azure.WebJobs.Script.Grpc: The operation has timed out.
[2023-06-09T07:23:20.623Z] Failed to start a new language worker for runtime: python.
[2023-06-09T07:23:20.780Z] PublishContainerActivity
[2023-06-09T07:23:20.781Z] System.Net.Http: An invalid request URI was provided. Either the request URI must be an absolute URI or BaseAddress must be set.
[2023-06-09T07:23:21.597Z] Failed to publish status to /memoryactivity
[2023-06-09T07:23:21.598Z] System.Private.Uri: Invalid URI: The hostname could not be parsed.
[2023-06-09T07:23:22.749Z] Language Worker Process exited. Pid=10368.
[2023-06-09T07:23:22.750Z] py exited with code -1073741819 (0xC0000005). .
[2023-06-09T07:23:34.664Z] Language Worker Process exited. Pid=2652.
[2023-06-09T07:23:34.667Z] py exited with code -1073741819 (0xC0000005). .\

I tried to connect to the local port 53444 but this also failed. Currently, I do not know what to do and how to fix this.

Grobln
  • 23
  • 6

1 Answers1

0

I followed the given MS Doc, I can able to run the azure functions python code and got the output through the URL,

Code:

import logging
import azure.functions as func

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
    else:
        return func.HttpResponse(
             "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
             status_code=200
        )

host.json:

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[3.*, 4.0.0)"
  }
}

local.settings.json:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "python"
  }
}

function.json:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    }
  ]
}

requirement.txt:

azure-functions

Below is my project structure,

enter image description here

Make sure that the port-7071 , which is used by the Azure Functions Core Tools, is not being used by any other process on your machine.

Run your Azur functions HTTP trigger code like below:-

Click fn + f5 or Click on run > Debug > like below:-

Output:

HTTP trigger function ran successfully as below,

enter image description here

With the above URL, I got the output at browser,

enter image description here

Then, I passed a name in the output URL,

enter image description here

Output:

It shows succeded as below,

enter image description here

Dasari Kamali
  • 811
  • 2
  • 2
  • 6
  • I check if anything is using port 7071 with netstat and in the Resource Monitor but I found nothing. When I run and debug the function, I got this message "connect ECONNREFUSED 127.0.0.1:9091" with a launch.json : { "version": "0.2.0", "configurations": [ { "name": "Attach to Python Functions", "type": "python", "request": "attach", "port": 9091, "preLaunchTask": "func: host start" } ] } I tryied to look if port 9091 was used but same thing, it's not. – Grobln Jun 09 '23 at 10:04
  • I'm working with a Windows OS, could it be the problem beacause I don't use Linux ? – Grobln Jun 09 '23 at 10:38
  • 1
    @Grobln No, using Windows as your operating system should not be the cause of the problem. Azure Functions supports development on both Windows and Linux environments. – Dasari Kamali Jun 09 '23 at 10:41
  • 1
    @Grobln Try changing the launch.json as [this](https://stackoverflow.com/questions/68708788/running-azure-functions-with-vs-code-instanlty-fails-with-econnrefused). – Dasari Kamali Jun 09 '23 at 10:44
  • @Grobln Have you got the answer without any issues? – Dasari Kamali Jun 12 '23 at 11:15
  • I am using Python 3.10.0. Try it again by downloading the Python version through this link below, https://www.python.org/downloads/release/python-3100/ – Dasari Kamali Jun 12 '23 at 12:01
  • I ask an other question just here[link](https://stackoverflow.com/questions/76455222/getting-error-connect-econnrefused-127-0-0-19091-while-running-azure-function/76456275?noredirect=1#comment134812713_76456275) because they deleted my answer. I had more details and screens. – Grobln Jun 12 '23 at 12:46
  • I tried to downgrade my python version but nothing changed – Grobln Jun 12 '23 at 12:46
  • Ok, what's the error you getting still? – Dasari Kamali Jun 12 '23 at 13:39
  • I'm still getting "connect ECONNREFUSED 127.0.0.1:9091". I dont know if it can be an explanation but my Azure account and my Microsoft account have not the same email address – Grobln Jun 12 '23 at 14:07
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/254053/discussion-between-dasari-kamali-and-grobln). – Dasari Kamali Jun 13 '23 at 04:22