Easily use Aspire RabbitMQ

Easily use Aspire RabbitMQ

NET Aspire is a technology stack launched by Microsoft that aims to simplify the development and management of cloud-native applications.

最后更新 5/1/2024 10:38 PM
Sky.楚子航
预计阅读 4 分钟
分类
.NET
标签
.NET C# Aspire RabbitMQ

This article was submitted by netizen [Sky. Chu Zihang]

1. Learn about. NET Aspire

.NET Aspire 是微软推出的一个技术栈,旨在简化云原生应用的开发和管理。以下是关于.NET Aspire 的详细介绍:

  1. ** Definition and Purpose **:

    • NET Aspire is a fixed cloud-ready technology stack used to build observable and production-ready distributed applications.
    • Its main purpose is to simplify the coordination and management of various elements within cloud-native applications and help developers build cloud-native applications more efficiently using. NET.
  2. ** Features and Advantages **:

    • NET Aspire provides a unified project format and a fixed technology stack, which helps reduce developer complexity when selecting and configuring technology components.
    • It includes a select set of components for cloud-native enhancements, such as service discovery, telemetry, resilience, and health checks, which are key elements in cloud-native application development.
    • NET Aspire also provides rich APIs and tools that allow developers to express resources and dependencies in distributed applications, further simplifying the development and operation and maintenance of cloud-native applications.
  3. ** Relationship with. NET **:

    • NET Aspire is built on the. NET platform, taking full advantage of the power of. NET and ecosystem.
    • As part of. NET,. NET Aspire is closely integrated with. NET 8 and later, providing developers with full process support from development to deployment.
  4. ** Release and Availability **:

    • Microsoft introduced. NET Aspire for the first time in a preview version of. NET 8 and plans to release it as part of the official version of. NET 8.
    • Developers can try out. NET Aspire in a preview version of. NET 8 and experience the simplicity and convenience it brings.

In general,. NET Aspire is an important technology introduced by Microsoft to simplify the development and management of cloud-native applications. It makes full use of the advantages of the. NET platform and provides developers with an efficient and unified solution.

2. present context

I have been using Preview1 - Preview6 (currently the latest 2024/5/1). In the first version, I used it to put it into one of my microservices (https://gitee.com/SkyNingDuan/PublicActivityServices.git) and kept updating it iteratively.

在其中,我一直使用外部 RabbitMQ 的方式给我的微服务传递消息(用的是Zack.EventBus),但是它一直有直接通过 Aspire 方式创建 RabbitMQ 容器在你的项目中使用, 我一直想着用杨中科老师的框架为指导,开发一个在 Aspire 环境下的 EventBus, 但是一直拖着(已经有现成的了,就一直不想走出舒适区)最后经过不断的自我抗争,simpleUseAspireRabbitmq 第一版开发好了,功能比较简单,也比较好用,如果大家热情高的话, 后面再加便是。欢迎大家拥抱新技术,有任何问题都可以提 issue 和我互动,源代码地址https://github.com/skyDuanXianBing/SimpleUseAspireRabbitMQ.git ,NuGet 名称:SimpleUseAspireRabbitMQ) (目前由于 Aspire 也是处于预览版,所以这个也是预览版,后面有任何改进也会跟进的)。

3. usage tutorial

3.1. Creating the Aspire Project

在 aspire.host 中安装Aspire.Hosting.RabbitMQ 包,在program.cs中创建RabbitMQ容器,并且在你要使用 RabbitMQ 的项目后 WithReference RabbitMQ容器: img

3.2. Registration service

在你要使用RabbitMQ项目的 program.cs 中分别加入builder.EventConfiguration("rabbitmq", "myexchange");(第一个参数是RabbitMQ容器名称,第二是交换机名称),app.RegisterRabbitmqEvent();来注册服务: img

3.3. Test Send Message

使用IEventBus发送消息,目前仅支持 string/泛型数据(都会转换成 json,后面在反序列化),publish 第一个参数是队列名称:

img

3.4. Define processing classes

一定要定义在网站项目中(因为是通过反射网站项目拿到全部的处理类),继承IEventJsonHandler/IEventStringHandler 分别实现就行。

一定要在处理类上贴 [event("XXX")], 这个 attribute 用来指示接收哪个队列信息:

img

img

3.5. Perfect reception of news

img

4. summary

Use is that simple, please leave a message and communicate.

Keep Exploring

延伸阅读

更多文章
同分类 / 同标签 4/22/2026

Support for. NET by operating system versions (250707 update)

Use virtual machines and test machines to test the support of each version of the operating system for. NET. After installing the operating system, it is passed by measuring the corresponding running time of the installation and being able to run the Stardust Agent.

继续阅读
同分类 / 同标签 2/7/2026

Summary of experience in using AOT

From the very beginning of project creation, you should develop a good habit of conducting AOT release testing in a timely manner whenever new features are added or newer syntax is used.

继续阅读