Skip to content

Commit c07e640

Browse files
authored
Fix SignalR functions extensions sample (#310)
1. The recommended way to serve static content in function apps is to use "site/wwwroot" directory. However, it doesn't work locally. Update code to make reading static html file work both on Azure and locally. 2. Fix negotiation function error
1 parent e923386 commit c07e640

File tree

4 files changed

+50
-13
lines changed

4 files changed

+50
-13
lines changed

samples/DotnetIsolated-BidirectionChat/DotnetIsolated-BidirectionChat.csproj

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFramework>net6.0</TargetFramework>
3+
<TargetFramework>net8.0</TargetFramework>
44
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
55
<OutputType>Exe</OutputType>
66
<RootNamespace>IsolatedModel_BidirectionChat</RootNamespace>
77
</PropertyGroup>
88
<ItemGroup>
9-
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.12" />
10-
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.SignalRService" Version="1.7.0" />
11-
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.3.0" OutputItemType="Analyzer" />
12-
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.5.2" />
9+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.3.0" />
10+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.SignalRService" Version="1.15.0" />
11+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="2.0.1" OutputItemType="Analyzer" />
12+
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="2.0.0" />
1313
</ItemGroup>
1414
<ItemGroup>
1515
<None Update="host.json">

samples/DotnetIsolated-BidirectionChat/Functions.cs

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.IO;
23
using System.Net;
34
using Microsoft.Azure.Functions.Worker;
@@ -19,7 +20,28 @@ public Functions(ILoggerFactory loggerFactory)
1920
public HttpResponseData GetWebPage([HttpTrigger(AuthorizationLevel.Anonymous)] HttpRequestData req)
2021
{
2122
var response = req.CreateResponse(HttpStatusCode.OK);
22-
response.WriteString(File.ReadAllText("content/index.html"));
23+
string content;
24+
string home = Environment.GetEnvironmentVariable("HOME");
25+
if (!string.IsNullOrEmpty(home))
26+
{
27+
string htmlFilePath = Path.Combine(home, "site", "wwwroot", "content", "index.html");
28+
if (File.Exists(htmlFilePath))
29+
{
30+
// When running on Azure
31+
content = File.ReadAllText(htmlFilePath);
32+
}
33+
else
34+
{
35+
// Assume the function is running locally with function core tools
36+
content = File.ReadAllText("content/index.html");
37+
}
38+
}
39+
else
40+
{
41+
// Assume the function is running locally with function core tools
42+
content = File.ReadAllText("content/index.html");
43+
}
44+
response.WriteString(content);
2345
response.Headers.Add("Content-Type", "text/html");
2446
return response;
2547
}

samples/DotnetIsolated-BidirectionChat/content/index.html

-3
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,6 @@ <h3>Serverless chat</h3>
165165
throw "No username entered";
166166
}
167167
getConnectionInfo().then(info => {
168-
// make compatible with old and new SignalRConnectionInfo
169-
info.accessToken = info.AccessToken || info.accessKey; // pay attention to the case
170-
info.url = info.Url || info.endpoint; // pay attention to the case
171168
data.ready = true;
172169
const options = {
173170
accessTokenFactory: () => info.accessToken

samples/DotnetIsolated-ClassBased/Functions.cs

+21-3
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,31 @@ public Functions(IServiceProvider serviceProvider, ILogger<Functions> logger) :
2222
public HttpResponseData GetWebPage([HttpTrigger(AuthorizationLevel.Anonymous)] HttpRequestData req)
2323
{
2424
var response = req.CreateResponse(HttpStatusCode.OK);
25+
string content;
2526
string home = Environment.GetEnvironmentVariable("HOME");
26-
string htmlFilePath = Path.Combine(home, "site", "wwwroot", "content", "index.html");
27-
response.WriteString(File.ReadAllText(htmlFilePath));
27+
if (!string.IsNullOrEmpty(home))
28+
{
29+
string htmlFilePath = Path.Combine(home, "site", "wwwroot", "content", "index.html");
30+
if (File.Exists(htmlFilePath))
31+
{
32+
// When running on Azure
33+
content = File.ReadAllText(htmlFilePath);
34+
}
35+
else
36+
{
37+
// Assume the function is running locally with function core tools
38+
content = File.ReadAllText("content/index.html");
39+
}
40+
}
41+
else
42+
{
43+
// Assume the function is running locally with function core tools
44+
content = File.ReadAllText("content/index.html");
45+
}
46+
response.WriteString(content);
2847
response.Headers.Add("Content-Type", "text/html");
2948
return response;
3049
}
31-
3250
[Function("negotiate")]
3351
public async Task<HttpResponseData> Negotiate([HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req)
3452
{

0 commit comments

Comments
 (0)