|
| 1 | +# Develop, test, and deploy an Azure Function with Visual Studio |
| 2 | + |
| 3 | +## Create and test a simple Azure Function locally with Visual Studio |
| 4 | + |
| 5 | +## Modify Visual Studio Install |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +2. The Modifying - Visual Studio page appears. |
| 10 | +  |
| 11 | + |
| 12 | +## Azure Function App |
| 13 | + |
| 14 | +``` |
| 15 | +public static class Function1 |
| 16 | +{ |
| 17 | + [FunctionName("Function1")] |
| 18 | + public static async Task<IActionResult> Run( |
| 19 | + [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, |
| 20 | + ILogger log) |
| 21 | + { |
| 22 | + log.LogInformation("C# HTTP trigger function processed a request."); |
| 23 | +
|
| 24 | + string name = req.Query["name"]; |
| 25 | +
|
| 26 | + string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); |
| 27 | + dynamic data = JsonConvert.DeserializeObject(requestBody); |
| 28 | + name = name ?? data?.name; |
| 29 | +
|
| 30 | + return name != null |
| 31 | + ? (ActionResult)new OkObjectResult($"Hello, {name}") |
| 32 | + : new BadRequestObjectResult("Please pass a name on the query string or in the request body"); |
| 33 | + } |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +``` |
| 38 | +public static class Function2 |
| 39 | +{ |
| 40 | + [FunctionName("Function2")] |
| 41 | + public static void Run([BlobTrigger("samples-workitems/{name}", Connection = "xxxxxxxxxxxxxxxxxxxxxxx")]Stream myBlob, string name, ILogger log) |
| 42 | + { |
| 43 | + log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes"); |
| 44 | + } |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +## Exercise - Create and test a simple Azure Function locally with Visual Studio |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +``` |
| 59 | +namespace WatchPortalFunction |
| 60 | +{ |
| 61 | + public static class Function1 |
| 62 | + { |
| 63 | + [FunctionName("Function1")] |
| 64 | + public static async Task<IActionResult> Run( |
| 65 | + [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, |
| 66 | + ILogger log) |
| 67 | + { |
| 68 | + log.LogInformation("C# HTTP trigger function processed a request."); |
| 69 | +
|
| 70 | + string name = req.Query["name"]; |
| 71 | +
|
| 72 | + string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); |
| 73 | + dynamic data = JsonConvert.DeserializeObject(requestBody); |
| 74 | + name = name ?? data?.name; |
| 75 | +
|
| 76 | + return name != null |
| 77 | + ? (ActionResult)new OkObjectResult($"Hello, {name}") |
| 78 | + : new BadRequestObjectResult("Please pass a name on the query string or in the request body"); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +## Create the WatchInfo Azure Function |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | +``` |
| 91 | +namespace WatchPortalFunction |
| 92 | +{ |
| 93 | + public static class WatchInfo |
| 94 | + { |
| 95 | + [FunctionName("WatchInfo")] |
| 96 | + public static async Task<IActionResult> Run( |
| 97 | + [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, |
| 98 | + ILogger log) |
| 99 | + { |
| 100 | + log.LogInformation("C# HTTP trigger function processed a request."); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +``` |
| 107 | +// Retrieve the model id from the query string |
| 108 | +string model = req.Query["model"]; |
| 109 | +
|
| 110 | +// If the user specified a model id, find the details of the model of watch |
| 111 | +if (model != null) |
| 112 | +{ |
| 113 | + // Use dummy data for this example |
| 114 | + dynamic watchinfo = new { Manufacturer = "abc", CaseType = "Solid", Bezel = "Titanium", Dial = "Roman", CaseFinish = "Silver", Jewels = 15 }; |
| 115 | +
|
| 116 | + return (ActionResult)new OkObjectResult($"Watch Details: {watchinfo.Manufacturer}, {watchinfo.CaseType}, {watchinfo.Bezel}, {watchinfo.Dial}, {watchinfo.CaseFinish}, {watchinfo.Jewels}"); |
| 117 | +} |
| 118 | +return new BadRequestObjectResult("Please provide a watch model in the query string"); |
| 119 | +``` |
| 120 | + |
| 121 | +## Test the Azure Function locally |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | +## Publish a simple Azure Function |
| 129 | + |
| 130 | +- Bitbucket |
| 131 | +- Dropbox |
| 132 | +- External repository (Git or Mercurial) |
| 133 | +- Git local repository |
| 134 | +- GitHub |
| 135 | +- OneDrive |
| 136 | +- Azure DevOps |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | +## Zip deployment |
| 141 | + |
| 142 | +``` |
| 143 | +az functionapp deployment source config-zip -g <resource-group> -n <function-app-name> --src <zip-file> |
| 144 | +``` |
| 145 | + |
| 146 | +## Create an Azure Function App using the Azure portal |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | +## Deploy the WatchInfo function to the Azure Function App |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | + |
| 160 | + |
| 161 | +## Verify the functions have been deployed |
| 162 | + |
| 163 | + |
| 164 | + |
| 165 | + |
| 166 | + |
| 167 | + |
| 168 | +## Exercise - Unit test an Azure Function |
| 169 | + |
| 170 | +### Create a unit test project |
| 171 | + |
| 172 | + |
| 173 | + |
| 174 | + |
0 commit comments