.NET幫助 NServiceBus C# (對開發者如何運作) Jacob Mellor 更新:2026年1月18日 下載 IronPDF NuGet 下載 DLL 下載 Windows Installer 開始免費試用 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 相關任務。 什麼是 C# 中的NServiceBus ? NServiceBus 是一個功能強大且適應性強的框架,可以輕鬆建立分散式系統和服務導向的.NET架構。 利用 NServiceBus,您可以輕鬆管理各種訊息類型並確保可靠的通訊。 這一點至關重要,尤其是在 Web 應用程式和類似架構中,因為無縫的訊息路由和處理是必不可少的。 NServiceBus 的訊息處理程序能夠有效地處理接收訊息,確保每個邏輯元件都能順利互動。 NServiceBus具有以下重要特性: NServiceBus 的特性 基於訊息的通信 NServiceBus 鼓勵系統中不同服務或元件之間基於訊息的通訊。 透過解耦組件,這種方法可以創建更易於擴展和管理的設計。 可靠的訊息傳遞 透過自動管理重試、死信佇列和其他容錯技術,它保證了訊息的可靠傳遞。 在分散式系統中,網路中斷和其他故障問題經常發生,這種可靠性至關重要。 發布/訂閱模式 NServiceBus 支援發布/訂閱模式,使服務能夠發布事件並允許其他服務訂閱這些事件。 這使得事件驅動架構成為可能,在這種架構中,對系統中一個元件的事件所做的修改可以引起其他元件的回應。 Saga Management 由於 NServiceBus 整合了對 saga 的支持,因此可以管理長時間運行的業務流程。 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 需要傳輸層來接收和傳送訊息。 我們將繼續使用 Learning Transport,因為它易於使用,並且非常適合測試和開發。 透過執行以下命令安裝 Learning Transport 軟體包。 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 啟動應用程式並建立專案。 控制台應顯示訊息"收到訊息:你好,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 在 Send 和 Receiver 專案中,都要包含 Messages 專案的引用。 在 Sender 專案中設定 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 作為傳輸機制。 審計佇列和錯誤佇列 失敗訊息和稽核處理訊息的傳送位置分別使用 SendFailedMessagesTo("error") 和 AuditProcessedMessagesTo("audit") 進行設定。 訊息已發送: endpointInstance.Send("ReceiverEndpoint", message) 向"ReceiverEndpoint"發送 GeneratePdfMessage。 配置接收器產生 PDF 在 Receiver 專案中設定 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 GeneratePdfMessageHandler 使用 IHandleMessages 介面處理 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 軟體官方網站靈活套件的功能、效能、相容性和易用性,從而提供額外的 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核心代碼庫的原始開發者,他自公司成立以來就塑造了公司的產品架構,並與CEO Cameron Rimington將公司轉型為服務NASA、Tesla以及全球政府機構的50多人公司。Jacob擁有曼徹斯特大學土木工程一級榮譽學士學位(1998年–2001年)。他於1999年在倫敦開立首家軟體公司,並於2005年建立了他的第一個.NET組件,專注於解決Microsoft生態系統中的複雜問題。他的旗艦作品IronPDF和Iron Suite .NET程式庫全球已獲得超過3000萬次NuGet安裝,他的基礎代碼不斷在全球各地驅動開發者工具。擁有25年以上的商業經驗和41年的編碼專業知識,Jacob仍然專注於推動企業級C#、Java和Python PDF技術的創新,同時指導下一代技術領導者。 相關文章 更新2026年2月20日 銜接 CLI 簡化與 .NET : 使用 Curl DotNet 與 IronPDF for .NET Jacob Mellor 藉由 CurlDotNet 彌補了這方面的不足,CurlDotNet 是為了讓 .NET 生態系統能熟悉 cURL 而建立的函式庫。 閱讀更多 更新2025年12月20日 RandomNumberGenerator C# 使用RandomNumberGenerator C#類可以幫助將您的PDF生成和編輯項目提升至新水準 閱讀更多 更新2025年12月20日 C#字符串等於(它如何對開發者起作用) 當結合使用強大的PDF庫IronPDF時,開關模式匹配可以讓您構建更智能、更清晰的邏輯來進行文檔處理 閱讀更多 Flurl C# (對開發者如何運作)C# 參考傳遞 (對開發者如何...
更新2026年2月20日 銜接 CLI 簡化與 .NET : 使用 Curl DotNet 與 IronPDF for .NET Jacob Mellor 藉由 CurlDotNet 彌補了這方面的不足,CurlDotNet 是為了讓 .NET 生態系統能熟悉 cURL 而建立的函式庫。 閱讀更多