Skip to content

Commit

Permalink
Package update and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
charlessolar committed Dec 6, 2022
1 parent 3e81ddb commit afd9cf2
Show file tree
Hide file tree
Showing 50 changed files with 675 additions and 287 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Aggregates.NET.NServiceBus" Version="0.17.11.102" />
<PackageReference Include="NEST" Version="7.17.4" />
<PackageReference Include="Aggregates.NET.NServiceBus" Version="0.18.7.112" />
<PackageReference Include="NEST" Version="7.17.5" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Aggregates.NET.NServiceBus" Version="0.17.11.102" />
<PackageReference Include="MongoDB.Driver" Version="2.16.1" />
<PackageReference Include="Aggregates.NET.NServiceBus" Version="0.18.7.112" />
<PackageReference Include="MongoDB.Driver" Version="2.18.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
</Compile>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Aggregates.NET.Testing" Version="0.17.11.102" />
<PackageReference Include="Aggregates.NET.Testing" Version="0.18.7.112" />
<PackageReference Include="AutoFixture" Version="4.17.0" />
<PackageReference Include="AutoFixture.AutoFakeItEasy" Version="4.17.0" />
<PackageReference Include="AutoFixture.Xunit2" Version="4.17.0" />
<PackageReference Include="FakeItEasy" Version="7.3.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Aggregates.NET.NServiceBus" Version="0.17.11.102" />
<PackageReference Include="Aggregates.NET.NServiceBus" Version="0.18.7.112" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions src/Contexts/Basket/Domain/Tests/Basket.Domain.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Aggregates.NET.Testing" Version="0.17.11.102" />
<PackageReference Include="Aggregates.NET.Testing" Version="0.18.7.112" />
<PackageReference Include="AutoFixture" Version="4.17.0" />
<PackageReference Include="AutoFixture.AutoFakeItEasy" Version="4.17.0" />
<PackageReference Include="AutoFixture.Xunit2" Version="4.17.0" />
<PackageReference Include="FakeItEasy" Version="7.3.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
33 changes: 22 additions & 11 deletions src/Contexts/Basket/Presentation/Basket.Presentation.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,34 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="6.0.4" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.12.2" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.0" />
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="6.0.5" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.25.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.11" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.HealthChecks" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.0" />
<PackageReference Include="Microsoft.AspNetCore.HealthChecks" Version="1.0.0" />
<PackageReference Include="AspNetCore.HealthChecks.Rabbitmq" Version="6.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.0" />

<PackageReference Include="Serilog.AspNetCore" Version="4.1.1-dev-00229" />
<PackageReference Include="Serilog.AspNetCore" Version="6.1.0" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.2.1-dev-00787" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.3.0-dev-00291" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1-dev-00876" />
<PackageReference Include="Serilog.Sinks.Http" Version="8.0.0-beta.9" />
<PackageReference Include="Serilog.Sinks.Seq" Version="4.1.0-dev-00166" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.4.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.Http" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Seq" Version="5.2.2" />

<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.1" />
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="6.2.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="6.4.0" />

<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.14.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.17.0" />


<PackageReference Include="Aggregates.NET" Version="0.18.7.112" />
<PackageReference Include="Aggregates.NET.NewtonsoftJson" Version="0.18.7.112" />
<PackageReference Include="Aggregates.NET.NServiceBus" Version="0.18.7.112" />
<PackageReference Include="NServiceBus" Version="8.0.1" />
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="2.0.0" />
<PackageReference Include="NServiceBus.RabbitMQ" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions src/Contexts/Basket/Presentation/BasketSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

public class BasketSettings
{
public string ConnectionString { get; set; }
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Basket.API.Infrastructure.Exceptions;

public class BasketDomainException : Exception
{
public BasketDomainException()
{ }

public BasketDomainException(string message)
: base(message)
{ }

public BasketDomainException(string message, Exception innerException)
: base(message, innerException)
{ }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using Basket.API.Infrastructure.Exceptions;
using Basket.Infrastructure;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System.Net;

namespace Basket.API.Infrastructure.Filters;

public partial class HttpGlobalExceptionFilter : IExceptionFilter
{
private readonly IWebHostEnvironment env;
private readonly ILogger<HttpGlobalExceptionFilter> logger;

public HttpGlobalExceptionFilter(IWebHostEnvironment env, ILogger<HttpGlobalExceptionFilter> logger)
{
this.env = env;
this.logger = logger;
}

public void OnException(ExceptionContext context)
{
logger.LogError(new EventId(context.Exception.HResult),
context.Exception,
context.Exception.Message);

if (context.Exception.GetType() == typeof(BasketDomainException))
{
var json = new JsonErrorResponse
{
Messages = new[] { context.Exception.Message }
};

context.Result = new BadRequestObjectResult(json);
context.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
}
else
{
var json = new JsonErrorResponse
{
Messages = new[] { "An error occurred. Try it again." }
};

if (env.IsDevelopment())
{
json.DeveloperMessage = context.Exception;
}

context.Result = new InternalServerErrorObjectResult(json);
context.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
}
context.ExceptionHandled = true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Basket.API.Infrastructure.Filters;

public class JsonErrorResponse
{
public string[] Messages { get; set; }

public object DeveloperMessage { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace Basket.API.Infrastructure.Filters;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

public class ValidateModelStateFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{
if (context.ModelState.IsValid)
{
return;
}

var validationErrors = context.ModelState
.Keys
.SelectMany(k => context.ModelState[k].Errors)
.Select(e => e.ErrorMessage)
.ToArray();

var json = new JsonErrorResponse
{
Messages = validationErrors
};

context.Result = new BadRequestObjectResult(json);
}
}

74 changes: 63 additions & 11 deletions src/Contexts/Basket/Presentation/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
using Aggregates;
using Basket.Extensions;
using Infrastructure;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Serilog;
using Serilog.Events;
using System.Net;

var configuration = GetConfiguration();

Log.Logger = CreateSerilogLogger(configuration);
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.Console()
.CreateBootstrapLogger();

try
{
Expand All @@ -28,8 +35,12 @@
Log.CloseAndFlush();
}

IWebHost BuildWebHost(IConfiguration configuration, string[] args) =>
WebHost.CreateDefaultBuilder(args)
IHost BuildWebHost(IConfiguration configuration, string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Host.UseSerilog(CreateSerilogLogger);
ConfigureNServiceBus(builder, configuration);
builder.WebHost
.CaptureStartupErrors(false)
.ConfigureKestrel(options =>
{
Expand All @@ -52,24 +63,65 @@ IWebHost BuildWebHost(IConfiguration configuration, string[] args) =>
options.NotFilteredPaths.AddRange(new[] { "/hc", "/liveness" });
})
.UseStartup<Startup>()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseSerilog()
.Build();
.UseContentRoot(Directory.GetCurrentDirectory());

var host = builder.Build();
host.UseSerilogRequestLogging();
return host;
}
void ConfigureNServiceBus(WebApplicationBuilder builder, IConfiguration configuration)
{

var endpointConfiguration = new EndpointConfiguration("Web");

Serilog.ILogger CreateSerilogLogger(IConfiguration configuration)
var transport = endpointConfiguration.UseTransport<RabbitMQTransport>();
transport.UseConventionalRoutingTopology(QueueType.Classic);
transport.ConnectionString(GetRabbitConnectionString(configuration));

endpointConfiguration.Pipeline.Register(
behavior: typeof(IncomingLoggingMessageBehavior),
description: "Logs incoming messages"
);
endpointConfiguration.Pipeline.Register(
behavior: typeof(OutgoingLoggingMessageBehavior),
description: "Logs outgoing messages"
);

builder.Host
.AddAggregatesNet(c => c
.NewtonsoftJson()
.NServiceBus(endpointConfiguration)
.SetCommandDestination("Domain"));
}
string GetRabbitConnectionString(IConfiguration config)
{
var host = config["RabbitConnection"];
var user = config["RabbitUserName"];
var password = config["RabbitPassword"];

if (string.IsNullOrEmpty(user))
return $"host={host}";

return $"host={host};username={user};password={password};";
}

void CreateSerilogLogger(HostBuilderContext context, IServiceProvider services, LoggerConfiguration logConfiguration)
{
var configuration = context.Configuration;
var seqServerUrl = configuration["Serilog:SeqServerUrl"];
var logstashUrl = configuration["Serilog:LogstashgUrl"];
return new LoggerConfiguration()
logConfiguration
.MinimumLevel.Verbose()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
.Enrich.WithProperty("ApplicationContext", Program.AppName)
.ReadFrom.Configuration(configuration)
.ReadFrom.Services(services)
.Enrich.FromLogContext()
.WriteTo.Console()
.WriteTo.Seq(string.IsNullOrWhiteSpace(seqServerUrl) ? "http://seq" : seqServerUrl)
.WriteTo.Http(string.IsNullOrWhiteSpace(logstashUrl) ? "http://logstash:8080" : logstashUrl)
.ReadFrom.Configuration(configuration)
.CreateLogger();
.WriteTo.Http(string.IsNullOrWhiteSpace(logstashUrl) ? "http://logstash:8080" : logstashUrl, queueLimitBytes: null);

}

IConfiguration GetConfiguration()
Expand Down
Loading

0 comments on commit afd9cf2

Please sign in to comment.