Skip to content

Commit df00069

Browse files
committed
Improve zones.json for deployment
Fixes #43 Embed `zones.json` as a resource and modify code to read it. * **Api.csproj** - Add `zones.json` as an embedded resource. * **NwsManager.cs** - Modify `GetZonesAsync` method to read `zones.json` as an embedded resource instead of from the `wwwroot` folder. - Remove the `webHostEnvironment` parameter from the `NwsManager` constructor. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/dotnet-presentations/dotnet-aspire-workshop/issues/43?shareId=XXXX-XXXX-XXXX-XXXX).
1 parent 39ac854 commit df00069

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

complete/Api/Api.csproj

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@
1212
<ItemGroup>
1313
<ProjectReference Include="..\ServiceDefaults\ServiceDefaults.csproj" />
1414
</ItemGroup>
15-
</Project>
15+
<ItemGroup>
16+
<EmbeddedResource Include="wwwroot\zones.json" />
17+
</ItemGroup>
18+
</Project>

complete/Api/Data/NwsManager.cs

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
using System.Text.Json;
22
using System.Web;
3+
using System.Reflection;
34
using Microsoft.AspNetCore.Http.HttpResults;
45
using Microsoft.Extensions.Caching.Memory;
56
using Api.Data;
67

78
namespace Api
89
{
9-
public class NwsManager(HttpClient httpClient, IMemoryCache cache, IWebHostEnvironment webHostEnvironment)
10+
public class NwsManager(HttpClient httpClient, IMemoryCache cache)
1011
{
1112
private static readonly JsonSerializerOptions options = new(JsonSerializerDefaults.Web);
1213

@@ -25,15 +26,16 @@ public class NwsManager(HttpClient httpClient, IMemoryCache cache, IWebHostEnvir
2526
// .Distinct()
2627
// .ToArray() ?? [];
2728

28-
// Deserialize the zones.json file from the wwwroot folder
29-
var zonesFilePath = Path.Combine(webHostEnvironment.WebRootPath, "zones.json");
30-
if (!File.Exists(zonesFilePath))
29+
// Deserialize the zones.json file from the embedded resource
30+
var assembly = Assembly.GetExecutingAssembly();
31+
var resourceName = "Api.wwwroot.zones.json";
32+
using var stream = assembly.GetManifestResourceStream(resourceName);
33+
if (stream == null)
3134
{
3235
return [];
3336
}
3437

35-
using var zonesJson = File.OpenRead(zonesFilePath);
36-
var zones = await JsonSerializer.DeserializeAsync<ZonesResponse>(zonesJson, options);
38+
var zones = await JsonSerializer.DeserializeAsync<ZonesResponse>(stream, options);
3739

3840
return zones?.Features
3941
?.Where(f => f.Properties?.ObservationStations?.Count > 0)
@@ -122,4 +124,4 @@ public static IServiceCollection AddNwsManager(this IServiceCollection services)
122124
return app;
123125
}
124126
}
125-
}
127+
}

0 commit comments

Comments
 (0)