Skip to content

Commit 33d7b17

Browse files
authored
Merge pull request #2 from NimblePros/sadukie/nservicebus-domain-events
Adding code and README notes for NServiceBus
2 parents e399dae + c4a5f45 commit 33d7b17

File tree

18 files changed

+190
-10
lines changed

18 files changed

+190
-10
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,6 @@ pub/
260260
.devcontainer
261261

262262
.azure
263+
264+
# NServiceBus
265+
.learningtransport

Directory.Packages.props

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
<PackageVersion Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
5050
<PackageVersion Include="Microsoft.Win32.Primitives" Version="4.3.0" />
5151
<PackageVersion Include="MinimalApi.Endpoint" Version="1.3.0" />
52+
<PackageVersion Include="NServiceBus" Version="9.2.7" />
53+
<PackageVersion Include="NServiceBus.Extensions.Hosting" Version="3.0.1" />
5254
<PackageVersion Include="NimblePros.Metronome" Version="0.4.1" />
5355
<PackageVersion Include="NSubstitute" Version="5.3.0" />
5456
<PackageVersion Include="NSubstitute.Analyzers.CSharp" Version="1.0.17" />

README.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
1-
# Microsoft eShopOnWeb ASP.NET Core Reference Application
1+
# eShopOnNServiceBus - Microsoft eShopOnWeb ASP.NET Core Reference Application + NServiceBus
22

3-
This Microsoft reference application is now being maintained by [NimblePros](https://nimblepros.com/).
3+
This repo contains a snapshot of eShopOnWeb as of June 2025.
44

5-
If you're looking for the .NET Aspire eShop sample it's [here](https://github.com/dotnet/eShop). Microsoft also recommends the [Reliable Web App](https://learn.microsoft.com/azure/architecture/web-apps/guides/reliable-web-app/overview) patterns guidance for building web apps with enterprise app patterns.
5+
The purpose of this repo is to show how you can use [NServiceBus](https://go.particular.net/using-nservicebus-2024) in .NET apps.
66

7-
Sample ASP.NET Core reference application, powered by Microsoft, demonstrating a single-process (monolithic) application architecture and deployment model. If you're new to .NET development, read the [Getting Started for Beginners](https://github.com/nimblepros/eShopOnWeb/wiki/Getting-Started-for-Beginners) guide.
7+
This demo was shown as part of [the NimblePros' webinar on domain events](https://www.youtube.com/live/29-kl2V2sps?si=EhkwoRqZsypvu2DE).
8+
9+
This repo will be expanded on for [the NimblePros' webinar on NServiceBus](https://www.youtube.com/live/YWHY09R46m4?si=TLzKCk0FnWl0qSwv).
10+
11+
## Other eShop samples
12+
13+
- [.NET Aspire eShop sample](https://github.com/dotnet/eShop)
14+
- Microsoft also recommends the [Reliable Web App](https://learn.microsoft.com/azure/architecture/web-apps/guides/reliable-web-app/overview) patterns guidance for building web apps with enterprise app patterns.
15+
- [eShopOnWeb without NServiceBus](https://github.com/NimblePros/eShopOnWeb)
16+
17+
## Learn more about NServiceBus + NimblePros' Relationship with It
18+
19+
- [Meet our Particular Software Recognized Professionals](https://blog.nimblepros.com/blogs/introducing-our-particular-software-recognized-professionals/)
20+
- [NimblePros Named a Particular Partner for Expertise in NServiceBus](https://blog.nimblepros.com/blogs/nimblepros-particular-partner/)
21+
- [NimblePros Blog Series on NServiceBus](https://blog.nimblepros.com/series/nservicebus/) including:
22+
- [Introduction](https://blog.nimblepros.com/blogs/what-is-nservicebus/) and [Getting Started](https://blog.nimblepros.com/blogs/getting-started-with-nservicebus/)
23+
- Concepts such as [asychronous messaging](https://blog.nimblepros.com/blogs/what-is-asynchronous-messaging/) and [commands, events, and messages](https://blog.nimblepros.com/blogs/commands-events-messages-explained/)
24+
- [Testing Message Handlers](https://blog.nimblepros.com/blogs/testing-nservicebus-message-handlers/)
25+
- Sagas - [Introduction](https://blog.nimblepros.com/blogs/supercharged-sagas-introduction/), [create your first saga](https://blog.nimblepros.com/blogs/supercharged-sagas-creating-your-first-nservicebus-saga/), [timeout tips](https://blog.nimblepros.com/blogs/supercharged-sagas-timeout-tips/), and [testing strategies](https://blog.nimblepros.com/blogs/supercharged-sagas-unit-testing-strategies/)
26+
- And some fun! [Automating Santa's Workshop with NServiceBus](https://blog.nimblepros.com/blogs/automating-santas-workshop-with-nservicebus/)
27+
28+
## More from the README on eShopOnWeb
829

930
A list of Frequently Asked Questions about this repository can be found [here](https://github.com/nimblepros/eShopOnWeb/wiki/Frequently-Asked-Questions).
1031

eShopOnWeb.sln

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ EndProject
2424
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{0BD72BEA-EF42-4B72-8B69-12A39EC76FBA}"
2525
ProjectSection(SolutionItems) = preProject
2626
.editorconfig = .editorconfig
27+
.gitignore = .gitignore
2728
.github\workflows\comment-on-pr.yml = .github\workflows\comment-on-pr.yml
2829
Directory.Packages.props = Directory.Packages.props
2930
docker-compose.override.yml = docker-compose.override.yml
@@ -46,6 +47,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "eShopWeb.AppHost", "src\eSh
4647
EndProject
4748
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "eShopWeb.AspireServiceDefaults", "src\eShopWeb.AspireServiceDefaults\eShopWeb.AspireServiceDefaults.csproj", "{DC649B65-3762-44ED-9DF8-54F450CEA311}"
4849
EndProject
50+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "eShopOnWeb.Worker", "src\eShopOnWeb.Worker\eShopOnWeb.Worker.csproj", "{479A770A-EC51-E26C-D0F8-8F2254F6F1CB}"
51+
EndProject
4952
Global
5053
GlobalSection(SolutionConfigurationPlatforms) = preSolution
5154
Debug|Any CPU = Debug|Any CPU
@@ -104,6 +107,10 @@ Global
104107
{DC649B65-3762-44ED-9DF8-54F450CEA311}.Debug|Any CPU.Build.0 = Debug|Any CPU
105108
{DC649B65-3762-44ED-9DF8-54F450CEA311}.Release|Any CPU.ActiveCfg = Release|Any CPU
106109
{DC649B65-3762-44ED-9DF8-54F450CEA311}.Release|Any CPU.Build.0 = Release|Any CPU
110+
{479A770A-EC51-E26C-D0F8-8F2254F6F1CB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
111+
{479A770A-EC51-E26C-D0F8-8F2254F6F1CB}.Debug|Any CPU.Build.0 = Debug|Any CPU
112+
{479A770A-EC51-E26C-D0F8-8F2254F6F1CB}.Release|Any CPU.ActiveCfg = Release|Any CPU
113+
{479A770A-EC51-E26C-D0F8-8F2254F6F1CB}.Release|Any CPU.Build.0 = Release|Any CPU
107114
EndGlobalSection
108115
GlobalSection(SolutionProperties) = preSolution
109116
HideSolutionNode = FALSE
@@ -120,6 +127,7 @@ Global
120127
{715CF7AF-A1EE-40A6-94A0-8DA3F3B2CAE9} = {419A6ACE-0419-4315-A6FB-B0E63D39432E}
121128
{D53EF010-8F8C-4337-A059-456E19D8AE63} = {15EA4737-125B-4E6E-A806-E13B7EBCDCCF}
122129
{9269A0CE-2596-4215-8ACA-765724A1630C} = {419A6ACE-0419-4315-A6FB-B0E63D39432E}
130+
{479A770A-EC51-E26C-D0F8-8F2254F6F1CB} = {419A6ACE-0419-4315-A6FB-B0E63D39432E}
123131
EndGlobalSection
124132
GlobalSection(ExtensibilityGlobals) = postSolution
125133
SolutionGuid = {49813262-5DA3-4D61-ABD3-493C74CE8C2B}

src/ApplicationCore/ApplicationCore.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
<ItemGroup>
99
<PackageReference Include="Ardalis.GuardClauses" />
1010
<PackageReference Include="Ardalis.Result" />
11-
<PackageReference Include="Ardalis.Specification" />
11+
<PackageReference Include="Ardalis.Specification" />
12+
<PackageReference Include="NServiceBus" />
1213
<PackageReference Include="System.Security.Claims" />
1314
<PackageReference Include="System.Text.Json" />
1415
</ItemGroup>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate.Events;
2+
using NServiceBus;
3+
4+
namespace Microsoft.eShopWeb.ApplicationCore.Configuration;
5+
public static class NServiceBusConfiguration
6+
{
7+
public static EndpointConfiguration GetNServiceBusConfiguration()
8+
{
9+
var endpointConfiguration = new EndpointConfiguration("orders-worker");
10+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
11+
12+
var transport = endpointConfiguration.UseTransport<LearningTransport>();
13+
14+
transport.Routing().RouteToEndpoint(
15+
typeof(OrderCreatedEvent),
16+
"orders-worker");
17+
18+
endpointConfiguration.SendFailedMessagesTo("error");
19+
endpointConfiguration.AuditProcessedMessagesTo("audit");
20+
21+
return endpointConfiguration;
22+
}
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Text.Json.Serialization;
5+
using NServiceBus;
6+
7+
namespace Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate.Events;
8+
public class OrderCreatedEvent :IMessage
9+
{
10+
public int Id { get; init; }
11+
public string? BuyerId { get; init; }
12+
public DateTimeOffset OrderDate { get; init; }
13+
public decimal Total { get; init; }
14+
public IReadOnlyCollection<OrderItem> Items { get; init; } = [];
15+
public OrderCreatedEvent() { }
16+
17+
public OrderCreatedEvent(int orderId, string? buyerId, DateTimeOffset orderDate, decimal orderTotal, IReadOnlyCollection<OrderItem> orderItems) {
18+
Id = orderId;
19+
BuyerId = buyerId;
20+
OrderDate = orderDate;
21+
Total = orderTotal;
22+
Items = orderItems;
23+
}
24+
}

src/ApplicationCore/Services/OrderService.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
using System.Linq;
2+
using System.Threading;
23
using System.Threading.Tasks;
34
using Ardalis.GuardClauses;
45
using Microsoft.eShopWeb.ApplicationCore.Entities;
56
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
67
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
8+
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate.Events;
79
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
810
using Microsoft.eShopWeb.ApplicationCore.Specifications;
11+
using NServiceBus;
912

1013
namespace Microsoft.eShopWeb.ApplicationCore.Services;
1114

@@ -15,16 +18,17 @@ public class OrderService : IOrderService
1518
private readonly IUriComposer _uriComposer;
1619
private readonly IRepository<Basket> _basketRepository;
1720
private readonly IRepository<CatalogItem> _itemRepository;
18-
21+
private readonly IMessageSession _messageSession;
1922
public OrderService(IRepository<Basket> basketRepository,
2023
IRepository<CatalogItem> itemRepository,
2124
IRepository<Order> orderRepository,
22-
IUriComposer uriComposer)
25+
IUriComposer uriComposer, IMessageSession messageSession)
2326
{
2427
_orderRepository = orderRepository;
2528
_uriComposer = uriComposer;
2629
_basketRepository = basketRepository;
2730
_itemRepository = itemRepository;
31+
_messageSession = messageSession;
2832
}
2933

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

5155
await _orderRepository.AddAsync(order);
56+
OrderCreatedEvent orderCreatedEvent = new(order.Id,order.BuyerId,order.OrderDate, order.Total(), order.OrderItems);
57+
await _messageSession.Send(orderCreatedEvent,CancellationToken.None);
5258
}
5359
}

src/Web/Program.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
using Ardalis.ListStartupServices;
1+
using System.Text.Json;
2+
using Ardalis.ListStartupServices;
23
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
34
using Microsoft.AspNetCore.Identity;
45
using Microsoft.AspNetCore.Mvc.ApplicationModels;
6+
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate.Events;
57
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
68
using Microsoft.eShopWeb.Infrastructure.Identity;
79
using Microsoft.eShopWeb.Web;
810
using Microsoft.eShopWeb.Web.Areas.Identity.Helpers;
911
using Microsoft.eShopWeb.Web.Configuration;
1012
using Microsoft.eShopWeb.Web.Extensions;
13+
using NServiceBus;
1114
using NimblePros.Metronome;
15+
using Microsoft.eShopWeb.ApplicationCore.Configuration;
1216

1317
var builder = WebApplication.CreateBuilder(args);
1418

@@ -48,6 +52,9 @@
4852
});
4953
}
5054

55+
// NServiceBus
56+
builder.Host.UseNServiceBus( config => NServiceBusConfiguration.GetNServiceBusConfiguration());
57+
5158
builder.Services.AddScoped<ITokenClaimsService, IdentityTokenClaimService>();
5259
builder.Services.AddCoreServices(builder.Configuration);
5360
builder.Services.AddWebServices(builder.Configuration);

src/Web/Web.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
<PrivateAssets>all</PrivateAssets>
3636
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
3737
</PackageReference>
38-
<PackageReference Include="NimblePros.Metronome" />
38+
<PackageReference Include="NimblePros.Metronome" />
39+
<PackageReference Include="NServiceBus.Extensions.Hosting" />
3940
<PackageReference Include="System.IdentityModel.Tokens.Jwt" />
4041
<PackageReference Include="System.Text.Encoding.Extensions" />
4142
<PackageReference Include="System.Threading" />

0 commit comments

Comments
 (0)