diff --git a/samples/MicroServices/NBB.Contracts/NBB.Contracts.Worker/MultiTenancy/TenantRepositoryMock.cs b/samples/MicroServices/NBB.Contracts/NBB.Contracts.Worker/MultiTenancy/TenantRepositoryMock.cs index dda251ff..272d26a1 100644 --- a/samples/MicroServices/NBB.Contracts/NBB.Contracts.Worker/MultiTenancy/TenantRepositoryMock.cs +++ b/samples/MicroServices/NBB.Contracts/NBB.Contracts.Worker/MultiTenancy/TenantRepositoryMock.cs @@ -10,19 +10,13 @@ namespace NBB.Contracts.Worker.MultiTenancy { public class TenantRepositoryMock : ITenantRepository { - private readonly IOptions _tenancyHostingOptions; - - public TenantRepositoryMock(IOptions tenancyHostingOptions) + public TenantRepositoryMock() { - _tenancyHostingOptions = tenancyHostingOptions; } public Task Get(Guid id, CancellationToken token = default) { - var isMonoTenant = _tenancyHostingOptions.Value.TenancyType == TenancyType.MonoTenant && - id == _tenancyHostingOptions.Value.TenantId; - - var tenant = new Tenant(id, id.ToString(), !isMonoTenant); + var tenant = new Tenant(id, id.ToString(), true); return Task.FromResult(tenant); } diff --git a/samples/MicroServices/NBB.Contracts/NBB.Contracts.Worker/Program.cs b/samples/MicroServices/NBB.Contracts/NBB.Contracts.Worker/Program.cs index fb45ad3d..14d9e57a 100644 --- a/samples/MicroServices/NBB.Contracts/NBB.Contracts.Worker/Program.cs +++ b/samples/MicroServices/NBB.Contracts/NBB.Contracts.Worker/Program.cs @@ -90,7 +90,7 @@ public static async Task MainAsync(string[] args) .UsePipeline(pipelineBuilder => pipelineBuilder .UseCorrelationMiddleware() .UseExceptionHandlingMiddleware() - //.UseTenantMiddleware() + .UseTenantMiddleware() .UseDefaultResiliencyMiddleware() .UseMediatRMiddleware() ); diff --git a/src/Messaging/NBB.Messaging.MultiTenancy/TenantMiddleware.cs b/src/Messaging/NBB.Messaging.MultiTenancy/TenantMiddleware.cs index 45f8f824..0be20e87 100644 --- a/src/Messaging/NBB.Messaging.MultiTenancy/TenantMiddleware.cs +++ b/src/Messaging/NBB.Messaging.MultiTenancy/TenantMiddleware.cs @@ -33,6 +33,12 @@ public TenantMiddleware(ITenantContextAccessor tenantContextAccessor, ITenantIde public async Task Invoke(MessagingEnvelope message, CancellationToken cancellationToken, Func next) { + if (_tenancyOptions.Value.TenancyType == TenancyType.None) + { + await next(); + return; + } + var tenantId = await _tenantIdentificationService.GetTenantIdAsync(); if (!message.Headers.TryGetValue(MessagingHeaders.TenantId, out var messageTenantIdHeader)) @@ -71,14 +77,14 @@ public async Task Invoke(MessagingEnvelope message, CancellationToken cancellati throw new ApplicationException( $"Received a message for premium tenant {messageTenantIdHeader} in a MultiTenant (shared) context"); } - + if (_tenantContextAccessor.TenantContext != null) { throw new ApplicationException("Tenant context is already set"); } _tenantContextAccessor.TenantContext = new TenantContext(tenant); - + await next(); } } diff --git a/template/Worker/content/NBB.Worker/.template.config/dotnetcli.host.json b/template/Worker/content/NBB.Worker/.template.config/dotnetcli.host.json index 807bfb53..d458eed9 100644 --- a/template/Worker/content/NBB.Worker/.template.config/dotnetcli.host.json +++ b/template/Worker/content/NBB.Worker/.template.config/dotnetcli.host.json @@ -31,23 +31,14 @@ "SqlLoggingConnectionString": { "longName": "sql-logging-connection-string" }, - "FluentValidation": { - "longName": "fluent-validation" - }, - "AutoMapper": { - "longName": "auto-mapper" - }, - "MediatR": { - "longName": "mediat-r" - }, - "AddSamples": { - "longName": "add-samples" - }, "MessagingTransport": { "longName": "messaging-transport" }, "SkipRestore": { "longName": "no-restore" + }, + "OpenTracing": { + "longName": "open-tracing" } } } \ No newline at end of file diff --git a/template/Worker/content/NBB.Worker/.template.config/template.json b/template/Worker/content/NBB.Worker/.template.config/template.json index e3f996e6..415b9690 100644 --- a/template/Worker/content/NBB.Worker/.template.config/template.json +++ b/template/Worker/content/NBB.Worker/.template.config/template.json @@ -18,7 +18,7 @@ ], "sources": [ { - "exclude": ["PlaceholderTypes/*", ".template.config/*"], + "exclude": [".template.config/*"], "modifiers": [ { "condition": "(!HealthCheck)", @@ -107,29 +107,11 @@ "replaces": "SQL_LOG_CONNECTION_STRING", "description": "The connection string to the sql server database." }, - "FluentValidation": { + "OpenTracing": { "type": "parameter", "datatype": "bool", "defaultValue": "true", - "description": "Register fluent validators in IoC container." - }, - "AutoMapper": { - "type": "parameter", - "datatype": "bool", - "defaultValue": "true", - "description": "Register AutoMapper profilers in IoC container." - }, - "MediatR": { - "type": "parameter", - "datatype": "bool", - "defaultValue": "true", - "description": "Add MediatR support." - }, - "AddSamples": { - "type": "parameter", - "datatype": "bool", - "defaultValue": "true", - "description": "Add sample files like a subscriber pipeline middleware or a publisher decorator." + "description": "Add Jaeger / OpenTracing support" }, "MessagingTransport": { "type": "parameter", diff --git a/template/Worker/content/NBB.Worker/Application/HelloWorld.cs b/template/Worker/content/NBB.Worker/Application/HelloWorld.cs new file mode 100644 index 00000000..a555d08e --- /dev/null +++ b/template/Worker/content/NBB.Worker/Application/HelloWorld.cs @@ -0,0 +1,24 @@ +using MediatR; +using NBB.Core.Abstractions; +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace NBB.Worker.Application +{ + public class HelloWorld + { + public class Command : ICommand, IRequest + { + + } + + public class Handler : IRequestHandler + { + public Task Handle(Command request, CancellationToken cancellationToken) + { + throw new NotImplementedException(); + } + } + } +} diff --git a/template/Worker/content/NBB.Worker/DependencyInjectionExtensions.cs b/template/Worker/content/NBB.Worker/DependencyInjectionExtensions.cs index 1aa1a050..4f71c37f 100644 --- a/template/Worker/content/NBB.Worker/DependencyInjectionExtensions.cs +++ b/template/Worker/content/NBB.Worker/DependencyInjectionExtensions.cs @@ -1,23 +1,12 @@ -#if AutoMapper -using AutoMapper; -#endif -#if FluentValidation -using FluentValidation; -#endif -#if MediatR -using MediatR; +using MediatR; using MediatR.Pipeline; -#endif using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using NBB.Application.DataContracts; +using NBB.Worker.Messaging; +using NBB.Worker.Application; using NBB.Messaging.Host; using NBB.Messaging.Host.Builder; using NBB.Messaging.Host.MessagingPipeline; -using NBB.Messaging.InProcessMessaging.Extensions; -#if AddSamples -using NBB.Worker.Messaging; -#endif using NBB.Messaging.Abstractions; #if NatsMessagingTransport using NBB.Messaging.Nats; @@ -32,7 +21,7 @@ using Jaeger.Samplers; using Jaeger.Reporters; using Jaeger.Senders; -using OpenTracing.Util; +using OpenTracing.Util; #endif #elif KafkaMessagingTransport using NBB.Messaging.Kafka; @@ -45,55 +34,39 @@ namespace NBB.Worker { public static class DependencyInjectionExtensions { - public static void AddWorkerServices(this IServiceCollection services, IConfiguration configuration, - bool useTestDoubles = false) + public static void AddWorkerServices(this IServiceCollection services, IConfiguration configuration) { -#if MediatR // MediatR - services.AddMediatR(typeof(__AHandler__).Assembly); + services.AddMediatR(typeof(HelloWorld.Command).Assembly); services.AddScoped(typeof(IPipelineBehavior<,>), typeof(RequestPreProcessorBehavior<,>)); services.AddScoped(typeof(IPipelineBehavior<,>), typeof(RequestPostProcessorBehavior<,>)); - services.Scan(scan => scan.FromAssemblyOf<__AHandler__>() + services.Scan(scan => scan.FromAssemblyOf() .AddClasses(classes => classes.AssignableTo(typeof(IPipelineBehavior<,>))) .AsImplementedInterfaces() .WithScopedLifetime()); -#endif // Messaging - if (useTestDoubles) - { - services.AddInProcessMessaging(); - } - else - { #if NatsMessagingTransport - services.AddNatsMessaging(); + services.AddNatsMessaging(); #elif KafkaMessagingTransport - services.AddKafkaMessaging(); + services.AddKafkaMessaging(); #endif - } + #if (Resiliency) services.AddResiliency(); #endif -#if AddSamples services.Decorate(); -#endif + #if OpenTracing services.Decorate(); #endif services.AddMessagingHost() .AddSubscriberServices(selector => { -#if MediatR selector .FromMediatRHandledCommands() - .AddClassesAssignableTo(); -#else - selector - .FromAssemblyOf<__ACommand__>() - .AddClassesAssignableTo(); -#endif + .AddAllClasses(); }) .WithDefaultOptions() .UsePipeline(builder => builder @@ -105,28 +78,10 @@ public static void AddWorkerServices(this IServiceCollection services, IConfigur #if Resiliency .UseDefaultResiliencyMiddleware() #endif -#if MediatR .UseMediatRMiddleware() -#endif -#if AddSamples .UseMiddleware() -#endif ); - -#if FluentValidation - // Validation - services.Scan(scan => scan.FromAssemblyOf<__AValidator__>() - .AddClasses(classes => classes.AssignableTo()) - .AsImplementedInterfaces() - .WithScopedLifetime()); -#endif - -#if AutoMapper - // AutoMapper - services.AddAutoMapper(typeof(__AMappingProfile__).Assembly); -#endif - #if OpenTracing // OpenTracing services.AddOpenTracing(); diff --git a/template/Worker/content/NBB.Worker/Dockerfile b/template/Worker/content/NBB.Worker/Dockerfile index fa7b0da0..033a833c 100644 --- a/template/Worker/content/NBB.Worker/Dockerfile +++ b/template/Worker/content/NBB.Worker/Dockerfile @@ -1,8 +1,8 @@ -FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base +FROM microsoft/dotnet:3.1-aspnetcore-runtime AS base WORKDIR /app EXPOSE 80 -FROM microsoft/dotnet:2.2-sdk AS build +FROM microsoft/dotnet:3.1-sdk AS build WORKDIR /src COPY NuGet.config . COPY dependencies.props . diff --git a/template/Worker/content/NBB.Worker/Messaging/SamplePublisherDecorator.cs b/template/Worker/content/NBB.Worker/Messaging/SamplePublisherDecorator.cs index bde416a1..3717a480 100644 --- a/template/Worker/content/NBB.Worker/Messaging/SamplePublisherDecorator.cs +++ b/template/Worker/content/NBB.Worker/Messaging/SamplePublisherDecorator.cs @@ -1,5 +1,4 @@ -#if AddSamples -using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging; using NBB.Messaging.Abstractions; using NBB.Messaging.DataContracts; using System; @@ -32,5 +31,4 @@ void NewCustomizer(MessagingEnvelope outgoingEnvelope) return _inner.PublishAsync(message, cancellationToken, NewCustomizer); } } -} -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/template/Worker/content/NBB.Worker/Messaging/SampleSubscriberPipelineMiddleware.cs b/template/Worker/content/NBB.Worker/Messaging/SampleSubscriberPipelineMiddleware.cs index 67880ab6..f3204f97 100644 --- a/template/Worker/content/NBB.Worker/Messaging/SampleSubscriberPipelineMiddleware.cs +++ b/template/Worker/content/NBB.Worker/Messaging/SampleSubscriberPipelineMiddleware.cs @@ -1,5 +1,4 @@ -#if AddSamples -using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging; using NBB.Core.Pipeline; using NBB.Messaging.DataContracts; using System; @@ -23,5 +22,4 @@ public async Task Invoke(MessagingEnvelope message, CancellationToken cancellati await next(); } } -} -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/template/Worker/content/NBB.Worker/NBB.Worker.csproj b/template/Worker/content/NBB.Worker/NBB.Worker.csproj index 46d0f477..ac87d3ec 100644 --- a/template/Worker/content/NBB.Worker/NBB.Worker.csproj +++ b/template/Worker/content/NBB.Worker/NBB.Worker.csproj @@ -1,8 +1,12 @@  + + + + - netcoreapp2.2 + netcoreapp3.1 Linux latest Exe @@ -22,6 +26,12 @@ PROJECT-DESCRIPTION-XML Copyright © PROJECT-AUTHOR-XML. All rights Reserved + + + + + + @@ -29,29 +39,27 @@ - - - + + + + - - - - + + - - + @@ -67,7 +75,7 @@ - - + + diff --git a/template/Worker/content/NBB.Worker/PlaceholderTypes/ACommand.cs b/template/Worker/content/NBB.Worker/PlaceholderTypes/ACommand.cs deleted file mode 100644 index 7952ef7e..00000000 --- a/template/Worker/content/NBB.Worker/PlaceholderTypes/ACommand.cs +++ /dev/null @@ -1,11 +0,0 @@ -using NBB.Application.DataContracts; - -namespace NBB.Worker -{ - public class __ACommand__ : Command - { - public __ACommand__(CommandMetadata metadata) : base(metadata) - { - } - } -} diff --git a/template/Worker/content/NBB.Worker/PlaceholderTypes/AHandler.cs b/template/Worker/content/NBB.Worker/PlaceholderTypes/AHandler.cs deleted file mode 100644 index 9f65ae76..00000000 --- a/template/Worker/content/NBB.Worker/PlaceholderTypes/AHandler.cs +++ /dev/null @@ -1,17 +0,0 @@ -#if MediatR -using System; -using System.Threading; -using System.Threading.Tasks; -using MediatR; - -namespace NBB.Worker -{ - public class __AHandler__ : IRequestHandler<__ACommand__> - { - public Task Handle(__ACommand__ request, CancellationToken cancellationToken) - { - throw new NotImplementedException(); - } - } -} -#endif diff --git a/template/Worker/content/NBB.Worker/PlaceholderTypes/AMappingProfile.cs b/template/Worker/content/NBB.Worker/PlaceholderTypes/AMappingProfile.cs deleted file mode 100644 index 8c55f2a2..00000000 --- a/template/Worker/content/NBB.Worker/PlaceholderTypes/AMappingProfile.cs +++ /dev/null @@ -1,10 +0,0 @@ -#if AutoMapper -using AutoMapper; - -namespace NBB.Worker -{ - public class __AMappingProfile__ : Profile - { - } -} -#endif diff --git a/template/Worker/content/NBB.Worker/PlaceholderTypes/AValidator.cs b/template/Worker/content/NBB.Worker/PlaceholderTypes/AValidator.cs deleted file mode 100644 index e662dc90..00000000 --- a/template/Worker/content/NBB.Worker/PlaceholderTypes/AValidator.cs +++ /dev/null @@ -1,15 +0,0 @@ -#if FluentValidation -using FluentValidation; -using NBB.Application.DataContracts; - -namespace NBB.Worker -{ - public class __AValidator__ : AbstractValidator - { - public __AValidator__() - { - RuleFor(x => x.Metadata.CommandId).NotEmpty(); - } - } -} -#endif \ No newline at end of file diff --git a/template/Worker/content/NBB.Worker/Program.cs b/template/Worker/content/NBB.Worker/Program.cs index 21e6f6e0..1ee9a618 100644 --- a/template/Worker/content/NBB.Worker/Program.cs +++ b/template/Worker/content/NBB.Worker/Program.cs @@ -68,7 +68,7 @@ public static IHost BuildConsoleHost(string[] args) => ConfigureSerilog(hostBuilderContext.Configuration); loggingBuilder.AddSerilog(); }) - .ConfigureServices((hostingContext, services) => services.AddWorkerServices(Configuration)) + .ConfigureServices((hostingContext, services) => services.AddWorkerServices(hostingContext.Configuration)) .UseConsoleLifetime() .Build(); #endif diff --git a/template/Worker/content/NBB.Worker/Properties/launchSettings.json b/template/Worker/content/NBB.Worker/Properties/launchSettings.json index d313b8dc..6573a80e 100644 --- a/template/Worker/content/NBB.Worker/Properties/launchSettings.json +++ b/template/Worker/content/NBB.Worker/Properties/launchSettings.json @@ -1,6 +1,6 @@ { "profiles": { - "Notifier.Worker": { + "NBB.Worker": { "commandName": "Project", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" diff --git a/template/Worker/content/NBB.Worker/Startup.cs b/template/Worker/content/NBB.Worker/Startup.cs index 13aef407..cf0940fe 100644 --- a/template/Worker/content/NBB.Worker/Startup.cs +++ b/template/Worker/content/NBB.Worker/Startup.cs @@ -2,7 +2,7 @@ #if HealthCheck using NBB.Worker.HealthChecks; using Microsoft.AspNetCore.Diagnostics.HealthChecks; -using Microsoft.Extensions.Diagnostics.HealthChecks; +using HealthChecks.UI.Client; #endif using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; @@ -30,19 +30,19 @@ public void ConfigureServices(IServiceCollection services) services.AddHealthChecks() // Registers health checks services // Add a health check for a SQL database .AddCheck("SQL database", - new SqlConnectionHealthCheck("Log_Database", Configuration["ConnectionStrings:Log_Database"])); - - services.AddSingleton(); + new SqlConnectionHealthCheck("Log_Database", Configuration["ConnectionStrings:Log_Database"])) + .AddCheck("GC"); + #endif } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IHostingEnvironment env) + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { #if HealthCheck - app.UseHealthChecks("/healthz", options: new HealthCheckOptions() + app.UseHealthChecks("/health", options: new HealthCheckOptions() { - ResponseWriter = HealthCheckWriter.WriteResponse + ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse }); #endif } diff --git a/template/Worker/content/NBB.Worker/appsettings.json b/template/Worker/content/NBB.Worker/appsettings.json index cfdec8db..86ad65e2 100644 --- a/template/Worker/content/NBB.Worker/appsettings.json +++ b/template/Worker/content/NBB.Worker/appsettings.json @@ -21,5 +21,14 @@ "group_id": "NBB.Worker" } //#endif + }, + //#if (OpenTracing) + "OpenTracing": { + "Jeager": { + "IsEnabled": "true", + "AgentHost": "your_agent_host", + "AgentPort": 9999 // Your agent port + } } + //#endif } \ No newline at end of file diff --git a/template/Worker/content/NBB.Worker/dependencies.props b/template/Worker/content/NBB.Worker/dependencies.props new file mode 100644 index 00000000..81162eae --- /dev/null +++ b/template/Worker/content/NBB.Worker/dependencies.props @@ -0,0 +1,22 @@ + + + 4.2.6 + 11.0.2 + 4.1.0 + 3.1.0 + 4.8.2 + 2.4.0 + 2.4.0 + 3.1.0 + 4.5.0 + 2.7.1 + 2.0.2 + 5.1.2 + 2.1.1 + 0.12.0 + 0.5.0 + 0.3.2 + 4.1.3 + 2.2.0 + + \ No newline at end of file diff --git a/template/Worker/development.props b/template/Worker/development.props index e8693130..a81b5710 100644 --- a/template/Worker/development.props +++ b/template/Worker/development.props @@ -1,9 +1,4 @@  - - 2.2.0 - 2.2.0 - 2.2.0 - true @@ -15,11 +10,7 @@ false false true - true - true - true true - true $(HealthCheck) @@ -29,10 +20,6 @@ $(ConditionalCompilationSymbols);AspNetApp $(ConditionalCompilationSymbols);SqlLogging $(ConditionalCompilationSymbols);Resiliency - $(ConditionalCompilationSymbols);FluentValidation - $(ConditionalCompilationSymbols);AutoMapper - $(ConditionalCompilationSymbols);MediatR $(ConditionalCompilationSymbols);OpenTracing - $(ConditionalCompilationSymbols);AddSamples \ No newline at end of file