Skip to content

Commit cee4037

Browse files
Develop, test, and deploy an Azure Function with Visual Studio
1 parent 10a02d0 commit cee4037

33 files changed

+174
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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+
![alt text](image.png)
8+
9+
2. The Modifying - Visual Studio page appears.
10+
![alt text](image-1.png)
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+
![alt text](image-2.png)
49+
![alt text](image-3.png)
50+
![alt text](image-4.png)
51+
52+
## Exercise - Create and test a simple Azure Function locally with Visual Studio
53+
54+
![alt text](image-5.png)
55+
![alt text](image-6.png)
56+
![alt text](image-7.png)
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+
![alt text](image-8.png)
87+
![alt text](image-9.png)
88+
![alt text](image-10.png)
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+
![alt text](image-11.png)
124+
![alt text](image-12.png)
125+
![alt text](image-13.png)
126+
![alt text](image-14.png)
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+
![alt text](image-15.png)
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+
![alt text](image-16.png)
149+
![alt text](image-17.png)
150+
151+
## Deploy the WatchInfo function to the Azure Function App
152+
153+
![alt text](image-18.png)
154+
155+
![alt text](image-19.png)
156+
![alt text](image-20.png)
157+
![alt text](image-21.png)
158+
![alt text](image-22.png)
159+
![alt text](image-23.png)
160+
161+
## Verify the functions have been deployed
162+
163+
![alt text](image-24.png)
164+
![alt text](image-25.png)
165+
![alt text](image-26.png)
166+
![alt text](image-27.png)
167+
168+
## Exercise - Unit test an Azure Function
169+
170+
### Create a unit test project
171+
172+
![alt text](image-28.png)
173+
![alt text](image-29.png)
174+
![alt text](image-30.png)
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
408 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)