Skip to content

Commit

Permalink
Migrate to .NET 8
Browse files Browse the repository at this point in the history
  • Loading branch information
tiwatsuka authored and YaSuenag committed Mar 27, 2024
1 parent 734605d commit 9d51f79
Show file tree
Hide file tree
Showing 72 changed files with 968 additions and 167 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM mcr.microsoft.com/dotnet/sdk:6.0
FROM mcr.microsoft.com/dotnet/sdk:8.0

RUN apt-get update

Expand Down
34 changes: 19 additions & 15 deletions .github/workflows/1-pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ env:
# web app
DOCKERFILE_PATH: "CarbonAware.WebApi/src/Dockerfile"
HEALTH_ENDPOINT: "0.0.0.0:8080/health"
DLL_FILE_PATH: "./bin/Release/net6.0/CarbonAware.WebApi.dll"
DLL_FILE_PATH: "./bin/Release/net8.0/CarbonAware.WebApi.dll"
DOTNET_SRC_DIR: "./src"
# console app packages
DOTNET_SOLUTION: "src/GSF.CarbonAware/src/GSF.CarbonAware.csproj"
Expand Down Expand Up @@ -40,9 +40,9 @@ jobs:

- uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v3
with:
dotnet-version: 6.0.x
dotnet-version: 8.0.x

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
Expand Down Expand Up @@ -86,14 +86,14 @@ jobs:
needs: sln-build-and-test
runs-on: ubuntu-latest
container:
image: mcr.microsoft.com/dotnet/sdk:6.0
image: mcr.microsoft.com/dotnet/sdk:8.0
steps:
- uses: actions/checkout@v3

- name: Setup .NET Core SDK 6
uses: actions/setup-dotnet@v2
- name: Setup .NET Core SDK 8
uses: actions/setup-dotnet@v3
with:
dotnet-version: '6.0.x'
dotnet-version: '8.0.x'
include-prerelease: false

- name: Install dependencies
Expand Down Expand Up @@ -123,6 +123,8 @@ jobs:

- name: Generate Open API
run: dotnet tool run swagger tofile --output ./wwwroot/api/v1/swagger.yaml --yaml ${{ env.DLL_FILE_PATH }} v1
env:
DOTNET_ROLL_FORWARD: LatestMajor
working-directory: ./src/CarbonAware.WebApi/src

- name: Upload swagger artifact
Expand All @@ -144,7 +146,7 @@ jobs:

- name: Docker Run Container
run: |
docker run -d --name runnable-container -p 8080:80 ca-api
docker run -d --name runnable-container -p 8080:8080 ca-api
docker container ls
- name: Docker WGET Health Endpoint
Expand All @@ -164,10 +166,10 @@ jobs:
uses: actions/checkout@v3
with:
ref: dev
- name: Setup .NET Core SDK 6
uses: actions/setup-dotnet@v2
- name: Setup .NET Core SDK 8
uses: actions/setup-dotnet@v3
with:
dotnet-version: '6.0.x'
dotnet-version: '8.0.x'
include-prerelease: false
- name: Install dependencies
run: dotnet restore
Expand All @@ -179,6 +181,8 @@ jobs:
working-directory: ${{ env.DOTNET_SRC_DIR }}
- name: Generate Open API
run: dotnet tool run swagger tofile --output ./wwwroot/api/v1/swagger.yaml --yaml ${{ env.DLL_FILE_PATH }} v1
env:
DOTNET_ROLL_FORWARD: LatestMajor
- name: Upload dev artifact
uses: actions/upload-artifact@v1
with:
Expand All @@ -199,10 +203,10 @@ jobs:
- name: Checkout
uses: actions/checkout@v3

- name: Setup .NET Core SDK 6
uses: actions/setup-dotnet@v2
- name: Setup .NET Core SDK 8
uses: actions/setup-dotnet@v3
with:
dotnet-version: '6.0.x'
dotnet-version: '8.0.x'
include-prerelease: false

- name: Create packages
Expand Down Expand Up @@ -244,4 +248,4 @@ jobs:
command: config
globs: |
./custom.markdownlint.jsonc
{"*[^.github]/**,*"}.md
{"*[^.github]/**,*"}.md
2 changes: 1 addition & 1 deletion .github/workflows/dev_carbon-aware-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Set up .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: '6.0.x'
dotnet-version: '8.0.x'
include-prerelease: true

- name: Build with dotnet
Expand Down
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"type": "coreclr",
"request": "launch",
"preLaunchTask": "buildCLI",
"program": "${workspaceFolder}/src/CarbonAware.CLI/src/bin/Debug/net6.0/caw.dll",
"program": "${workspaceFolder}/src/CarbonAware.CLI/src/bin/Debug/net8.0/caw.dll",
"args": [
"emissions",
"--location", "${input:caw_location}"
Expand All @@ -27,7 +27,7 @@
"type": "coreclr",
"request": "launch",
"preLaunchTask": "buildWebApi",
"program": "${workspaceFolder}/src/CarbonAware.WebApi/src/bin/Debug/net6.0/CarbonAware.WebApi.dll",
"program": "${workspaceFolder}/src/CarbonAware.WebApi/src/bin/Debug/net8.0/CarbonAware.WebApi.dll",
"args": [],
"cwd": "${workspaceFolder}/src/CarbonAware.WebApi/src/",
"stopAtEntry": false,
Expand Down
2 changes: 1 addition & 1 deletion casdk-docs/docs/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ generated libraries for your language of choice!

Prerequisites:

- .NET Core 6.0
- .NET Core 8.0
- Alternatively:
- Docker
- VSCode (it is recommended to work in a Dev Container)
Expand Down
8 changes: 4 additions & 4 deletions casdk-docs/docs/tutorial-basics/carbon-aware-webapi.md
Original file line number Diff line number Diff line change
Expand Up @@ -456,10 +456,10 @@ CarbonAware.LocationSources.LocationSource: Warning: New key swedencentral_1 gen
## Error Handling

The WebAPI leveraged the
[.Net controller filter pipeline](https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/filters?view=aspnetcore-6.0)
[.Net controller filter pipeline](https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/filters?view=aspnetcore-8.0)
to ensure that all requests respond with a consistent JSON schema.

![.Net controller filter pipeline image](https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/filters/_static/filter-pipeline-2.png?view=aspnetcore-6.0)
![.Net controller filter pipeline image](https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/filters/_static/filter-pipeline-2.png?view=aspnetcore-8.0)

Controllers are responsible for managing the "Success" responses. If an error
occurs in the WebAPI code and an unhandled exception is thrown, the
Expand All @@ -470,7 +470,7 @@ caught and handled by the WebAPI code, the controller will continue to manage
the response.

The .Net framework will automatically respond to validation errors with a
[ValidationProblemDetails](https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.validationproblemdetails?view=aspnetcore-6.0)
[ValidationProblemDetails](https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.validationproblemdetails?view=aspnetcore-8.0)
object. Using the Exception Filter class enables the WebAPI to consistently
respond with the `ValidationProblemDetails` error schema in all error cases and
take advantage of error handling automatically provided by the framework.
Expand All @@ -489,7 +489,7 @@ specification
cd CarbonAware.WebApi/src
dotnet tool restore
dotnet build --configuration Release --no-restore
dotnet tool run swagger tofile --output ./wwwroot/api/v1/swagger.yaml --yaml bin/Release/net6.0/CarbonAware.WebApi.dll v1
dotnet tool run swagger tofile --output ./wwwroot/api/v1/swagger.yaml --yaml bin/Release/net8.0/CarbonAware.WebApi.dll v1
```
1. The `CarbonAware.WebApi/src/wwwroot/api/v1/swagger.yaml` file contains the supported
OpenApi specification.
Expand Down
8 changes: 4 additions & 4 deletions casdk-docs/docs/tutorial-basics/containerization.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ carbon_aware v1 6293e2528bf2 About an hour ago 230MB
## Run WebApi Image

1. Run the image using `docker run` with host port 8000 mapped to the WebApi
port 80 and configure environment variable settings for
port 8080 and configure environment variable settings for
[WattTime](https://www.watttime.org) provider.

```sh
docker run --rm -p 8000:80 \
docker run --rm -p 8000:8080 \
> -e DataSources__EmissionsDataSource="WattTime" \
> -e DataSources__ForecastDataSource="WattTime" \
> -e DataSources__Configurations__WattTime__Type="WattTime" \
Expand All @@ -40,7 +40,7 @@ carbon_aware v1 6293e2528bf2 About an hour ago 230MB
or the [ElectricityMaps](https://www.electricitymaps.com) provider

```sh
docker run --rm -p 8000:80 \
docker run --rm -p 8000:8080 \
> -e DataSources__EmissionsDataSource="ElectricityMaps" \
> -e DataSources__ForecastDataSource="ElectricityMaps" \
> -e DataSources__Configurations__ElectricityMaps__Type="ElectricityMaps" \
Expand All @@ -52,7 +52,7 @@ carbon_aware v1 6293e2528bf2 About an hour ago 230MB
or the [ElectricityMapsFree](https://www.co2signal.com/) provider

```sh
docker run --rm -p 8000:80 \
docker run --rm -p 8000:8080 \
> -e DataSources__EmissionsDataSource="ElectricityMapsFree" \
> -e DataSources__Configurations__ElectricityMapsFree__Type="ElectricityMapsFree" \
> -e DataSources__Configurations__ElectricityMapsFree__token="<YOUR_CO2SIGNAL_TOKEN>" \
Expand Down
57 changes: 54 additions & 3 deletions casdk-docs/docs/tutorial-extras/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
- [ElectricityMapsFree Configuration](#electricitymapsfree-configuration)
- [API Token](#api-token)
- [BaseUrl](#baseurl)
- [Cache](#cache)
- [CarbonAwareVars](#carbonawarevars)
- [Tracing and Monitoring Configuration](#tracing-and-monitoring-configuration)
- [Verbosity](#verbosity)
- [Prometheus exporter](#prometheus-exporter-for-emissions-data)
- [Web API Prefix](#web-api-prefix)
- [LocationDataSourcesConfiguration](#locationdatasourcesconfiguration)
- [Sample Configurations](#sample-configurations)
Expand Down Expand Up @@ -194,7 +196,7 @@ custom `EmissionsData` sets. The file should be located under the
`<user's repo>/src/data/data-sources/` directory that is part of the repository.
At build time, all the JSON files under `<user's repo>/src/data/data-sources/`
are copied over the destination directory
`<user's repo>/src/CarbonAware.WebApi/src/bin/[Debug|Publish]/net6.0/data-sources/json`
`<user's repo>/src/CarbonAware.WebApi/src/bin/[Debug|Publish]/net8.0/data-sources/json`
that is part of the `CarbonAware.WebApi` assembly. Also the file can be placed
where the assembly `CarbonAware.WebApi.dll` is located under `data-sources/json`
directory. For instance, if the application is installed under `/app`, copy the
Expand Down Expand Up @@ -327,6 +329,29 @@ The url to use when connecting to ElectricityMapsFree. Defaults to
"https://api.co2signal.com/v1/" but can be overridden in the config if needed
(such as to enable integration testing scenarios).

## Cache

Frequent access to data sources could cause problems such as performance trouble
or exceed rate limit. To avoid them, you can configure data cache like this:

```json
{
"EmissionsDataCache": {
"Enabled": true,
"ExpirationMin": 30
}
}
```

The behavior of current cache implementation:
* Only emissions data are cached
* Forecast data are not stored
* The result of the latest query to data sources is cached
* Use cache rather than data sources if even one datum in cache match with the query
* Even though more data in data sources would be matched, they are not retrieved
* Cached data are stored in memory
* They are cleard when the process of the SDK is down

## CarbonAwareVars

This section contains the global settings for the SDK. The configuration looks
Expand Down Expand Up @@ -379,6 +404,32 @@ InstrumentationKey. For more details, please refer to
AppInsights_InstrumentationKey="AppInsightsInstrumentationKey"
```

### Prometheus exporter for emissions data

> DISCLAIMER: The `/metrics` Prometheus exporter is currently unsupported, and is used for internal GSF needs, and may change in the future. It will retrieve _all_ emissions data and create heavy load on your data API's. It is turned off by default.
In the WebApi project, this application can exporse latest carbon emissions data as a prometheus exporter.

```bash
CarbonAwareVars__EnableCarbonExporter="true"
```
The scraping endpoint is `<ROOT_PATH>/metrics` like this:

```bash
http://localhost/metrics
```

By default, the exposed data are latest ones within last 24 hours. If you would like to change the period
in some reasones, you can configure the value like this:

```json
{
"CarbonExporter": {
"PeriodInHours": 48
}
}
```

### Verbosity

You can configure the verbosity of the application error messages by setting the
Expand Down Expand Up @@ -427,7 +478,7 @@ By setting `LocationDataSourcesConfiguration` property with one or more location
data sources, it is possible to load different `Location` data sets in order to
have more than one location. For instance by setting two location regions, the
property would be set as follow using
[environment](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-6.0#naming-of-environment-variables)
[environment](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-8.0#naming-of-environment-variables)
variables:

```sh
Expand Down Expand Up @@ -458,7 +509,7 @@ curl "http://${IP_HOST}:${PORT}/emissions/bylocations/best?location=${REGION}&ti
At build time, all the JSON files under
`<user's repo>/src/data/location-sources` are copied over the destination
directory
`<user's repo>/src/CarbonAware.WebApi/src/bin/[Debug|Publish]/net6.0/location-sources/json`
`<user's repo>/src/CarbonAware.WebApi/src/bin/[Debug|Publish]/net8.0/location-sources/json`
that is part of the `CarbonAware.WebApi` assembly. Also the file can be placed
where the assembly `CarbonAware.WebApi.dll` is located under
`location-sources/json` directory. For instance, if the application is installed
Expand Down
2 changes: 1 addition & 1 deletion casdk-docs/docs/tutorial-extras/packaging.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ project. When running in the dev container you will need:
- [Remote Containers extension for VSCode](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers)

Alternatively you can run in your local environment using the
[.NET Core 6.0 SDK](https://dotnet.microsoft.com/en-us/download/dotnet/6.0).
[.NET Core 8.0 SDK](https://dotnet.microsoft.com/en-us/download/dotnet/8.0).

## SDK Configuration

Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "6.0.418",
"version": "8.0.201",
"rollForward": "latestFeature"
}
}
2 changes: 1 addition & 1 deletion helm-chart/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ spec:
{{- end }}
ports:
- name: http
containerPort: 80
containerPort: 8080
protocol: TCP
volumeMounts:
- name: appsettings
Expand Down
2 changes: 1 addition & 1 deletion helm-chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ securityContext: {}

service:
type: ClusterIP
port: 80
port: 8080

ingress:
enabled: false
Expand Down
5 changes: 3 additions & 2 deletions samples/azure/azure-function/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Find the Dockerfile at this URL
# https://github.com/Azure/azure-functions-docker/blob/dev/host/4/bullseye/amd64/dotnet/dotnet-inproc/dotnet.Dockerfile

FROM mcr.microsoft.com/azure-functions/dotnet:4.0 AS base
FROM mcr.microsoft.com/azure-functions/dotnet-isolated:4-dotnet-isolated8.0 AS base
WORKDIR /home/site/wwwroot

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
COPY ["src/", "data/src/"]
COPY ["scripts/", "data/scripts/"]
COPY ["samples/", "data/samples/"]
Expand All @@ -21,3 +21,4 @@ RUN dotnet publish "samples/azure/azure-function/function.csproj" -c Release -o
FROM base AS final
WORKDIR /home/site/wwwroot
COPY --from=publish /app/publish .
ENV ASPNETCORE_CONTENTROOT=/home/site/wwwroot
Loading

0 comments on commit 9d51f79

Please sign in to comment.