Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,6 @@ pub/
.devcontainer

.azure

# NServiceBus
.learningtransport
2 changes: 2 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
<PackageVersion Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
<PackageVersion Include="Microsoft.Win32.Primitives" Version="4.3.0" />
<PackageVersion Include="MinimalApi.Endpoint" Version="1.3.0" />
<PackageVersion Include="NServiceBus" Version="9.2.7" />
<PackageVersion Include="NServiceBus.Extensions.Hosting" Version="3.0.1" />
<PackageVersion Include="NimblePros.Metronome" Version="0.4.1" />
<PackageVersion Include="NSubstitute" Version="5.3.0" />
<PackageVersion Include="NSubstitute.Analyzers.CSharp" Version="1.0.17" />
Expand Down
7 changes: 7 additions & 0 deletions eShopOnWeb.sln
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "eShopWeb.AppHost", "src\eSh
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "eShopWeb.AspireServiceDefaults", "src\eShopWeb.AspireServiceDefaults\eShopWeb.AspireServiceDefaults.csproj", "{DC649B65-3762-44ED-9DF8-54F450CEA311}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "eShopOnWeb.Worker", "src\eShopOnWeb.Worker\eShopOnWeb.Worker.csproj", "{479A770A-EC51-E26C-D0F8-8F2254F6F1CB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -104,6 +106,10 @@ Global
{DC649B65-3762-44ED-9DF8-54F450CEA311}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DC649B65-3762-44ED-9DF8-54F450CEA311}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DC649B65-3762-44ED-9DF8-54F450CEA311}.Release|Any CPU.Build.0 = Release|Any CPU
{479A770A-EC51-E26C-D0F8-8F2254F6F1CB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{479A770A-EC51-E26C-D0F8-8F2254F6F1CB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{479A770A-EC51-E26C-D0F8-8F2254F6F1CB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{479A770A-EC51-E26C-D0F8-8F2254F6F1CB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -120,6 +126,7 @@ Global
{715CF7AF-A1EE-40A6-94A0-8DA3F3B2CAE9} = {419A6ACE-0419-4315-A6FB-B0E63D39432E}
{D53EF010-8F8C-4337-A059-456E19D8AE63} = {15EA4737-125B-4E6E-A806-E13B7EBCDCCF}
{9269A0CE-2596-4215-8ACA-765724A1630C} = {419A6ACE-0419-4315-A6FB-B0E63D39432E}
{479A770A-EC51-E26C-D0F8-8F2254F6F1CB} = {419A6ACE-0419-4315-A6FB-B0E63D39432E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {49813262-5DA3-4D61-ABD3-493C74CE8C2B}
Expand Down
3 changes: 2 additions & 1 deletion src/ApplicationCore/ApplicationCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
<ItemGroup>
<PackageReference Include="Ardalis.GuardClauses" />
<PackageReference Include="Ardalis.Result" />
<PackageReference Include="Ardalis.Specification" />
<PackageReference Include="Ardalis.Specification" />
<PackageReference Include="NServiceBus" />
<PackageReference Include="System.Security.Claims" />
<PackageReference Include="System.Text.Json" />
</ItemGroup>
Expand Down
23 changes: 23 additions & 0 deletions src/ApplicationCore/Configuration/NServiceBusConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate.Events;
using NServiceBus;

namespace Microsoft.eShopWeb.ApplicationCore.Configuration;
public static class NServiceBusConfiguration
{
public static EndpointConfiguration GetNServiceBusConfiguration()
{
var endpointConfiguration = new EndpointConfiguration("orders-worker");
endpointConfiguration.UseSerialization<SystemJsonSerializer>();

var transport = endpointConfiguration.UseTransport<LearningTransport>();

transport.Routing().RouteToEndpoint(
typeof(OrderCreatedEvent),
"orders-worker");

endpointConfiguration.SendFailedMessagesTo("error");
endpointConfiguration.AuditProcessedMessagesTo("audit");

return endpointConfiguration;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.Json.Serialization;
using NServiceBus;

namespace Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate.Events;
public class OrderCreatedEvent :IMessage
{
public int Id { get; init; }
public string? BuyerId { get; init; }
public DateTimeOffset OrderDate { get; init; }
public decimal Total { get; init; }
public IReadOnlyCollection<OrderItem> Items { get; init; } = [];
public OrderCreatedEvent() { }

public OrderCreatedEvent(int orderId, string? buyerId, DateTimeOffset orderDate, decimal orderTotal, IReadOnlyCollection<OrderItem> orderItems) {
Id = orderId;
BuyerId = buyerId;
OrderDate = orderDate;
Total = orderTotal;
Items = orderItems;
}
}
10 changes: 8 additions & 2 deletions src/ApplicationCore/Services/OrderService.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Ardalis.GuardClauses;
using Microsoft.eShopWeb.ApplicationCore.Entities;
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate.Events;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using Microsoft.eShopWeb.ApplicationCore.Specifications;
using NServiceBus;

namespace Microsoft.eShopWeb.ApplicationCore.Services;

Expand All @@ -15,16 +18,17 @@ public class OrderService : IOrderService
private readonly IUriComposer _uriComposer;
private readonly IRepository<Basket> _basketRepository;
private readonly IRepository<CatalogItem> _itemRepository;

private readonly IMessageSession _messageSession;
public OrderService(IRepository<Basket> basketRepository,
IRepository<CatalogItem> itemRepository,
IRepository<Order> orderRepository,
IUriComposer uriComposer)
IUriComposer uriComposer, IMessageSession messageSession)
{
_orderRepository = orderRepository;
_uriComposer = uriComposer;
_basketRepository = basketRepository;
_itemRepository = itemRepository;
_messageSession = messageSession;
}

public async Task CreateOrderAsync(int basketId, Address shippingAddress)
Expand All @@ -49,5 +53,7 @@ public async Task CreateOrderAsync(int basketId, Address shippingAddress)
var order = new Order(basket.BuyerId, shippingAddress, items);

await _orderRepository.AddAsync(order);
OrderCreatedEvent orderCreatedEvent = new(order.Id,order.BuyerId,order.OrderDate, order.Total(), order.OrderItems);
await _messageSession.Send(orderCreatedEvent,CancellationToken.None);
}
}
9 changes: 8 additions & 1 deletion src/Web/Program.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
using Ardalis.ListStartupServices;
using System.Text.Json;
using Ardalis.ListStartupServices;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate.Events;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using Microsoft.eShopWeb.Infrastructure.Identity;
using Microsoft.eShopWeb.Web;
using Microsoft.eShopWeb.Web.Areas.Identity.Helpers;
using Microsoft.eShopWeb.Web.Configuration;
using Microsoft.eShopWeb.Web.Extensions;
using NServiceBus;
using NimblePros.Metronome;
using Microsoft.eShopWeb.ApplicationCore.Configuration;

var builder = WebApplication.CreateBuilder(args);

Expand Down Expand Up @@ -48,6 +52,9 @@
});
}

// NServiceBus
builder.Host.UseNServiceBus( config => NServiceBusConfiguration.GetNServiceBusConfiguration());

builder.Services.AddScoped<ITokenClaimsService, IdentityTokenClaimService>();
builder.Services.AddCoreServices(builder.Configuration);
builder.Services.AddWebServices(builder.Configuration);
Expand Down
3 changes: 2 additions & 1 deletion src/Web/Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NimblePros.Metronome" />
<PackageReference Include="NimblePros.Metronome" />
<PackageReference Include="NServiceBus.Extensions.Hosting" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" />
<PackageReference Include="System.Text.Encoding.Extensions" />
<PackageReference Include="System.Threading" />
Expand Down
23 changes: 23 additions & 0 deletions src/eShopOnWeb.Worker/Handlers/OrderCreatedHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Threading.Tasks;
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate.Events;
using Microsoft.Extensions.Logging;
using NServiceBus;

namespace eShopOnWeb.Worker.Handlers;

public class OrderCreatedEventHandler(ILogger<OrderCreatedEventHandler> logger)
: IHandleMessages<OrderCreatedEvent>
{
private readonly ILogger<OrderCreatedEventHandler> _logger = logger;

public Task Handle(OrderCreatedEvent message, IMessageHandlerContext context)
{
_logger.LogInformation("{OrderDate} - Received {EventName} - Order {OrderId} from buyer {BuyerId}: {OrderTotal:C}\nTotal Units: {itemCount}\nTotal Line Items: {uniqueItems}",
nameof(OrderCreatedEvent),
message.OrderDate,message.Id,message.BuyerId,message.Total,
message.Items.Sum(x => x.Units),
message.Items.Count);
return Task.CompletedTask;
}
}

11 changes: 11 additions & 0 deletions src/eShopOnWeb.Worker/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Text.Json;
using Microsoft.eShopWeb.ApplicationCore.Configuration;
using NServiceBus;

var builder = Host.CreateDefaultBuilder();

builder.UseConsoleLifetime();
builder.UseNServiceBus(context => NServiceBusConfiguration.GetNServiceBusConfiguration());

var host = builder.Build();
host.Run();
12 changes: 12 additions & 0 deletions src/eShopOnWeb.Worker/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"eShopOnWeb.Worker": {
"commandName": "Project",
"dotnetRunMessages": true,
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Development"
}
}
}
}
8 changes: 8 additions & 0 deletions src/eShopOnWeb.Worker/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
8 changes: 8 additions & 0 deletions src/eShopOnWeb.Worker/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
19 changes: 19 additions & 0 deletions src/eShopOnWeb.Worker/eShopOnWeb.Worker.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk.Worker">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>dotnet-eShopOnWeb.Worker-30ced907-7698-44a1-9c83-71b84755240f</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NServiceBus" />
<PackageReference Include="NServiceBus.Extensions.Hosting" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ApplicationCore\ApplicationCore.csproj" />
<ProjectReference Include="..\eShopWeb.AspireServiceDefaults\eShopWeb.AspireServiceDefaults.csproj" />
</ItemGroup>
</Project>
4 changes: 3 additions & 1 deletion src/eShopWeb.AppHost/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var builder = DistributedApplication.CreateBuilder(args);
var builder = DistributedApplication.CreateBuilder(args);

var seq = builder.AddSeq("seq")
.ExcludeFromManifest()
Expand All @@ -14,4 +14,6 @@
.WithReference(seq)
.WaitFor(seq);

builder.AddProject<Projects.eShopOnWeb_Worker>("eshoponweb-worker");

builder.Build().Run();
1 change: 1 addition & 0 deletions src/eShopWeb.AppHost/eShopWeb.AppHost.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

<ItemGroup>
<ProjectReference Include="..\BlazorAdmin\BlazorAdmin.csproj" />
<ProjectReference Include="..\eShopOnWeb.Worker\eShopOnWeb.Worker.csproj" />
<ProjectReference Include="..\PublicApi\PublicApi.csproj" />
<ProjectReference Include="..\Web\Web.csproj" />
</ItemGroup>
Expand Down