1

I am having an Http triggered azure function which I am able to debug successfully using Postman by calling the api in postman and getting the desired response. Now I am trying to debug by calling the api from a separate method and that I am doing by calling the api in the main function something like this: Azure Function:

 public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            

            log.LogInformation("C# HTTP trigger function processed a request.");

            var requestBody = await new StreamReader(req.Body).ReadToEndAsync();

            return new OkObjectResult("Test");

        }
    }
 class Program
        {
             static async Task Main(string[] args)
        {
            HttpClient client = new HttpClient();
            var requestUri = new Uri("http://localhost:7071/api/Function1");
            var rec = "Test:test";
            var response = await client.PostAsJsonAsync(requestUri, rec);
        }}

When I run I get an error in the last line saying "target machine actively refused the connection". The url is same which I am passing in postman and when I do from postman, it hits my function method. What wrong am I doing here??

Note: I have not yet published my function as I am debugging locally now

19
  • Offtopic: You should be using async methods. You should never make a blocking call to .Result. Commented Jan 23, 2021 at 15:02
  • How are you running the Azure Function locally? Are you absolutely certain that your another process on your same machine is listening to http://localhost:7221 and that it isn't https: and a different port number? You do know how localhost connections work, right? Commented Jan 23, 2021 at 15:04
  • Also, if this is a UWP application you're building are you aware that localhost client connections are blocked by default on Windows? Commented Jan 23, 2021 at 15:04
  • "I removed async as this is my main method and it wont allow async method" - but Main can be async! - where are you getting that incorrect information from? Commented Jan 23, 2021 at 15:04
  • 1
    On a side note, should var rec = "Test:test"; this be defined as var rec = new { Test = "test" }; to represent as an object? Commented Jan 23, 2021 at 15:28

1 Answer 1

1

When I run I get an error in the last line saying "target machine actively refused the connection". The url is same which I am passing in postman and when I do from postman, it hits my function method. What wrong am I doing here??

First of all, please make sure your function runtime is still running.

And below code works fine on my side:

using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            HttpClient client = new HttpClient();
            var requestUri = new Uri("http://localhost:7071/api/Function1");
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
            request.Method = "POST";
            string contents = "{\"Test\":\"test\"}";

            byte[] content = Encoding.UTF8.GetBytes(contents);
            int contentlength = content.Length;
            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(content, 0, (int)contentlength);
            }
            using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
            {
                StreamReader reader = new StreamReader(resp.GetResponseStream());
                string text = reader.ReadToEnd();
                Console.WriteLine(text);
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.