-1

I have a Azure DevOps Function which creates a new team in devOps. I'm extending the code to create Repositories and Files and want to have the different parts divided into methods to have it more streamlined.

I would really appreciate it if someone can guide me how I can create methods inside an Azure function and call them as I'm quiet new to C#.

I am using Visual Studio with .NET Core 3.0.

Here is the code so far:

[FunctionName("Function2")]
        public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req)
        {
            var PAT = "";

            using (HttpClient client = new HttpClient())
            {

                client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));

                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                    Convert.ToBase64String(
                        ASCIIEncoding.ASCII.GetBytes(
                            string.Format("{0}:{1}", "", PAT))));

                // Create Team
                var teambody = new
                {
                   name = "myteamname",

                };

                //Connecting to the DevOps REST API
                var requestMessage = new HttpRequestMessage(HttpMethod.Post, $"https://dev.azure.com/{}/_apis/projects/{}/teams?api-version=6.0");
                requestMessage.Content = new StringContent(JsonConvert.SerializeObject(teambody), Encoding.UTF8, "application/json");

                //Reading Server Response
                using (HttpResponseMessage response = await client.SendAsync(requestMessage))
                {
                    if (!response.IsSuccessStatusCode)
                    {
                        
                        response.EnsureSuccessStatusCode();
                    }

                    return req.CreateResponse(HttpStatusCode.Created,"Teams created successfully!");
                }
                
              // Create Repository

              // Create Files

            }
        }

melody
  • 25
  • 9
  • Azure Functions are implemented in a class, adding methods is no different than any other (static) class. – Crowcoder Apr 27 '21 at 14:05

1 Answers1

0

You can add the methods under your function class(which on the same level with public static async Task<HttpResponseMessage> Run.....).

For example:

namespace FunctionApp2
{
    public static class Function2
    {
        [FunctionName("Function2")]
        public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req)
        {
            //use your method
            string name = Method(name);
            .............
        }

        //add your method here
        public static string Method(string name)
        {
            return name+"abc";
        }
    }
}
Hury Shen
  • 14,948
  • 1
  • 9
  • 18