Skip to content

Commit a3f0e37

Browse files
Update MassTransitDemo readme
1 parent bc26d50 commit a3f0e37

File tree

1 file changed

+1
-141
lines changed

1 file changed

+1
-141
lines changed

MassTransitDemo/README.md

+1-141
Original file line numberDiff line numberDiff line change
@@ -1,143 +1,3 @@
11
# Using MassTransit via eShopOnAbp
22

3-
[MassTransit](https://masstransit-project.com/) is a lightweight service bus for building distributed .NET applications. MassTransit makes it easy to create applications and services. It works with different integrations.
4-
- In Memory
5-
- RabbitMQ
6-
- Azure Service Bus
7-
- Amazon SQS
8-
- gRPC
9-
- ActiveMQ
10-
11-
As you know, `RabbitMQ` is one of the most popular open-source message brokers which is used by many developers. So we will continue with it. Also, you can implement the others easily as well.
12-
13-
MassTransit also supports exceptions, retries, sagas, scheduling and more features. We're planning to write some new articles about them.
14-
15-
In this article, we will show you how to use `MassTransit` in the [eShopOnAbp](https://github.com/abpframework/eShopOnAbp) project instead of `Volo.Abp.EventBus.Distributed`.
16-
17-
In eShopOnAbp, once the order service cancels the order, it publishes an event. The configured services can consume it. In this case, the just payment service will do that. The system already works properly, but we will make changes without touching the business logic code and use MassTransit abilities.
18-
19-
Firstly, let's focus on the publishing event side (order service) and then we will focus on the consumer side (payment service).
20-
21-
Let's do its configuration in `OrderingServiceHttpApiHostModule` under the `EShopOnAbp.OrderingService.HttpApi.Host`
22-
```csharp
23-
public class OrderingServiceHttpApiHostModule : AbpModule
24-
{
25-
public override void ConfigureServices(ServiceConfigurationContext context)
26-
{
27-
//the other configurations
28-
context.Services.AddMassTransit(x =>
29-
{
30-
x.AddBus(provider => Bus.Factory.CreateUsingRabbitMq(config =>
31-
{
32-
config.Host("host", h =>
33-
{
34-
h.Username("username");
35-
h.Password("pwd");
36-
});
37-
}));
38-
});
39-
}
40-
}
41-
```
42-
43-
To complete this configuration you need to install the related packages on NuGet or via the below code
44-
> Install-Package MassTransit.RabbitMQ -Version 8.0.6
45-
46-
Let's do its implemantation on `OrderManager` under `EShopOnAbp.OrderingService.Domain`
47-
As we mentioned above, the system already works properly so firstly we will remove/comment on the old published code and write the new codes.
48-
49-
```csharp
50-
public class OrderManager : DomainService
51-
{
52-
//...
53-
private readonly IBus _bus;
54-
55-
public OrderManager(
56-
//..
57-
IBus bus)
58-
{
59-
//..
60-
_bus = bus;
61-
}
62-
63-
public async Task<Order> CancelOrderAsync(Guid orderId)
64-
{
65-
var order = await _orderRepository.GetAsync(orderId);
66-
if (order == null)
67-
{
68-
throw new BusinessException(OrderingServiceErrorCodes.OrderWithIdNotFound)
69-
.WithData("OrderId", orderId);
70-
}
71-
72-
order.SetOrderCancelled();
73-
74-
//Just used _bus(IBus) instead of _distributedEventBus(IDistributedEventBus)
75-
await _bus.Publish(new OrderCancelledEto
76-
{
77-
PaymentRequestId = order.PaymentRequestId.GetValueOrDefault(),
78-
OrderId = order.Id,
79-
OrderDate = order.OrderDate,
80-
OrderNo = order.OrderNo,
81-
Buyer = GetBuyerEto(order.Buyer),
82-
Items = GetProductItemEtoList(order.OrderItems)
83-
});
84-
85-
return await _orderRepository.UpdateAsync(order, autoSave: true);
86-
}
87-
```
88-
89-
Note: You can define your model `OrderCancelledEto` as a class, interface or record but MassTransit recommends [record](https://docs.microsoft.com/en-US/dotnet/csharp/whats-new/csharp-9#record-types) as the best practice.
90-
91-
We're ready to focus on the consumer side (payment service). Firstly let's implement the configuration in `PaymentServiceHttpApiHostModule` under the `EShopOnAbp.PaymentService.HttpApi.Host`
92-
93-
```csharp
94-
public class PaymentServiceHttpApiHostModule : AbpModule
95-
{
96-
public override void ConfigureServices(ServiceConfigurationContext context)
97-
{
98-
context.Services.AddMassTransit(x =>
99-
{
100-
x.AddConsumer<OrderCancelledConsumer>();
101-
102-
x.AddBus(provider => Bus.Factory.CreateUsingRabbitMq(cfg =>
103-
{
104-
cfg.Host("host", h =>
105-
{
106-
h.Username("username");
107-
h.Password("pwd");
108-
});
109-
cfg.ReceiveEndpoint("order-service", ep =>
110-
{
111-
ep.ConfigureConsumer<OrderCancelledConsumer>(provider);
112-
});
113-
}));
114-
});
115-
}
116-
}
117-
```
118-
119-
Let's create `OrderCancelledConsumer` in the `EShopOnAbp.PaymentService.Domain`
120-
```csharp
121-
public class OrderCancelledConsumer : IConsumer<OrderCancelledEto>
122-
{
123-
private readonly IPaymentRequestRepository _paymentRepository;
124-
public OrderCancelledConsumer(IPaymentRequestRepository paymentRepository)
125-
{
126-
_paymentRepository = paymentRepository;
127-
}
128-
public async Task Consume(ConsumeContext<OrderCancelledEto> context)
129-
{
130-
//Write your business code ...
131-
var payment = await _paymentRepository.GetAsync(context.Message.PaymentRequestId);
132-
Log.Information($"Cancelled the order: {payment.OrderId} payment:{payment.Id}");
133-
}
134-
}
135-
```
136-
137-
To complete this configuration you need to install the related packages on NuGet or via the below code
138-
> Install-Package MassTransit.RabbitMQ -Version 8.0.6
139-
140-
141-
In this article, we demonstrated how to publish and consume the events with the eShopOnAbp project by using MassTransit. For more detail, you can visit its [own official website](https://masstransit-project.com/getting-started/).
142-
143-
Regards.
3+
**https://abp.io/community/articles/using-masstransit-via-eshoponabp-8amok6h8**

0 commit comments

Comments
 (0)