From 4150ea3c33fe6042160bed60a05a5ca61b22dc85 Mon Sep 17 00:00:00 2001 From: Zitong Yang Date: Thu, 3 Apr 2025 19:34:19 +0800 Subject: [PATCH] Fix SignalR functions extensions sample 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 --- .../DotnetIsolated-BidirectionChat.csproj | 12 +++++----- .../Functions.cs | 24 ++++++++++++++++++- .../content/index.html | 3 --- .../DotnetIsolated-ClassBased/Functions.cs | 24 ++++++++++++++++--- 4 files changed, 50 insertions(+), 13 deletions(-) diff --git a/samples/DotnetIsolated-BidirectionChat/DotnetIsolated-BidirectionChat.csproj b/samples/DotnetIsolated-BidirectionChat/DotnetIsolated-BidirectionChat.csproj index fa777186..c6079ab4 100644 --- a/samples/DotnetIsolated-BidirectionChat/DotnetIsolated-BidirectionChat.csproj +++ b/samples/DotnetIsolated-BidirectionChat/DotnetIsolated-BidirectionChat.csproj @@ -1,15 +1,15 @@ - + - net6.0 + net8.0 v4 Exe IsolatedModel_BidirectionChat - - - - + + + + diff --git a/samples/DotnetIsolated-BidirectionChat/Functions.cs b/samples/DotnetIsolated-BidirectionChat/Functions.cs index 8b2c493c..5bafd021 100644 --- a/samples/DotnetIsolated-BidirectionChat/Functions.cs +++ b/samples/DotnetIsolated-BidirectionChat/Functions.cs @@ -1,3 +1,4 @@ +using System; using System.IO; using System.Net; using Microsoft.Azure.Functions.Worker; @@ -19,7 +20,28 @@ public Functions(ILoggerFactory loggerFactory) public HttpResponseData GetWebPage([HttpTrigger(AuthorizationLevel.Anonymous)] HttpRequestData req) { var response = req.CreateResponse(HttpStatusCode.OK); - response.WriteString(File.ReadAllText("content/index.html")); + string content; + string home = Environment.GetEnvironmentVariable("HOME"); + if (!string.IsNullOrEmpty(home)) + { + string htmlFilePath = Path.Combine(home, "site", "wwwroot", "content", "index.html"); + if (File.Exists(htmlFilePath)) + { + // When running on Azure + content = File.ReadAllText(htmlFilePath); + } + else + { + // Assume the function is running locally with function core tools + content = File.ReadAllText("content/index.html"); + } + } + else + { + // Assume the function is running locally with function core tools + content = File.ReadAllText("content/index.html"); + } + response.WriteString(content); response.Headers.Add("Content-Type", "text/html"); return response; } diff --git a/samples/DotnetIsolated-BidirectionChat/content/index.html b/samples/DotnetIsolated-BidirectionChat/content/index.html index 1396df43..6711b787 100644 --- a/samples/DotnetIsolated-BidirectionChat/content/index.html +++ b/samples/DotnetIsolated-BidirectionChat/content/index.html @@ -165,9 +165,6 @@

Serverless chat

throw "No username entered"; } getConnectionInfo().then(info => { - // make compatible with old and new SignalRConnectionInfo - info.accessToken = info.AccessToken || info.accessKey; // pay attention to the case - info.url = info.Url || info.endpoint; // pay attention to the case data.ready = true; const options = { accessTokenFactory: () => info.accessToken diff --git a/samples/DotnetIsolated-ClassBased/Functions.cs b/samples/DotnetIsolated-ClassBased/Functions.cs index 57f0a6b9..f52b4ba9 100644 --- a/samples/DotnetIsolated-ClassBased/Functions.cs +++ b/samples/DotnetIsolated-ClassBased/Functions.cs @@ -22,13 +22,31 @@ public Functions(IServiceProvider serviceProvider, ILogger logger) : public HttpResponseData GetWebPage([HttpTrigger(AuthorizationLevel.Anonymous)] HttpRequestData req) { var response = req.CreateResponse(HttpStatusCode.OK); + string content; string home = Environment.GetEnvironmentVariable("HOME"); - string htmlFilePath = Path.Combine(home, "site", "wwwroot", "content", "index.html"); - response.WriteString(File.ReadAllText(htmlFilePath)); + if (!string.IsNullOrEmpty(home)) + { + string htmlFilePath = Path.Combine(home, "site", "wwwroot", "content", "index.html"); + if (File.Exists(htmlFilePath)) + { + // When running on Azure + content = File.ReadAllText(htmlFilePath); + } + else + { + // Assume the function is running locally with function core tools + content = File.ReadAllText("content/index.html"); + } + } + else + { + // Assume the function is running locally with function core tools + content = File.ReadAllText("content/index.html"); + } + response.WriteString(content); response.Headers.Add("Content-Type", "text/html"); return response; } - [Function("negotiate")] public async Task Negotiate([HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req) {