Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
90 views
in Technique[技术] by (71.8m points)

c# - way of Debugging in Azure function

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

question from:https://stackoverflow.com/questions/65860682/way-of-debugging-in-azure-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
            }
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...