.NET 帮助 NServiceBus C#(开发者用法) Jacob Mellor 已更新:2026年1月18日 下载 IronPDF NuGet 下载 DLL 下载 Windows 安装程序 免费试用 LLM副本 LLM副本 将页面复制为 Markdown 格式,用于 LLMs 在 ChatGPT 中打开 向 ChatGPT 咨询此页面 在双子座打开 向 Gemini 询问此页面 在 Grok 中打开 向 Grok 询问此页面 打开困惑 向 Perplexity 询问有关此页面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 复制链接 电子邮件文章 NServiceBus 是一款功能强大且适应性强的服务总线,专为 .NET Framework 设计,可简化分布式系统开发。 它提供的强大消息模式保证跨多个微服务和应用程序的可靠消息处理和传递。 NServiceBus 抽象了底层的消息架构,使开发人员能够专注于业务逻辑,而不是构建分布式系统的复杂性。 相比之下,IronPDF 是一个流行的 .NET 库,用于生成、查看和修改 PDF 文件。 它因易于使用和高效地从各种来源(如 ASPX 文件和 HTML)创建 PDF 而闻名。 通过结合使用 NServiceBus 和 IronPDF,开发人员可以构建可靠、可扩展且可维护的软件系统,这些系统可以在其业务运营中生成和管理 PDF 文档。 在本文中,我们将学习如何设置一个简单的 C# NServiceBus 项目并将其与 IronPDF 集成,以便您可以在分布式应用程序架构中构建流畅的工作流程来管理和生成 PDF 文档。 阅读此入门教程后,您应该确切地了解这两种有效技术如何合作以简化分布式环境中的 PDF 相关任务。 什么是 NServiceBus C#? NServiceBus 是一个功能强大且灵活的框架,使创建分布式系统和面向服务的 .NET 架构变得简单。 通过使用 NServiceBus,您可以轻松管理各种消息类型并确保可靠通信。 这尤其重要,特别是在网络应用程序和类似架构中,顺畅的消息路由和处理至关重要。 NServiceBus 的消息处理程序有效地处理接收消息,确保每个逻辑组件顺利互动。 NServiceBus 的重要功能如下: NServiceBus 的特性 基于消息的通信 NServiceBus 鼓励系统中不同服务或组件之间的基于消息的通信。 通过解耦组件,这种方法创建了更易于扩展和管理的设计。 可靠的消息传递 通过自动管理重试、死信队列和其他故障容忍技术,确保可靠的消息传递。 在分布式系统中,网络中断和其他故障问题频繁发生,这种可靠性至关重要。 发布/订阅模型 NServiceBus 支持发布/订阅模式,使服务能够发布事件并让其他服务订阅这些事件。 这使得事件驱动架构成为可能,即系统中一部分的事件修改可以引发其他部分的响应。 Saga 管理 得益于其对saga的集成支持,长时间运行的业务流程可以通过NServiceBus进行管理。 Saga 使服务平台能够管理状态并在多个服务之间协调复杂操作。 扩展性和定制化 它提供了卓越的扩展性,使开发人员能够个性化消息处理、处理和传输过程。 由于其适应性,适用于多种场景。 与各种消息平台的集成 NServiceBus 可与包括 MSMQ、RabbitMQ、Azure Service Bus、Amazon SQS 等在内的多种消息系统集成。 这使开发人员能够选择最符合他们需求的通信基础设施解决方案。 Create and configure NServiceBus in C# 您首先需要设置开发环境,创建一个基本项目,并构建一个基本消息服务和场景,然后才能在 C# 项目中开始使用 NServiceBus。 以下是逐步指南,帮助您入门。 创建新的Visual Studio项目 在 Visual Studio 中,创建控制台项目的过程很简单。 在 Visual Studio 环境中使用以下简单步骤来启动控制台应用程序: 在使用之前,请确保您已在计算机上安装了Visual Studio。 启动新项目 点击文件,然后选择新建,最后选择项目。 您可以从下面的项目模板列表中选择"控制台应用程序"或"控制台应用程序(.NET Core)"模板。 在"名称"字段中为您的项目提供名称。 为项目选择存储位置。 点击"创建"将开始控制台应用程序项目。 安装 NServiceBus 包 导航到工具 > NuGet 包管理器 > 包管理器控制台以打开 NuGet 包管理器控制台。 运行以下命令以安装 NServiceBus NuGet 包。 Install-Package NServiceBus 选择传输 NServiceBus 需要传输来接收和发送消息。 我们将坚持使用学习传输,因为它易于使用,并且在测试和开发中运行良好。 通过执行安装学习传输的包。 Install-Package NServiceBus.RabbitMQ 配置 NServiceBus 设置终结点 在您的Program.cs文件中设置NServiceBus端点配置: using NServiceBus; using System; using System.Threading.Tasks; using Messages; class Program { static async Task Main() { Console.Title = "Sender"; var endpointConfiguration = new EndpointConfiguration("SenderEndpoint"); // Use RabbitMQ Transport var transport = endpointConfiguration.UseTransport<RabbitMQTransport>(); transport.ConnectionString("host=localhost"); // Set up error queue endpointConfiguration.SendFailedMessagesTo("error"); // Set up audit queue endpointConfiguration.AuditProcessedMessagesTo("audit"); // Start the endpoint var endpointInstance = await Endpoint.Start(endpointConfiguration).ConfigureAwait(false); Console.WriteLine("Press Enter to send a message..."); Console.ReadLine(); // Send a message var message = new MyMessage { Content = "Hello, NServiceBus with RabbitMQ!" }; await endpointInstance.Send("ReceiverEndpoint", message).ConfigureAwait(false); Console.WriteLine("Message sent. Press Enter to exit..."); Console.ReadLine(); // Stop the endpoint await endpointInstance.Stop().ConfigureAwait(false); } } using NServiceBus; using System; using System.Threading.Tasks; using Messages; class Program { static async Task Main() { Console.Title = "Sender"; var endpointConfiguration = new EndpointConfiguration("SenderEndpoint"); // Use RabbitMQ Transport var transport = endpointConfiguration.UseTransport<RabbitMQTransport>(); transport.ConnectionString("host=localhost"); // Set up error queue endpointConfiguration.SendFailedMessagesTo("error"); // Set up audit queue endpointConfiguration.AuditProcessedMessagesTo("audit"); // Start the endpoint var endpointInstance = await Endpoint.Start(endpointConfiguration).ConfigureAwait(false); Console.WriteLine("Press Enter to send a message..."); Console.ReadLine(); // Send a message var message = new MyMessage { Content = "Hello, NServiceBus with RabbitMQ!" }; await endpointInstance.Send("ReceiverEndpoint", message).ConfigureAwait(false); Console.WriteLine("Message sent. Press Enter to exit..."); Console.ReadLine(); // Stop the endpoint await endpointInstance.Stop().ConfigureAwait(false); } } $vbLabelText $csharpLabel 创建消息 新增一个类以表示消息。 public class MyMessage : IMessage { public string Content { get; set; } } public class MyMessage : IMessage { public string Content { get; set; } } $vbLabelText $csharpLabel 创建消息处理程序 新增一个类以处理消息。 using NServiceBus; using System.Threading.Tasks; public class MyMessageHandler : IHandleMessages<MyMessage> { public Task Handle(MyMessage message, IMessageHandlerContext context) { Console.WriteLine($"Received message: {message.Content}"); return Task.CompletedTask; } } using NServiceBus; using System.Threading.Tasks; public class MyMessageHandler : IHandleMessages<MyMessage> { public Task Handle(MyMessage message, IMessageHandlerContext context) { Console.WriteLine($"Received message: {message.Content}"); return Task.CompletedTask; } } $vbLabelText $csharpLabel 发送消息 从终结点发送消息。 通过处理程序调整您的主要方法以传递消息。 using NServiceBus; using System; using System.Threading.Tasks; class Program { static async Task Main() { Console.Title = "Receiver"; var endpointConfiguration = new EndpointConfiguration("ReceiverEndpoint"); // Serialization configuration endpointConfiguration.UseSerialization<NewtonsoftJsonSerializer>(); // Use RabbitMQ Transport var transport = endpointConfiguration.UseTransport<RabbitMQTransport>(); transport.UseConventionalRoutingTopology(QueueType.Quorum); transport.ConnectionString("host=localhost"); // Set up error queue endpointConfiguration.SendFailedMessagesTo("error"); // Set up audit queue endpointConfiguration.AuditProcessedMessagesTo("audit"); endpointConfiguration.EnableInstallers(); // Start the endpoint var endpointInstance = await Endpoint.Start(endpointConfiguration).ConfigureAwait(false); Console.WriteLine("Press Enter to exit..."); Console.ReadLine(); // Stop the endpoint await endpointInstance.Stop().ConfigureAwait(false); } } using NServiceBus; using System; using System.Threading.Tasks; class Program { static async Task Main() { Console.Title = "Receiver"; var endpointConfiguration = new EndpointConfiguration("ReceiverEndpoint"); // Serialization configuration endpointConfiguration.UseSerialization<NewtonsoftJsonSerializer>(); // Use RabbitMQ Transport var transport = endpointConfiguration.UseTransport<RabbitMQTransport>(); transport.UseConventionalRoutingTopology(QueueType.Quorum); transport.ConnectionString("host=localhost"); // Set up error queue endpointConfiguration.SendFailedMessagesTo("error"); // Set up audit queue endpointConfiguration.AuditProcessedMessagesTo("audit"); endpointConfiguration.EnableInstallers(); // Start the endpoint var endpointInstance = await Endpoint.Start(endpointConfiguration).ConfigureAwait(false); Console.WriteLine("Press Enter to exit..."); Console.ReadLine(); // Stop the endpoint await endpointInstance.Stop().ConfigureAwait(false); } } $vbLabelText $csharpLabel 启动应用程序并构建项目。 控制台应显示消息"收到消息:Hello, NServiceBus!" 开始 在 C# 项目中,集成 NServiceBus 与 RabbitMQ 和 IronPDF 需要配置 NServiceBus 与 RabbitMQ 之间的消息以及使用 IronPDF 生成 PDF。 以下是详细的操作步骤,帮助您入门: 什么是IronPDF? IronPDF 是一个 .NET 库,用于创建、读取、编辑和转换 PDF 文件。 使用它,程序员可以在 C# 或 VB.NET 应用程序中使用强大直观的工具处理 PDF 文件。 IronPDF 的特征和功能如下详细描述: IronPDF的功能 从 HTML 生成 PDF 将JavaScript、HTML和CSS转换为PDF。 支持媒体查询和响应式设计等现代网络标准。 用于使用 HTML 和 CSS 生成动态样式的 PDF 文档、发票和报告。 PDF 编辑 在已存在的 PDF 中添加文本、图片和其他材料。 从 PDF 文件中提取文本和图片。 将多个 PDF 合并为一个文件。将 PDF 文件拆分为多个文档。 添加注释、页脚、页眉和水印。 PDF 转换 将 Word、Excel、图片和其他文件格式转换为 PDF。 PDF到图像转换(PNG、JPEG等)。 性能和可靠性 其设计目标是在生产环境中具备高性能和可靠性。 高效处理大型文档。 IronPDF 的安装 通过打开 NuGet 包管理器控制台安装 IronPDF。 Install-Package IronPdf 配置发送方与消息 Messages 是发送方和接收方都将使用的共享项目(类库)。 在 Messages 项目中定义消息类。 创建一个名为 Messages 的新类库项目,并将其添加到解决方案中。 定义消息: 在Messages项目中,创建一个新的类,命名为GeneratePdfMessage.cs: using NServiceBus; public class GeneratePdfMessage : IMessage { public string Content { get; set; } public string OutputPath { get; set; } } using NServiceBus; public class GeneratePdfMessage : IMessage { public string Content { get; set; } public string OutputPath { get; set; } } $vbLabelText $csharpLabel 在发送方和接收方项目中,包含对 Messages 项目的引用。 在发送方项目中设置 NServiceBus 终结点以使用 RabbitMQ 进行消息传递。 using NServiceBus; using System; using System.Threading.Tasks; using Messages; class Program { static async Task Main() { Console.Title = "Sender"; var endpointConfiguration = new EndpointConfiguration("SenderEndpoint"); // Use RabbitMQ Transport var transport = endpointConfiguration.UseTransport<RabbitMQTransport>(); transport.ConnectionString("host=localhost"); // Set up error queue endpointConfiguration.SendFailedMessagesTo("error"); // Set up audit queue endpointConfiguration.AuditProcessedMessagesTo("audit"); endpointConfiguration.EnableInstallers(); // Start the endpoint var endpointInstance = await Endpoint.Start(endpointConfiguration).ConfigureAwait(false); Console.WriteLine("Press Enter to send a message..."); Console.ReadLine(); // Send a message var message = new GeneratePdfMessage { Content = "<h1>Hello, NServiceBus with RabbitMQ and IronPDF!</h1>", OutputPath = "output.pdf" }; await endpointInstance.Send("ReceiverEndpoint", message).ConfigureAwait(false); Console.WriteLine("Message sent. Press Enter to exit..."); Console.ReadLine(); // Stop the endpoint await endpointInstance.Stop().ConfigureAwait(false); } } using NServiceBus; using System; using System.Threading.Tasks; using Messages; class Program { static async Task Main() { Console.Title = "Sender"; var endpointConfiguration = new EndpointConfiguration("SenderEndpoint"); // Use RabbitMQ Transport var transport = endpointConfiguration.UseTransport<RabbitMQTransport>(); transport.ConnectionString("host=localhost"); // Set up error queue endpointConfiguration.SendFailedMessagesTo("error"); // Set up audit queue endpointConfiguration.AuditProcessedMessagesTo("audit"); endpointConfiguration.EnableInstallers(); // Start the endpoint var endpointInstance = await Endpoint.Start(endpointConfiguration).ConfigureAwait(false); Console.WriteLine("Press Enter to send a message..."); Console.ReadLine(); // Send a message var message = new GeneratePdfMessage { Content = "<h1>Hello, NServiceBus with RabbitMQ and IronPDF!</h1>", OutputPath = "output.pdf" }; await endpointInstance.Send("ReceiverEndpoint", message).ConfigureAwait(false); Console.WriteLine("Message sent. Press Enter to exit..."); Console.ReadLine(); // Stop the endpoint await endpointInstance.Stop().ConfigureAwait(false); } } $vbLabelText $csharpLabel 终结点配置:通过调用 new EndpointConfiguration("SenderEndpoint") 初始化名为 "SenderEndpoint" 的终结点。 endpointConfiguration 是传输配置。 通过连接到本地 RabbitMQ 实例,方法 UseTransport() 将 NServiceBus 设置为使用 RabbitMQ 作为传输机制。 审核和错误队列,使用AuditProcessedMessagesTo("audit")分别配置发送失败消息和审核处理过的消息的位置。 消息已发送:endpointInstance.Send("ReceiverEndpoint", message)发送一个GeneratePdfMessage到"ReceiverEndpoint"。 配置接收方生成 PDF 在接收方项目中设置 NServiceBus 终结点以通过 RabbitMQ 接收消息并使用 IronPDF 生成 PDF。 using NServiceBus; using System; using System.Threading.Tasks; class Program { static async Task Main() { Console.Title = "Receiver"; var endpointConfiguration = new EndpointConfiguration("ReceiverEndpoint"); // Use RabbitMQ Transport var transport = endpointConfiguration.UseTransport<RabbitMQTransport>(); transport.ConnectionString("host=localhost"); // Set up error queue endpointConfiguration.SendFailedMessagesTo("error"); // Set up audit queue endpointConfiguration.AuditProcessedMessagesTo("audit"); // Start the endpoint var endpointInstance = await Endpoint.Start(endpointConfiguration).ConfigureAwait(false); Console.WriteLine("Press Enter to exit..."); Console.ReadLine(); // Stop the endpoint await endpointInstance.Stop().ConfigureAwait(false); } } using NServiceBus; using System; using System.Threading.Tasks; class Program { static async Task Main() { Console.Title = "Receiver"; var endpointConfiguration = new EndpointConfiguration("ReceiverEndpoint"); // Use RabbitMQ Transport var transport = endpointConfiguration.UseTransport<RabbitMQTransport>(); transport.ConnectionString("host=localhost"); // Set up error queue endpointConfiguration.SendFailedMessagesTo("error"); // Set up audit queue endpointConfiguration.AuditProcessedMessagesTo("audit"); // Start the endpoint var endpointInstance = await Endpoint.Start(endpointConfiguration).ConfigureAwait(false); Console.WriteLine("Press Enter to exit..."); Console.ReadLine(); // Stop the endpoint await endpointInstance.Stop().ConfigureAwait(false); } } $vbLabelText $csharpLabel 该设置对于 "ReceiverEndpoint" 接收终结点,类似于发送方配置。 消息处理程序 在Receiver项目中,创建一个新的类,命名为GeneratePdfMessageHandler.cs。 using NServiceBus; using System; using System.Threading.Tasks; using Messages; using IronPdf; public class GeneratePdfMessageHandler : IHandleMessages<GeneratePdfMessage> { public Task Handle(GeneratePdfMessage message, IMessageHandlerContext context) { Console.WriteLine($"Received message to generate PDF with content: {message.Content}"); // Generate PDF var renderer = new HtmlToPdf(); var pdf = renderer.RenderHtmlAsPdf(message.Content); pdf.SaveAs(message.OutputPath); Console.WriteLine($"PDF generated and saved to: {message.OutputPath}"); return Task.CompletedTask; } } using NServiceBus; using System; using System.Threading.Tasks; using Messages; using IronPdf; public class GeneratePdfMessageHandler : IHandleMessages<GeneratePdfMessage> { public Task Handle(GeneratePdfMessage message, IMessageHandlerContext context) { Console.WriteLine($"Received message to generate PDF with content: {message.Content}"); // Generate PDF var renderer = new HtmlToPdf(); var pdf = renderer.RenderHtmlAsPdf(message.Content); pdf.SaveAs(message.OutputPath); Console.WriteLine($"PDF generated and saved to: {message.OutputPath}"); return Task.CompletedTask; } } $vbLabelText $csharpLabel GeneratePdfMessage类型的消息。 处理方法:收到消息后,Handle函数使用IronPDF创建一个PDF。 消息中的HTML内容通过HtmlToPdf渲染器代码转换为PDF,然后保存到指定的输出路径。 结论 NServiceBus 可以与 RabbitMQ 和 IronPDF 集成在 C# 中,提供一个可扩展和稳定的解决方案,用于需要动态和可靠生成 PDF 的分布式系统。 这种组合利用了 NServiceBus 的消息处理能力,RabbitMQ 作为消息代理的可靠性和适应性,以及 IronPDF 的强大 PDF 编辑工具。 由此产生的架构可保证服务之间的解耦,实现自主管理和可伸缩。 即使在网络或应用程序故障的情况下,RabbitMQ 也确保消息的传递。 NServiceBus 使消息路由和处理更加简单,而 IronPDF 使将 HTML 文本转换为高质量 PDF 文档成为可能。 该集成不但改善了系统的可维护性和可靠性,还为开发复杂的大型应用程序提供了灵活的框架。 最后,通过将 IronPDF 和 Iron Software 加入您的 .NET 编程工具包中,您可以高效地处理条形码、生成 PDF、执行 OCR,并与 Excel 连接。 IronPDF的许可页面,从$799开始,完美融合了其功能、兼容性及易用性,并与Iron Software官方站点的灵活套件一起,为提供附加的Web应用和功能以及更高效的开发。 如果有定制项目特定需求的明确许可证选项,开发人员可以自信地选择最佳模式。 这些优势使开发人员能够有效且透明地处理各种问题。 常见问题解答 如何在 C# 中使用 NServiceBus 进行分布式系统开发? NServiceBus 通过抽象消息架构简化了 C# 中的分布式系统开发。这使得开发人员能够专注于业务逻辑,同时确保可靠的消息处理和跨微服务的传递。 将 NServiceBus 与 PDF 管理库集成有什么好处? 将 NServiceBus 与像 IronPDF 这样的 PDF 管理库集成允许在分布式应用程序中进行高效的 PDF 生成和管理,实现可扩展且易于维护的软件系统。 如何设置包含 NServiceBus 和 RabbitMQ 的 C# 项目? 要设置包含 NServiceBus 和 RabbitMQ 的 C# 项目,在 Visual Studio 中创建一个新的控制台应用程序,安装 NServiceBus 和 RabbitMQ 的 NuGet 包,并在代码中配置端点和消息传输。 NServiceBus 如何增强基于消息的通信? NServiceBus 通过提供可靠的消息模式,比如发布/订阅模型和 saga 管理,增强了基于消息的通信,确保消息在分布式系统中正确地传递和处理。 IronPDF 在使用 NServiceBus 的分布式系统中扮演什么角色? IronPDF 在使用 NServiceBus 的分布式系统中扮演关键角色,提供强大的 PDF 生成和操作能力,可以集成到消息驱动的工作流中,以自动化文档处理过程。 如何在使用 C# 的分布式系统中确保可靠的 PDF 生成? 在使用 C# 的分布式系统中,可靠的 PDF 生成可以通过集成 NServiceBus 进行消息处理和 IronPDF 进行 PDF 生成来实现,利用 RabbitMQ 的消息功能协调任务并确保一致性。 NServiceBus 中的发布/订阅模型如何工作? 在 NServiceBus 中,发布/订阅模型允许服务发布事件,其他服务可以订阅这些事件。这启用了事件驱动架构,其中一个组件的变化可以触发其他组件的动作,改善系统的响应能力和可扩展性。 NServiceBus 中的 saga 管理有什么意义? 在 NServiceBus 中,saga 管理对于协调跨服务的长时间业务流程至关重要。 Jacob Mellor 立即与工程团队聊天 首席技术官 Jacob Mellor 是 Iron Software 的首席技术官,也是一位开创 C# PDF 技术的有远见的工程师。作为 Iron Software 核心代码库的原始开发者,他从公司成立之初就开始塑造公司的产品架构,与首席执行官 Cameron Rimington 一起将公司转变为一家拥有 50 多名员工的公司,为 NASA、特斯拉和全球政府机构提供服务。Jacob 拥有曼彻斯特大学土木工程一级荣誉工程学士学位(BEng)(1998-2001 年)。他的旗舰产品 IronPDF 和 Iron Suite for .NET 库在全球的 NuGet 安装量已超过 3000 万次,其基础代码继续为全球使用的开发人员工具提供动力。Jacob 拥有 25 年的商业经验和 41 年的编码专业知识,他一直专注于推动企业级 C#、Java 和 Python PDF 技术的创新,同时指导下一代技术领导者。 相关文章 已更新2026年2月20日 架起 CLI 简洁性与 .NET 的桥梁:使用 IronPDF for .NET 的 Curl DotNet Jacob Mellor 通过 CurlDotNet 填补了这一空白,CurlDotNet 库的创建是为了将 cURL 的熟悉感带入 .NET 生态系统。 阅读更多 已更新2025年12月20日 RandomNumberGenerator C# 使用 RandomNumberGenerator C# 类可以帮助将您的 PDF 生成和编辑项目提升到一个新的高度。 阅读更多 已更新2025年12月20日 C# String Equals(开发者用法) 与强大的 PDF 库 IronPDF 结合使用,切换模式匹配允许您为文档处理构建更智能、更简洁的逻辑。 阅读更多 Flurl C#(开发者用法)C# 引用传递(开发者用法)
已更新2026年2月20日 架起 CLI 简洁性与 .NET 的桥梁:使用 IronPDF for .NET 的 Curl DotNet Jacob Mellor 通过 CurlDotNet 填补了这一空白,CurlDotNet 库的创建是为了将 cURL 的熟悉感带入 .NET 生态系统。 阅读更多
已更新2025年12月20日 RandomNumberGenerator C# 使用 RandomNumberGenerator C# 类可以帮助将您的 PDF 生成和编辑项目提升到一个新的高度。 阅读更多