Update Hub to .NET 8 and all OpenTelemetry

This commit is contained in:
2024-01-27 16:25:14 -05:00
parent d8fd6d13f9
commit 9cddb80f00
12 changed files with 78 additions and 125 deletions

View File

@@ -1,17 +0,0 @@
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
namespace ChrisKaczor.HomeMonitor.Hub.Service.Controllers
{
[Route("[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
return new[] { "value1", "value2" };
}
}
}

View File

@@ -1,8 +1,8 @@
FROM mcr.microsoft.com/dotnet/aspnet:5.0-alpine AS base
FROM mcr.microsoft.com/dotnet/aspnet:8.0-alpine AS base
WORKDIR /app
EXPOSE 80
EXPOSE 8080
FROM mcr.microsoft.com/dotnet/sdk:5.0-alpine AS build
FROM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS build
WORKDIR /src
COPY ["./Service.csproj", "./"]
RUN dotnet restore "Service.csproj"

View File

@@ -1,17 +1,17 @@
using JetBrains.Annotations;
using Microsoft.AspNetCore.SignalR;
using System;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
namespace ChrisKaczor.HomeMonitor.Hub.Service.Hubs
{
[UsedImplicitly]
public class DeviceStatusHub : Microsoft.AspNetCore.SignalR.Hub
public class DeviceStatusHub(ILogger<DeviceStatusHub> logger) : Microsoft.AspNetCore.SignalR.Hub
{
[UsedImplicitly]
public async Task RequestLatestStatus()
{
Console.WriteLine("RequestLatestStatus");
logger.LogInformation("RequestLatestStatus");
await Clients.Others.SendAsync("RequestLatestStatus");
}
@@ -19,7 +19,7 @@ namespace ChrisKaczor.HomeMonitor.Hub.Service.Hubs
[UsedImplicitly]
public async Task SendLatestStatus(string message)
{
Console.WriteLine($"LatestStatus: {message}");
logger.LogInformation($"LatestStatus: {message}");
await Clients.Others.SendAsync("LatestStatus", message);
}

View File

@@ -1,17 +1,17 @@
using JetBrains.Annotations;
using Microsoft.AspNetCore.SignalR;
using System;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
namespace ChrisKaczor.HomeMonitor.Hub.Service.Hubs
{
[UsedImplicitly]
public class EnvironmentHub : Microsoft.AspNetCore.SignalR.Hub
public class EnvironmentHub(ILogger<EnvironmentHub> logger) : Microsoft.AspNetCore.SignalR.Hub
{
[UsedImplicitly]
public async Task SendMessage(string message)
{
Console.WriteLine(message);
logger.LogInformation(message);
await Clients.Others.SendAsync("Message", message);
}

View File

@@ -1,17 +1,17 @@
using JetBrains.Annotations;
using Microsoft.AspNetCore.SignalR;
using System;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
namespace ChrisKaczor.HomeMonitor.Hub.Service.Hubs
{
[UsedImplicitly]
public class PowerHub : Microsoft.AspNetCore.SignalR.Hub
public class PowerHub(ILogger<PowerHub> logger) : Microsoft.AspNetCore.SignalR.Hub
{
[UsedImplicitly]
public async Task SendLatestSample(string message)
{
Console.WriteLine(message);
logger.LogInformation(message);
await Clients.Others.SendAsync("LatestSample", message);
}

View File

@@ -1,17 +1,17 @@
using JetBrains.Annotations;
using Microsoft.AspNetCore.SignalR;
using System;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
namespace ChrisKaczor.HomeMonitor.Hub.Service.Hubs
{
[UsedImplicitly]
public class WeatherHub : Microsoft.AspNetCore.SignalR.Hub
public class WeatherHub(ILogger<WeatherHub> logger) : Microsoft.AspNetCore.SignalR.Hub
{
[UsedImplicitly]
public async Task SendLatestReading(string message)
{
Console.WriteLine(message);
logger.LogInformation(message);
await Clients.Others.SendAsync("LatestReading", message);
}

View File

@@ -1,18 +1,43 @@
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using ChrisKaczor.Common.OpenTelemetry;
using ChrisKaczor.HomeMonitor.Hub.Service.Hubs;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Reflection;
namespace ChrisKaczor.HomeMonitor.Hub.Service
namespace ChrisKaczor.HomeMonitor.Hub.Service;
public static class Program
{
public static class Program
public static void Main(string[] args)
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
var builder = WebApplication.CreateBuilder(args);
private static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
return WebHost.CreateDefaultBuilder(args).UseStartup<Startup>();
}
builder.Configuration.AddEnvironmentVariables();
builder.Services.AddCommonOpenTelemetry(Assembly.GetExecutingAssembly().GetName().Name, builder.Configuration["Telemetry:Endpoint"]);
builder.Services.AddCors(o => o.AddPolicy("CorsPolicy", corsPolicyBuilder => corsPolicyBuilder.AllowAnyMethod().AllowAnyHeader().AllowCredentials().WithOrigins("http://localhost:4200")));
builder.Services.AddSignalR().AddJsonProtocol(options => options.PayloadSerializerOptions.WriteIndented = false);
// ---
var app = builder.Build();
if (builder.Environment.IsDevelopment())
app.UseDeveloperExceptionPage();
app.UseCors("CorsPolicy");
app.UseRouting();
app.MapHub<WeatherHub>("/weather");
app.MapHub<PowerHub>("/power");
app.MapHub<DeviceStatusHub>("/device-status");
app.MapHub<EnvironmentHub>("/environment");
app.Run();
}
}

View File

@@ -1,30 +1,12 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:53872",
"sslPort": 0
"profiles": {
"Service": {
"commandName": "Project",
"launchBrowser": false,
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "values",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Service": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "values",
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
}

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<AssemblyName>ChrisKaczor.HomeMonitor.Hub.Service</AssemblyName>
<RootNamespace>ChrisKaczor.HomeMonitor.Hub.Service</RootNamespace>
@@ -10,7 +10,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ChrisKaczor.HomeMonitor.Weather.Models" Version="1.1.6" />
<PackageReference Include="JetBrains.Annotations" Version="2021.1.0" />
<PackageReference Include="ChrisKaczor.Common.OpenTelemetry" Version="1.0.2" />
<PackageReference Include="ChrisKaczor.HomeMonitor.Weather.Models" Version="1.1.8" />
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" />
</ItemGroup>
</Project>

View File

@@ -1,42 +0,0 @@
using ChrisKaczor.HomeMonitor.Hub.Service.Hubs;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace ChrisKaczor.HomeMonitor.Hub.Service
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
services.AddCors(o => o.AddPolicy("CorsPolicy", builder => builder.AllowAnyMethod().AllowAnyHeader().AllowCredentials().WithOrigins("http://localhost:4200")));
services.AddSignalR().AddJsonProtocol(options => options.PayloadSerializerOptions.WriteIndented = false);
}
public void Configure(IApplicationBuilder applicationBuilder, IWebHostEnvironment environment)
{
if (environment.IsDevelopment())
{
applicationBuilder.UseDeveloperExceptionPage();
}
applicationBuilder.UseCors("CorsPolicy");
applicationBuilder.UseRouting();
applicationBuilder.UseEndpoints(endpoints =>
{
endpoints.MapHub<WeatherHub>("/weather");
endpoints.MapHub<PowerHub>("/power");
endpoints.MapHub<DeviceStatusHub>("/device-status");
endpoints.MapHub<EnvironmentHub>("/environment");
endpoints.MapDefaultControllerRoute();
});
}
}
}

View File

@@ -1,8 +1,12 @@
{
"Logging": {
"LogLevel": {
"Default": "Warning"
"Logging": {
"LogLevel": {
"Default": "Warning",
"Microsoft": "Information"
}
},
"AllowedHosts": "*",
"Telemetry": {
"Endpoint": "http://signoz-otel-collector.platform:4317/"
}
},
"AllowedHosts": "*"
}

View File

@@ -38,7 +38,7 @@ metadata:
spec:
ports:
- name: client
port: 80
port: 8080
selector:
app: hub-service
type: ClusterIP
@@ -62,7 +62,7 @@ spec:
- kind: Service
name: hub-service
namespace: home-monitor
port: 80
port: 8080
---
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware