Skip to content

Commit

Permalink
Add Aspire project (#560)
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofPajak authored Jan 19, 2025
1 parent b2076b8 commit 9aa2ccc
Show file tree
Hide file tree
Showing 39 changed files with 684 additions and 487 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/aspnetcore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ jobs:
uses: actions/setup-dotnet@v4
with:
dotnet-version: 9.0.x
- name: Install .NET Aspire workload
run: dotnet workload install aspire
- name: Restore
run: dotnet restore ./GrandNode.sln
- name: Build with dotnet
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/grandnode.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ jobs:
dotnet-version: 9.0.x
- name: Create mongoDB Docker container
run: sudo docker run -d -p 27017:27017 mongo:latest
- name: Install .NET Aspire workload
run: dotnet workload install aspire
- name: Restore
run: dotnet restore "${{ env.WORKING_DIRECTORY }}"
- name: Build sln
Expand Down
9 changes: 9 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,14 @@
<PackageVersion Include="MSTest.TestFramework" Version="3.6.4" />
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
<PackageVersion Include="NUnit" Version="4.2.2" />
<PackageVersion Include="Aspire.Hosting.AppHost" Version="9.0.0" />
<PackageVersion Include="Aspire.Hosting.MongoDB" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Http.Resilience" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.ServiceDiscovery" Version="9.0.0" />
<PackageVersion Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.9.0" />
<PackageVersion Include="OpenTelemetry.Extensions.Hosting" Version="1.9.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.9.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.Http" Version="1.9.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.Runtime" Version="1.9.0" />
</ItemGroup>
</Project>
12 changes: 12 additions & 0 deletions GrandNode.sln
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Grand.Module.Api", "src\Mod
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Grand.Module.ScheduledTasks", "src\Modules\Grand.Module.ScheduledTasks\Grand.Module.ScheduledTasks.csproj", "{136F1E35-8B20-465C-8D42-30A5A0D0DA1F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.AppHost", "src\Aspire\Aspire.AppHost\Aspire.AppHost.csproj", "{FFA85947-690E-496B-B21E-DB1800F0DCA9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.ServiceDefaults", "src\Aspire\Aspire.ServiceDefaults\Aspire.ServiceDefaults.csproj", "{591950E4-6377-4ECA-A386-8BD3FFE302F1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -363,6 +367,14 @@ Global
{136F1E35-8B20-465C-8D42-30A5A0D0DA1F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{136F1E35-8B20-465C-8D42-30A5A0D0DA1F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{136F1E35-8B20-465C-8D42-30A5A0D0DA1F}.Release|Any CPU.Build.0 = Release|Any CPU
{FFA85947-690E-496B-B21E-DB1800F0DCA9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FFA85947-690E-496B-B21E-DB1800F0DCA9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FFA85947-690E-496B-B21E-DB1800F0DCA9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FFA85947-690E-496B-B21E-DB1800F0DCA9}.Release|Any CPU.Build.0 = Release|Any CPU
{591950E4-6377-4ECA-A386-8BD3FFE302F1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{591950E4-6377-4ECA-A386-8BD3FFE302F1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{591950E4-6377-4ECA-A386-8BD3FFE302F1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{591950E4-6377-4ECA-A386-8BD3FFE302F1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
29 changes: 8 additions & 21 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
# ASP.NET Core

# Build and test ASP.NET Core projects targeting .NET Core.
# Add steps that run tests, create a NuGet package, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core

trigger:
- main

pool:
vmImage: ubuntu-latest

variables:
buildConfiguration: 'Release'

resources:
containers:
- container: mongo
Expand All @@ -25,10 +10,15 @@ services:

steps:
- task: UseDotNet@2
displayName: 'Install .NET Core SDK 8'
displayName: 'Install .NET Core SDK 9'
inputs:
version: '9.0.x'

- task: Install .NET Aspire workload
inputs:
inlineScript: |
dotnet workload install aspire
- task: NuGetToolInstaller@1
displayName: 'Install NuGet >=6.3.0-0'
inputs:
Expand All @@ -37,7 +27,7 @@ steps:

- task: NuGetCommand@2
inputs:
command: 'restore'
command: 'restore'
restoreSolution: '**/*.sln'
feedsToUse: 'select'

Expand Down Expand Up @@ -69,7 +59,4 @@ steps:
arguments: '--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)'
zipAfterPublish: True

- task: PublishPipelineArtifact@1
inputs:
targetPath: '$(Build.ArtifactStagingDirectory)'
artifactName: 'grandnode'
- task: PublishPipelineArtifact@1
25 changes: 25 additions & 0 deletions src/Aspire/Aspire.AppHost/Aspire.AppHost.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<Sdk Name="Aspire.AppHost.Sdk" Version="9.0.0" />

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireHost>true</IsAspireHost>
<UserSecretsId>bdc1e5b4-4475-44fc-851c-dd576ac2c123</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Aspire.Hosting.AppHost" />
<PackageReference Include="Aspire.Hosting.MongoDB" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Web\Grand.Web.Admin\Grand.Web.Admin.csproj" />
<ProjectReference Include="..\..\Web\Grand.Web.Vendor\Grand.Web.Vendor.csproj" />
<ProjectReference Include="..\..\Web\Grand.Web\Grand.Web.csproj" />
</ItemGroup>

</Project>
9 changes: 9 additions & 0 deletions src/Aspire/Aspire.AppHost/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Aspire.AppHost;

var builder = DistributedApplication.CreateBuilder(args);

var mongo = builder.AddMongoDB("mongo").WithLifetime(ContainerLifetime.Persistent);
var mongodb = mongo.AddDatabase("Mongodb");
builder.ConfigureGrandWebProject(mongodb);

builder.Build().Run();
13 changes: 13 additions & 0 deletions src/Aspire/Aspire.AppHost/ProjectConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Aspire.AppHost;

public static class ProjectConfiguration
{
public static void ConfigureGrandWebProject(this IDistributedApplicationBuilder builder, IResourceBuilder<MongoDBDatabaseResource> mongodb)
{
builder
.AddProject<Projects.Grand_Web>("grand-web")
.WithHttpEndpoint(80, name: "front")
.WithReference(mongodb)
.WaitFor(mongodb);
}
}
32 changes: 32 additions & 0 deletions src/Aspire/Aspire.AppHost/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:17196;http://localhost:15129",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21225",
"DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22257",
"ASPIRE_ALLOW_UNSECURED_TRANSPORT": "true"
}
},
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:15129",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19032",
"DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20163",
"ASPIRE_ALLOW_UNSECURED_TRANSPORT": "true"

}
}
}
}
8 changes: 8 additions & 0 deletions src/Aspire/Aspire.AppHost/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions src/Aspire/Aspire.AppHost/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Aspire.Hosting.Dcp": "Warning"
}
}
}
22 changes: 22 additions & 0 deletions src/Aspire/Aspire.ServiceDefaults/Aspire.ServiceDefaults.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireSharedProject>true</IsAspireSharedProject>
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />

<PackageReference Include="Microsoft.Extensions.Http.Resilience" />
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" />
</ItemGroup>

</Project>
89 changes: 89 additions & 0 deletions src/Aspire/Aspire.ServiceDefaults/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.ServiceDiscovery;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;

namespace Microsoft.Extensions.Hosting;

// Adds common .NET Aspire services: service discovery, resilience, health checks, and OpenTelemetry.
// This project should be referenced by each service project in your solution.
// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults
public static class Extensions
{
public static TBuilder AddServiceDefaults<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
{
builder.ConfigureOpenTelemetry();

builder.Services.AddServiceDiscovery();

builder.Services.ConfigureHttpClientDefaults(http =>
{
// Turn on resilience by default
http.AddStandardResilienceHandler();

// Turn on service discovery by default
http.AddServiceDiscovery();
});

// Uncomment the following to restrict the allowed schemes for service discovery.
// builder.Services.Configure<ServiceDiscoveryOptions>(options =>
// {
// options.AllowedSchemes = ["https"];
// });

return builder;
}

public static TBuilder ConfigureOpenTelemetry<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
{
builder.Logging.AddOpenTelemetry(logging =>
{
logging.IncludeFormattedMessage = true;
logging.IncludeScopes = true;
});

builder.Services.AddOpenTelemetry()
.WithMetrics(metrics =>
{
metrics.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddRuntimeInstrumentation();
})
.WithTracing(tracing =>
{
tracing.AddSource(builder.Environment.ApplicationName)
.AddAspNetCoreInstrumentation()
// Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package)
//.AddGrpcClientInstrumentation()
.AddHttpClientInstrumentation();
});

builder.AddOpenTelemetryExporters();

return builder;
}

private static TBuilder AddOpenTelemetryExporters<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
{
var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);

if (useOtlpExporter)
{
builder.Services.AddOpenTelemetry().UseOtlpExporter();
}

// Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package)
//if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]))
//{
// builder.Services.AddOpenTelemetry()
// .UseAzureMonitor();
//}

return builder;
}
}
7 changes: 0 additions & 7 deletions src/Core/Grand.Infrastructure/Configuration/DatabaseConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,4 @@ public class DatabaseConfig
/// Gets or sets a value indicating whether use LiteDB database (only for installation process)
/// </summary>
public string LiteDbConnectionString { get; set; }

/// <summary>
/// Gets or sets a value indicating whether use connection string for database (only for installed databases)
/// </summary>
public string ConnectionString { get; set; }

public int DbProvider { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ public class PerformanceConfig
/// </summary>
public bool UseResponseCompression { get; set; }

/// <summary>
/// Gets or sets a value indicating whether ignore DbVersionCheckMiddleware
/// </summary>
public bool IgnoreDbVersionCheckMiddleware { get; set; }

/// <summary>
/// Gets or sets a value indicating whether ignore IgnoreUsePoweredByMiddleware
/// </summary>
Expand Down
15 changes: 11 additions & 4 deletions src/Core/Grand.Infrastructure/StartupBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,18 @@ public static class StartupBase
/// </summary>
private static void InitDatabase(IServiceCollection services, IConfiguration configuration)
{
var dbConfig = services.StartupConfig<DatabaseConfig>(configuration.GetSection("Database"));
if (!string.IsNullOrEmpty(dbConfig.ConnectionString))
var connectionString = configuration[SettingsConstants.ConnectionStrings];
var providerString = configuration[SettingsConstants.ConnectionStringsProvider];
var providerInt = 0;
if (!string.IsNullOrEmpty(providerString))
{
_ = int.TryParse(providerString, out providerInt);
}

if (!string.IsNullOrEmpty(connectionString))
DataSettingsManager.Instance.LoadDataSettings(new DataSettings {
ConnectionString = dbConfig.ConnectionString,
DbProvider = (DbProvider)dbConfig.DbProvider
ConnectionString = connectionString,
DbProvider = (DbProvider)providerInt,
});
}

Expand Down
7 changes: 7 additions & 0 deletions src/Core/Grand.SharedKernel/Extensions/SettingsConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Grand.SharedKernel.Extensions;

public static class SettingsConstants
{
public const string ConnectionStrings = "ConnectionStrings:Mongodb";
public const string ConnectionStringsProvider = "ConnectionStrings:Provider";
}
Loading

0 comments on commit 9aa2ccc

Please sign in to comment.