Getting Started With Microsoft Azure Functions Using Visual Studio 2017

 Introduction

Azure Functions is a solution for easily running small pieces of code in the cloud. We can create, execute, and test our custom logic function without creating a VM or web applications and also without needing to install any software or infrastructure to run the function.

In this article, you will learn to create, test, debug and deploy azure functions using Visual Studio 2017. Azure Functions tools are available as part of the Azure development workload in Visual Studio 2017 version 15.3, or a later version.

Azure Functions

Prerequisites

  1. Download and Install Visual 2017 version 15.3 .
  2. If you already Installed Visual Studio 2017, Update Visual Studio from Extensions and updates

Open Visual Studio 2017 > Tools > Extensions and Updates > Select product Updates under the Products category and select Visual studio update 15.3 preview 7.0 version and click on update

Azure Functions

Step 1 Create Azure Functions Project in Visual Studio 2017

Open Visual Studio 2017 Preview 15.3 > Select File >click on New Project (Ctrl +Shift +N) > Select on Visual C# >Click on Cloud > select Azure Functions, type a Name for your project, and click on OK

Azure Functions

Step 2 Create New Function

Azure Functions Solution will generate like below with host.json and locl.settings.json file .

Azure Functions       

In Solution Explorer, right-click on your Azure function project and select Add > New Item. Select Azure Function and click Add.

Azure Functions

Select HttpTrigger as Azure Function type, select Functions for Access Rights, and click Create. The function created is accessed by an HTTP request from any client.

Azure Functions

After clicking on Ok, it will generate a new class with a Static Run method, that is attributed with [FunctionName] attribute. The [FunctionName] attribute indicates that the method is the entry for an Azure Function

  1. using System.Linq;  
  2. using System.Net;  
  3. using System.Net.Http;  
  4. using System.Threading.Tasks;  
  5. using Microsoft.Azure.WebJobs;  
  6. using Microsoft.Azure.WebJobs.Extensions.Http;  
  7. using Microsoft.Azure.WebJobs.Host;  
  8.   
  9. namespace DevEnv_AzureFunctionsDemo  
  10. {  
  11.     public static class DemoFunctions  
  12.     {  
  13.         [FunctionName("DemoFunctions")]  
  14.         public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get""post", Route = null)]HttpRequestMessage req, TraceWriter log)  
  15.         {  
  16.             log.Info("C# HTTP trigger function processed a request.");  
  17.   
  18.             // parse query parameter  
  19.             string name = req.GetQueryNameValuePairs()  
  20.                 .FirstOrDefault(q => string.Compare(q.Key, "name"true) == 0)  
  21.                 .Value;  
  22.   
  23.             // Get request body  
  24.             dynamic data = await req.Content.ReadAsAsync<object>();  
  25.   
  26.             // Set name to query string or body data  
  27.             name = name ?? data?.name;  
  28.   
  29.             return name == null  
  30.                 ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")  
  31.                 : req.CreateResponse(HttpStatusCode.OK, "Hello " + name + "Welcome C# Corner ");  
  32.         }  
  33.     }  

Step 3 Azure Functions Test

You can Press F5 to run, test and debug your Azure function, and if prompted, accept the request from Visual Studio to download and install Azure Functions Core (CLI) tools. You may also need to enable a firewall exception so that the tools can handle HTTP requests.

Copy the Azure function local deploy url on runtime output window

Azure Functions

Append the name parameter to the query string. Use an actual name for the <Enter a name here> placeholder.

https://<Local Host url> /api/<Your Function Name>? name=<Enter a name here>

Paste the URL into your browser, and you should get a response similar to the following.

Azure Functions
If your Function is locally working as expected it means you can initiate publishing the project to Azure.

Step 4 Publish Project to Azure

  • If you don't have an Azure subscription, create a free account before starting to deploy Azure function.
  • In Solution Explorer, right-click the project and select Publish. Choose Create New and then click on Publish.

    Azure Functions
  • If you haven't already connected Visual Studio to your Azure account, click Add an Account and Provide the login details and connect
  • In the Create App Service dialog, provide Hosting settings as specified below

    Azure Functions

Click Create to create a function app in Azure with these settings. After the provisioning is complete, make a note of the Site URL value, which is the address of your function app in Azure.
Azure Functions
Copy the base URL of the function app from the Publish profile page. Replace the localhost:port of the URL you used when testing the function locally with the new base URL. As before, make sure to append the query string with &name=<yourname> to this URL and execute the request.

Azure Functions

Related Article

  1. Getting Started With Azure Functions Using Azure Free Trial Account
  2. How to Create and Test Azure Functions Using Azure Portal

Summary

In this article, you learned about Azure Functions and how to create, test, publish Azure Functions using visual studio 2017.

If you have any questions/ feedback/ issues, please write in the comment box.


Similar Articles