Skip to content

Commit

Permalink
Merge pull request #34 from nventive/feature/net7
Browse files Browse the repository at this point in the history
Feature/workflows
  • Loading branch information
fperreaultnv committed Apr 13, 2023
2 parents f2e2d31 + bd4b491 commit 5b5b3a9
Show file tree
Hide file tree
Showing 22 changed files with 202 additions and 84 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Build
on:
pull_request:
branches:
- master

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

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

- name: Restore dependencies
run: dotnet restore src/Content/Backend

- name: Build
run: dotnet build src/Content/Backend --configuration Release

- name: Run tests
run: dotnet test src/Content/Backend --configuration Release --no-build

- name: Restore dependencies
run: dotnet restore src/Content/NetStandardComponent

- name: Build
run: dotnet build src/Content/NetStandardComponent --configuration Release

- name: Run tests
run: dotnet test src/Content/NetStandardComponent --configuration Release --no-build
19 changes: 19 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Publish
on:
push:
tags:
- v*

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Build NuGet Package
run: nuget pack src/NV.Templates.nuspec -Version ${{ github.ref }} -OutputDirectory ./artifacts

- name: Publish NuGet package
run: nuget push ./artifacts/NV.Templates.*.nupkg ${{ secrets.NUGET_API_KEY }} -Source https://api.nuget.org/v3/index.json
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ When using the `--RestApi` option, 2 projects are added to the solution:
enabled and setup with support for versioning of the API in the URL (e.g. `/api/v1/...`)
- [Application Insights](https://docs.microsoft.com/en-us/azure/azure-monitor/app/app-insights-overview) is configured
- HTTPS is enforced and [HSTS](https://fr.wikipedia.org/wiki/HTTP_Strict_Transport_Security) is configured
- [CORS](https://developer.mozilla.org/fr/docs/Web/HTTP/CORS) is configured
- [CORS](https://developer.mozilla.org/en-us/docs/Web/HTTP/CORS) is configured
- Full request and response tracing is available, courtesy of the `AspNetCoreRequestTracing` component
- Generic error handling middleware is already setup (in `Framework.Middleware.ExceptionHandler`)
and configured for the exception classes provided in the Core project
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sdk": {
"version": "6.0.101"
"version": "6.0.407"
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using HttpTracing;
using Microsoft.Extensions.Configuration;
using NV.Templates.Backend.Core.Configuration;
using NV.Templates.Backend.Core.General;

namespace Microsoft.Extensions.DependencyInjection
{
Expand All @@ -22,7 +21,7 @@ public static IServiceCollection AddCore(this IServiceCollection services, IConf
};
});

services.AutoRegisterServicesFromAssemblyContaining<IApplicationInfo>();
services.AutoRegisterServicesFromAssembly();

services.AddHealthChecks();

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using Microsoft.Extensions.DependencyInjection;

namespace NV.Templates.Backend.Core.Framework.DependencyInjection
{
/// <summary>
/// Allows to register a service into the dependency injection service collection.
/// By default, the <see cref="RegistrationModes.Interface"/> registration mode is used with a <see cref="ServiceLifetime.Transient"/> lifetime.
/// </summary>
/// <param name="modes">The registration modes.</param>
/// <param name="lifetime">The registration lifetime.</param>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public sealed class RegisterServiceAttribute : Attribute
{
public RegisterServiceAttribute(
RegistrationModes modes = RegistrationModes.Interface,
ServiceLifetime lifetime = ServiceLifetime.Transient)
{
Modes = modes;
Lifetime = lifetime;
}

public RegisterServiceAttribute(ServiceLifetime lifetime, RegistrationModes modes = RegistrationModes.Interface)
: this(modes, lifetime)
{
}

public RegistrationModes Modes { get; }

public ServiceLifetime Lifetime { get; }
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

namespace NV.Templates.Backend.Core.Framework.DependencyInjection
{
[Flags]
public enum RegistrationModes
{
/// <summary>
/// Allows to register the interface.
/// </summary>
Interface,

/// <summary>
/// Allows to register the concrete class.
/// </summary>
ConcreteClass,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,69 @@ namespace Microsoft.Extensions.DependencyInjection
public static class RegistrationServiceCollectionExtensions
{
/// <summary>
/// Registers all services in <paramref name="assembly"/> that are marked with <see cref="RegisterSingletonServiceAttribute"/>,
/// <see cref="RegisterScopedServiceAttribute"/> or <see cref="RegisterTransientServiceAttribute"/>.
/// Registers all services in <paramref name="assembly"/> that are marked with <see cref="RegisterServiceAttribute"/>.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="assembly">The <see cref="Assembly"/> to scan.</param>
/// <returns>The <see cref="IServiceCollection"/> with services registered.</returns>
public static IServiceCollection AutoRegisterServicesFromAssembly(this IServiceCollection services, Assembly assembly)
public static IServiceCollection AutoRegisterServicesFromAssembly(this IServiceCollection services, Assembly? assembly = null)
{
assembly ??= Assembly.GetCallingAssembly();
services.Scan(scan =>
{
scan
.FromAssemblies(assembly)
.AddClasses(classes => classes.WithAttribute<RegisterSingletonServiceAttribute>())
.AddClasses(classes => classes
.WithAttribute<RegisterServiceAttribute>(a =>
a.Modes.HasFlag(RegistrationModes.Interface) &&
a.Lifetime == ServiceLifetime.Singleton))
.AsImplementedInterfaces()
.WithSingletonLifetime();
scan
.FromAssemblies(assembly)
.AddClasses(classes => classes.WithAttribute<RegisterScopedServiceAttribute>())
.AddClasses(classes => classes
.WithAttribute<RegisterServiceAttribute>(a =>
a.Modes.HasFlag(RegistrationModes.ConcreteClass) &&
a.Lifetime == ServiceLifetime.Singleton))
.AsSelf()
.WithSingletonLifetime();
scan
.FromAssemblies(assembly)
.AddClasses(classes => classes
.WithAttribute<RegisterServiceAttribute>(a =>
a.Modes.HasFlag(RegistrationModes.Interface) &&
a.Lifetime == ServiceLifetime.Scoped))
.AsImplementedInterfaces()
.WithScopedLifetime();
scan
.FromAssemblies(assembly)
.AddClasses(classes => classes.WithAttribute<RegisterTransientServiceAttribute>())
.AddClasses(classes => classes
.WithAttribute<RegisterServiceAttribute>(a =>
a.Modes.HasFlag(RegistrationModes.ConcreteClass) &&
a.Lifetime == ServiceLifetime.Scoped))
.AsSelf()
.WithScopedLifetime();
scan
.FromAssemblies(assembly)
.AddClasses(classes => classes
.WithAttribute<RegisterServiceAttribute>(a =>
a.Modes.HasFlag(RegistrationModes.Interface) &&
a.Lifetime == ServiceLifetime.Transient))
.AsImplementedInterfaces()
.WithTransientLifetime();
scan
.FromAssemblies(assembly)
.AddClasses(classes => classes
.WithAttribute<RegisterServiceAttribute>(a =>
a.Modes.HasFlag(RegistrationModes.ConcreteClass) &&
a.Lifetime == ServiceLifetime.Transient))
.AsSelf()
.WithTransientLifetime();
});

return services;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NV.Templates.Backend.Core.Framework.DependencyInjection;

Expand All @@ -7,7 +8,7 @@ namespace NV.Templates.Backend.Core.General
/// <summary>
/// <see cref="IApplicationInfo"/> implementation.
/// </summary>
[RegisterSingletonService]
[RegisterService(ServiceLifetime.Singleton)]
internal class ApplicationInfo : IApplicationInfo
{
public ApplicationInfo()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
using System;
using System.Security.Principal;
using Microsoft.Extensions.DependencyInjection;
using NV.Templates.Backend.Core.Framework.DependencyInjection;
using NV.Templates.Backend.Core.Framework.Entities;

namespace NV.Templates.Backend.Core.General
{
/// <summary>
/// <see cref="IOperationContext"/> implementation.
/// </summary>
[RegisterScopedService]
[RegisterService(ServiceLifetime.Scoped)]
internal class OperationContext : IOperationContext
{
/// <inheritdoc/>
public string Id { get; set; } = IdGenerator.Generate();
public string Id { get; set; } = $"{Guid.NewGuid()}";

/// <inheritdoc/>
public DateTimeOffset Timestamp { get; set; } = DateTimeOffset.UtcNow;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using NV.Templates.Backend.Core.Framework.Entities;
using NV.Templates.Backend.Core.General;

namespace NV.Templates.Backend.Functions
Expand Down Expand Up @@ -35,7 +34,7 @@ public class GeneralFunctions
throw new ArgumentNullException(nameof(request));
}

_operationContext.Id = executionContext?.InvocationId.ToString() ?? IdGenerator.Generate();
_operationContext.Id = executionContext?.InvocationId.ToString() ?? $"{Guid.NewGuid()}";
return new JsonResult(_applicationInfo) { StatusCode = StatusCodes.Status200OK };
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AzureFunctionsVersion>v3</AzureFunctionsVersion>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
<UserSecretsId>20403522-635f-44fd-ab82-8a754fe59c64</UserSecretsId>
</PropertyGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="5.0.0" />
<!--#if (SPA) -->
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="6.0.4" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<!--#endif -->
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
Expand All @@ -7,7 +8,6 @@
using Microsoft.AspNetCore.JsonPatch;
using Microsoft.AspNetCore.Mvc;
using NV.Templates.Backend.Core.Framework.Continuation;
using NV.Templates.Backend.Core.Framework.Entities;
using NV.Templates.Backend.Web.Framework.Models;

namespace NV.Templates.Backend.Web.RestApi
Expand Down Expand Up @@ -68,7 +68,7 @@ public async Task<ActionResult<SampleModel>> CreateSample([FromBody] SampleModel
{
var createdModel = new SampleModel
{
Id = IdGenerator.Generate(),
Id = $"{Guid.NewGuid()}",
Name = model.Name,
};

Expand Down
4 changes: 1 addition & 3 deletions src/Content/Backend/NV.Templates.Backend.Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using NV.Templates.Backend.Web.Framework.Middlewares;
using NV.Templates.Backend.Web.RestApi;

Expand All @@ -35,7 +33,7 @@ public void ConfigureServices(IServiceCollection services)
{
services
.AddRedisCacheIfPresent(_configuration)
.AutoRegisterServicesFromAssemblyContaining<Startup>()
.AutoRegisterServicesFromAssembly()
.AddCore(_configuration)
.AddWeb(_configuration)
.AddRestApi()
Expand Down
Loading

0 comments on commit 5b5b3a9

Please sign in to comment.