Skip to content

Commit 632206e

Browse files
committed
add function sample
1 parent 01b9820 commit 632206e

15 files changed

+224
-5
lines changed

Diff for: .gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
bin/
2-
obj/
2+
obj/
3+
.vs/
4+
.vscode/

Diff for: samples/ChatRoom/Constants.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ namespace Microsoft.Azure.SignalR.Samples.ChatRoom
55
{
66
internal static class Constants
77
{
8-
public const string AzureSignalRConnectionStringKey = "AzureSignalR:ConnectionString";
8+
public const string AzureSignalRConnectionStringKey = "AzureSignalRConnectionString";
99
}
1010
}

Diff for: samples/ChatRoom/Dockerfile

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
FROM microsoft/aspnetcore-build:2.0 AS build-env
2+
WORKDIR /app
3+
4+
# copy csproj and restore as distinct layers
5+
COPY NuGet.config ./
6+
RUN mkdir ChatRoom && cd ChatRoom/
7+
COPY *.csproj ./
8+
RUN dotnet restore
9+
10+
# copy everything else and build
11+
COPY ./ ./
12+
RUN dotnet publish -c Release -o out
13+
14+
# build runtime image
15+
FROM microsoft/aspnetcore:2.0
16+
WORKDIR /app
17+
COPY --from=build-env /app/out .
18+
ENTRYPOINT ["dotnet", "ChatRoom.dll"]
File renamed without changes.

Diff for: samples/ChatRoomLocal/NuGet.config

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<packageSources>
4+
<add key="azure-signalr-dev" value="https://www.myget.org/F/azure-signalr-dev/api/v3/index.json" />
5+
<add key="aspnetcore-dev" value="https://dotnet.myget.org/F/aspnetcore-dev/api/v3/index.json" />
6+
</packageSources>
7+
</configuration>

Diff for: samples/Timer/NuGet.config

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<packageSources>
4+
<add key="azure-signalr-dev" value="https://www.myget.org/F/azure-signalr-dev/api/v3/index.json" />
5+
<add key="aspnetcore-dev" value="https://dotnet.myget.org/F/aspnetcore-dev/api/v3/index.json" />
6+
</packageSources>
7+
</configuration>

Diff for: samples/Timer/Timer.csproj

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net461</TargetFramework>
4+
</PropertyGroup>
5+
<ItemGroup>
6+
<PackageReference Include="Microsoft.Azure.WebJobs" Version="2.1.0" />
7+
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="2.1.0" />
8+
<PackageReference Include="Microsoft.Azure.SignalR" Version="1.0.0-preview1-t000" />
9+
</ItemGroup>
10+
<ItemGroup>
11+
<Reference Include="Microsoft.CSharp" />
12+
</ItemGroup>
13+
<ItemGroup>
14+
<None Update="host.json">
15+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
16+
</None>
17+
<None Update="TimerFunction\function.json">
18+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
19+
</None>
20+
<None Update="local.settings.json">
21+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
22+
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
23+
</None>
24+
</ItemGroup>
25+
</Project>

Diff for: samples/Timer/Timer.sln

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.27004.2005
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Timer", "Timer.csproj", "{AEA659B8-C88F-4B8C-BE79-ACBDCF273A2C}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{AEA659B8-C88F-4B8C-BE79-ACBDCF273A2C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{AEA659B8-C88F-4B8C-BE79-ACBDCF273A2C}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{AEA659B8-C88F-4B8C-BE79-ACBDCF273A2C}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{AEA659B8-C88F-4B8C-BE79-ACBDCF273A2C}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {8E6B0EDC-0DFF-4545-BED0-1FB3A4C0E940}
24+
EndGlobalSection
25+
EndGlobal

Diff for: samples/Timer/TimerFunction.cs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace Timer
2+
{
3+
using System;
4+
using System.Threading.Tasks;
5+
using Microsoft.Azure.WebJobs;
6+
using Microsoft.Azure.WebJobs.Host;
7+
using Microsoft.Azure.SignalR;
8+
9+
public static class TimerFunction
10+
{
11+
public static async Task Run(TimerInfo myTimer, TraceWriter log)
12+
{
13+
var connectionString = Environment.GetEnvironmentVariable("AzureSignalRConnectionString");
14+
var proxy = SignalRService.CreateHubProxy(connectionString, "chat");
15+
await proxy.All.InvokeAsync("broadcastMessage", new object[] { "_BROADCAST_", $"Current time is: {DateTime.Now}" });
16+
}
17+
}
18+
}

Diff for: samples/Timer/TimerFunction/function.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"bindings": [
3+
{
4+
"type": "timerTrigger",
5+
"schedule": "0 * * * * *",
6+
"useMonitor": true,
7+
"runOnStartup": false,
8+
"name": "myTimer"
9+
}
10+
],
11+
"disabled": false,
12+
"scriptFile": "../Timer.dll",
13+
"entryPoint": "Timer.TimerFunction.Run"
14+
}

Diff for: samples/Timer/host.json

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{
2+
}

Diff for: samples/Timer/local.settings.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"IsEncrypted": false,
3+
"Values": {
4+
"AzureWebJobsStorage": "",
5+
"AzureWebJobsDashboard": ""
6+
}
7+
}

Diff for: tutorials/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ Here're a list of tutorials that help you learn Azure SignalR Service:
44

55
* [Get Started with SignalR: a Chat Room Example](chat-room.md)
66
* [Build Your First Azure SignalR Service Application](chat-room-service.md)
7+
* [Integrate with Azure Services](azure-integration.md)

Diff for: tutorials/azure-integration.md

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Integrate with Azure Services
2+
3+
In last tutorial you have learned how to use Azure SignalR Service in your application, but you still need to host the hub implementation on a web server.
4+
In this tutorial you'll learn how to use Azure web app to host your hub logic and also integrate with serverless applications like Azure functions.
5+
6+
## Deploy SignalR Hub to Azure Web App
7+
8+
Azure Web App is a service for hosting web applications, which is a perfect choice for hosting our SignalR hub.
9+
Azure Web App supports container, so we will build our application into a Docker container and deploy it to web app.
10+
11+
### Build Docker Image
12+
13+
First use the [Dockerfile](../samples/ChatRoom/Dockerfile) to build our application into a Docker container image:
14+
15+
```
16+
docker build -t chatroom .
17+
```
18+
19+
Let's take a look at the details of the Dockerfile.
20+
21+
First copy the source code, restore, build and publish the app:
22+
23+
```docker
24+
# copy csproj and restore as distinct layers
25+
COPY NuGet.config ./
26+
RUN mkdir ChatRoom && cd ChatRoom/
27+
COPY *.csproj ./
28+
RUN dotnet restore
29+
30+
# copy everything else and build
31+
COPY ./ ./
32+
RUN dotnet publish -c Release -o out
33+
```
34+
35+
Then copy the build output into `app` folder and set the entrypoint:
36+
```docker
37+
# build runtime image
38+
FROM microsoft/aspnetcore:2.0
39+
WORKDIR /app
40+
COPY --from=build-env /app/out .
41+
ENTRYPOINT ["dotnet", "ChatRoom.dll"]
42+
```
43+
44+
Then you can test the image locally:
45+
46+
```
47+
docker run -p 5000:5000 -e AzureSignalRConnectionString=<connection_string> chatroom
48+
```
49+
50+
> For more information about building Docker image for .NET Core, please refer to this [doc](https://docs.microsoft.com/en-us/dotnet/core/docker/building-net-docker-images).
51+
52+
After you test the image, push it to a Docker register (here we use [Azure Container Registry](https://azure.microsoft.com/en-us/services/container-registry/), you can also use others like DockerHub):
53+
54+
```
55+
docker login <acr_name>.azurecr.io
56+
docker tag chatdemo <acr_name>.azurecr.io/chatdemo
57+
docker push <acr_name>.azurecr.io/chatdemo
58+
```
59+
60+
### Deploy to Azure Web App
61+
62+
First create an Azure Web App:
63+
64+
```
65+
az group create --name <resource_group_name> --location "East US"
66+
az appservice plan create --name <plan_name> --resource-group <resource_group_name> --sku S1 --is-linux
67+
az webapp create \
68+
--resource-group <resource_group_name> --plan <plan_name> --name <app_name> \
69+
--deployment-container-image-name nginx
70+
```
71+
72+
This creates a web app with nginx image.
73+
74+
> This tutorial uses [Azure CLI](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest) to deploy to Azure, make sure you have Azure CLI installed first.
75+
76+
77+
Then update the web app with the chat room image:
78+
```
79+
az webapp config container set \
80+
--resource-group <resource_group_name> --name <app_name> \
81+
--docker-custom-image-name <acr_name>.azurecr.io/chatroom \
82+
--docker-registry-server-url https://<acr_name>.azurecr.io \
83+
--docker-registry-server-user <acr_name> \
84+
--docker-registry-server-password <acr_password>
85+
az webapp config appsettings set --resource-group <resource_group_name> --name <app_name> --setting PORT=5000
86+
az webapp config appsettings set --resource-group <resource_group_name> --name <app_name> --setting AzureSignalRConnectionString=<connection_string>
87+
```
88+
89+
Now open `http://<app_name>.azurewebsites.net` and you will see your chat room running on Azure.
90+
91+
> Since https is not supported on SignalR yet, it's required to use http to access the web site (and chrome has some issues in sending data over non secure web site, you have to use Edge for now). This issue should be gone after https support is added.
92+
93+
## Integrate with Azure Functions

Diff for: tutorials/chat-room-service.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Then let's update the chat room sample to use the new service you just created.
4141

4242
The full sample code can be found [here](../samples/ChatRoom/). Let's look at the key changes:
4343

44-
1. In [Startup.cs](../samples/ChatRoom/Startup.cs), instead of calling `AddSignalR()` and `UseSignalR()`, you need to call `AddSignalRService()` and `UseSignalRService()` and pass in connection string to make the application connect the service instead of hosting SignalR by itself.
44+
1. In [Startup.cs](../samples/ChatRoom/Startup.cs), instead of calling `AddSignalR()` and `UseSignalR()`, you need to call `AddSignalRService()` and `UseSignalRService()` and pass in connection string to make the application connect to the service instead of hosting SignalR by itself.
4545

4646
```cs
4747
public void ConfigureServices(IServiceCollection services)
@@ -71,7 +71,7 @@ The full sample code can be found [here](../samples/ChatRoom/). Let's look at th
7171

7272
The connection string is used for server code to connect to the service. For client, usually there are additional authentication needed, so Azure SignalR Service gives you the flexibility to implement your own authentication. A client doesn't directly connect to the service using connection string, instead your application need to implement an API that does the authentication and issues a token to the client. Client then use this token to connect to the service.
7373

74-
In this sample, we do no authentication but directly issue the token. You can implement a real authentication in your own project.
74+
In this sample, we simply do no authentication and directly issue the token. You can implement a real authentication in your own project.
7575

7676
```cs
7777
[HttpGet("{hubName}")]
@@ -138,7 +138,7 @@ Other than these changes, everything else remains the same, you can still use th
138138
Now let's build and run the app (you need to first set connect string as an environment variable).
139139

140140
```
141-
export AzureSignalR:ConnectionString=<connection_string>
141+
export AzureSignalRConnectionString=<connection_string>
142142
dotnet build
143143
dotnet run
144144
```

0 commit comments

Comments
 (0)