跳過到頁腳內容
.NET幫助

NServiceBus C# (對開發者如何運作)

NServiceBus 是專為 .NET Framework 設計的功能強大且適應性高的服務匯流排,可簡化分散式系統的開發。 它所提供的強大訊息傳送模式可保證在多個微服務和應用程式之間進行可靠的訊息處理和傳送。 NServiceBus 將底層的訊息傳送架構抽象化,讓開發人員可以專注於商業邏輯,而非建立分散式系統的複雜問題。

相比之下,IronPDF 是一個廣受歡迎的 .NET 函式庫,用於產生、檢視和修改 PDF 檔案。 它以簡單易用著稱,在從 ASPX 檔案和 HTML 等各種來源建立 PDF 方面非常有效率。

開發人員可以透過結合 NServiceBus 和 IronPDF,建立可靠、可擴充且可維護的軟體系統,以產生和管理 PDF 文件,作為其業務運作的一部分。

在本文中,我們將探討如何建立一個簡單的 C# NServiceBus 專案,並將其與 IronPDF 整合,這樣您就可以在分散式應用程式架構中建立一個簡化的工作流程,以管理和製作 PDF 文件。 閱讀完本簡介教學後,您應該可以確切知道這兩種有效的技術如何在分散式環境中合作簡化 PDF 相關工作。

什麼是 NServiceBus C#?

NServiceBus 是一個功能強大、適應性強的框架,可讓您輕鬆建立分散式系統和服務導向的 .NET 架構。 利用 NServiceBus,您可以輕鬆管理各種訊息類型,並確保可靠的通訊。 這一點至關重要,尤其是在 Web 應用程式和類似的架構中,無縫的訊息路由和處理是不可或缺的。 NServiceBus 的訊息處理程式能有效處理接收訊息,確保每個邏輯元件都能順利互動。 NServiceBus 具有以下重要功能:

NServiceBus C# (How It Works For Developers):圖 1 - NServiceBus C#

NServiceBus 的特點

以訊息為基礎的溝通

NServiceBus 鼓勵系統中不同服務或元件之間以訊息為基礎的通訊。 透過解耦元件,此方法可創造出更容易擴充與管理的設計。

可靠的訊息傳送

透過自動管理重試、死字母佇列和其他容錯技術,可保證可靠的訊息傳送。 在分散式系統中,網路中斷和其他故障問題經常發生,因此這種可靠性是不可或缺的。

發佈/訂閱模型

NServiceBus 支援發佈/訂閱模式,可讓服務發佈事件並讓其他服務訂閱這些事件。 這使得事件驅動架構成為可能,在此架構中,對系統中一個元件的事件所做的修改,會引起其他元件的回應。

傳奇管理

由於 NServiceBus 整合了對傳奇的支援,因此可以使用 NServiceBus 管理長時間運作的業務流程。 Sagas 可讓服務平台管理狀態,並協調數個服務間錯綜複雜的作業。

擴充性與客製化

它提供了卓越的擴充性,讓開發人員可以個人化處理、處理和傳輸訊息的過程。 由於其適應性,因此可以用於各種場景。

與各種訊息平台整合

許多訊息傳送系統,包括 MSMQ、RabbitMQ、Azure Service Bus、Amazon SQS 等,都可以與 NServiceBus 整合。 這可讓開發人員選擇最適合其需求的通訊基礎架構解決方案。

在 C# 中建立並配置 NServiceBus;。

在開始在 C# 專案中使用 NServiceBus 之前,您必須先設定開發環境、建立基本專案,並建立基本的訊息傳送服務和情境。 以下是一份分步指南,助您一臂之力。

建立新的 Visual Studio 專案

在 Visual Studio 中,建立控制台專案的過程很簡單。 在 Visual Studio 環境中使用這些簡單的步驟啟動 Console 應用程式:

使用前請確認您已在個人電腦上安裝 Visual Studio。

開始新專案

按一下檔案,然後選擇新增,最後選擇專案。

NServiceBus C# (How It Works For Developers):圖 2 - 按一下新增

您可以從下列專案範本參考清單中選擇"Console App"或"Console App (.NET Core)"範本。

在"名稱"欄位中提供專案名稱。

NServiceBus C# (How It Works For Developers):圖 3 - 提供專案名稱與位置

為專案選擇儲存位置。

按一下"建立"即可啟動 Console 應用程式專案。

NServiceBus C# (How It Works For Developers):圖 4 - 按一下建立

安裝 NServiceBus 套件

導覽至工具 > NuGet Package Manager > Package Manager Console,開啟 NuGet Package Manager Console。

執行下列指令以安裝 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);
    }
}
Imports NServiceBus
Imports System
Imports System.Threading.Tasks
Imports Messages

Friend Class Program
	Shared Async Function Main() As Task
		Console.Title = "Sender"
		Dim endpointConfiguration As New EndpointConfiguration("SenderEndpoint")

		' Use RabbitMQ Transport
		Dim transport = endpointConfiguration.UseTransport(Of RabbitMQTransport)()
		transport.ConnectionString("host=localhost")

		' Set up error queue
		endpointConfiguration.SendFailedMessagesTo("error")

		' Set up audit queue
		endpointConfiguration.AuditProcessedMessagesTo("audit")

		' Start the endpoint
		Dim endpointInstance = Await Endpoint.Start(endpointConfiguration).ConfigureAwait(False)
		Console.WriteLine("Press Enter to send a message...")
		Console.ReadLine()

		' Send a message
		Dim message = New MyMessage With {.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)
	End Function
End Class
$vbLabelText   $csharpLabel

NServiceBus C# (How It Works For Developers):圖 5 - 控制台輸出範例

建立訊息

為了表達訊息,請新增一個類別。

public class MyMessage : IMessage
{
    public string Content { get; set; }
}
public class MyMessage : IMessage
{
    public string Content { get; set; }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$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;
    }
}
Imports NServiceBus
Imports System.Threading.Tasks

Public Class MyMessageHandler
	Implements IHandleMessages(Of MyMessage)

	Public Function Handle(ByVal message As MyMessage, ByVal context As IMessageHandlerContext) As Task
		Console.WriteLine($"Received message: {message.Content}")
		Return Task.CompletedTask
	End Function
End Class
$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);
    }
}
Imports NServiceBus
Imports System
Imports System.Threading.Tasks

Friend Class Program
	Shared Async Function Main() As Task
		Console.Title = "Receiver"
		Dim endpointConfiguration As New EndpointConfiguration("ReceiverEndpoint")

		' Serialization configuration
		endpointConfiguration.UseSerialization(Of NewtonsoftJsonSerializer)()

		' Use RabbitMQ Transport
		Dim transport = endpointConfiguration.UseTransport(Of 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
		Dim endpointInstance = Await Endpoint.Start(endpointConfiguration).ConfigureAwait(False)
		Console.WriteLine("Press Enter to exit...")
		Console.ReadLine()

		' Stop the endpoint
		Await endpointInstance.Stop().ConfigureAwait(False)
	End Function
End Class
$vbLabelText   $csharpLabel

NServiceBus C# (How It Works For Developers):圖 6 - 控制台輸出範例

啟動應用程式並建立專案。 控制台應顯示"Received message:你好,NServiceBus!"

開始

在 C# 專案中,將 NServiceBus 與 RabbitMQ 和 IronPDF 整合需要設定 NServiceBus 與 RabbitMQ 之間的訊息,以及使用 IronPDF 建立 PDF。 以下是一份詳盡的指南,助您一臂之力:

什麼是 IronPDF?

IronPDF 是專為建立、閱讀、編輯和轉換 PDF 檔案而設計的 .NET 函式庫。 有了它,程式設計師可以在 C# 或 VB.NET 應用程式中使用功能強大且直覺的工具來處理 PDF 檔案。 IronPDF 的特性和功能在下文中有完整的描述:

NServiceBus C# (How It Works For Developers):圖 7 - IronPDF:C# PDF Library 首頁

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 專案中,建立一個新類別,名稱為 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; }
}
Imports NServiceBus

Public Class GeneratePdfMessage
	Implements IMessage

	Public Property Content() As String
	Public Property OutputPath() As String
End Class
$vbLabelText   $csharpLabel

在傳送者和接收者專案中,請參考訊息專案。

在 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);
    }
}
Imports NServiceBus
Imports System
Imports System.Threading.Tasks
Imports Messages

Friend Class Program
	Shared Async Function Main() As Task
		Console.Title = "Sender"
		Dim endpointConfiguration As New EndpointConfiguration("SenderEndpoint")

		' Use RabbitMQ Transport
		Dim transport = endpointConfiguration.UseTransport(Of RabbitMQTransport)()
		transport.ConnectionString("host=localhost")

		' Set up error queue
		endpointConfiguration.SendFailedMessagesTo("error")

		' Set up audit queue
		endpointConfiguration.AuditProcessedMessagesTo("audit")
		endpointConfiguration.EnableInstallers()

		' Start the endpoint
		Dim endpointInstance = Await Endpoint.Start(endpointConfiguration).ConfigureAwait(False)
		Console.WriteLine("Press Enter to send a message...")
		Console.ReadLine()

		' Send a message
		Dim message = New GeneratePdfMessage With {
			.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)
	End Function
End Class
$vbLabelText   $csharpLabel

端點組態:透過呼叫 new EndpointConfiguration("SenderEndpoint") 來初始化端點,並將其命名為"SenderEndpoint"。

endpointConfiguration 是傳輸組態。 透過連線至本機 RabbitMQ 範例,方法 UseTransport() 會設定 NServiceBus 使用 RabbitMQ 作為傳輸機制。

稽核與錯誤佇列 分別使用 SendFailedMessagesTo("error")AuditProcessedMessagesTo("audit") 設定傳送失敗訊息與稽核處理訊息的位置。

傳送訊息: endpointInstance.Send("ReceiverEndpoint", message) 傳送 GeneratePdfMessage 到"ReceiverEndpoint"。

設定接收器以產生 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);
    }
}
Imports NServiceBus
Imports System
Imports System.Threading.Tasks

Friend Class Program
	Shared Async Function Main() As Task
		Console.Title = "Receiver"
		Dim endpointConfiguration As New EndpointConfiguration("ReceiverEndpoint")

		' Use RabbitMQ Transport
		Dim transport = endpointConfiguration.UseTransport(Of RabbitMQTransport)()
		transport.ConnectionString("host=localhost")

		' Set up error queue
		endpointConfiguration.SendFailedMessagesTo("error")

		' Set up audit queue
		endpointConfiguration.AuditProcessedMessagesTo("audit")

		' Start the endpoint
		Dim endpointInstance = Await Endpoint.Start(endpointConfiguration).ConfigureAwait(False)
		Console.WriteLine("Press Enter to exit...")
		Console.ReadLine()

		' Stop the endpoint
		Await endpointInstance.Stop().ConfigureAwait(False)
	End Function
End Class
$vbLabelText   $csharpLabel

這個設定,針對"ReceiverEndpoint"接收者端點,與 Sender 的設定相類似。

訊息處理程式

在 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;
    }
}
Imports NServiceBus
Imports System
Imports System.Threading.Tasks
Imports Messages
Imports IronPdf

Public Class GeneratePdfMessageHandler
	Implements IHandleMessages(Of GeneratePdfMessage)

	Public Function Handle(ByVal message As GeneratePdfMessage, ByVal context As IMessageHandlerContext) As Task
		Console.WriteLine($"Received message to generate PDF with content: {message.Content}")

		' Generate PDF
		Dim renderer = New HtmlToPdf()
		Dim pdf = renderer.RenderHtmlAsPdf(message.Content)
		pdf.SaveAs(message.OutputPath)
		Console.WriteLine($"PDF generated and saved to: {message.OutputPath}")

		Return Task.CompletedTask
	End Function
End Class
$vbLabelText   $csharpLabel

GeneratePdfMessageHandler 使用 IHandleMessages 介面來處理類型為 GeneratePdfMessage 的訊息。

NServiceBus C# (How It Works For Developers):圖 8 - 控制台輸出範例

處理方法:接收訊息後,Handle 函式會使用 IronPDF 建立 PDF。 訊息中的 HTML 內容會由 HtmlToPdf 渲染程式碼轉換為 PDF,然後儲存至指定的輸出路徑。

NServiceBus C# (How It Works For Developers):圖 9 - 使用 NServiceBus 與 RabbitMQ 以及 IronPDF 所產生的 PDF 輸出

結論

NServiceBus 可與 C# 中的 RabbitMQ 和 IronPDF 整合,為需要動態且可靠地產生 PDF 的分散式系統提供可擴充且穩定的解決方案。 這個組合利用了 NServiceBus 的訊息處理功能、RabbitMQ 作為訊息中介的可靠性和適應性,以及 IronPDF 強大的 PDF 編輯工具。 結果架構可確保服務之間的解耦,允許自主演進和可擴展性。

即使在網路或應用程式故障的情況下,RabbitMQ 也能確保訊息傳遞。 NServiceBus 讓訊息路由和處理更簡單,而 IronPDF 則讓 HTML 文字轉換成高品質 PDF 文件成為可能。 除了改善系統的可維護性和可靠性之外,這種整合還為開發複雜的大型應用程式提供了靈活的框架。

最後,將 IronPDF 和 Iron Software 加入您的 .NET 程式設計工具包後,您就可以有效地處理條碼、產生 PDF、執行 OCR 以及與 Excel 連結。 IronPDF的授權頁面從$799開始,將其功能與Iron Software官方網站靈活套件的效能、相容性和易用性完美融合,提供額外的網路應用程式和功能,以及更有效率的開發。

如果有明確定義的授權選項,並可根據專案的特定需求客製化,開發人員就可以放心選擇最佳模式。 這些優點可讓開發人員有效且透明地處理一系列困難。

常見問題解答

如何在 C# 中使用 NServiceBus 進行分散式系統開發?

NServiceBus 透過抽象訊息傳輸架構,簡化了 C# 的分散式系統開發。這可讓開發人員專注於業務邏輯,同時確保跨微服務的可靠訊息處理與傳送。

將 NServiceBus 與 PDF 管理函式庫整合有什麼好處?

將 NServiceBus 與 IronPDF 等 PDF 管理函式庫整合,可在分散式應用程式中有效率地產生與管理 PDF,實現可擴充與可維護的軟體系統。

如何使用 NServiceBus 和 RabbitMQ 設定 C# 專案?

若要使用 NServiceBus 和 RabbitMQ 設定 C# 專案,請在 Visual Studio 中建立新的 Console Application,安裝 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, Team Iron 首席技术官
首席技术官

Jacob Mellor 是 Iron Software 的首席技術官,作為 C# PDF 技術的先鋒工程師。作為 Iron Software 核心代碼的原作者,他自開始以來塑造了公司產品架構,與 CEO Cameron Rimington 一起將其轉變為一家擁有超過 50 名員工的公司,為 NASA、特斯拉 和 全世界政府機構服務。

Jacob 持有曼徹斯特大學土木工程一級榮譽学士工程學位(BEng) (1998-2001)。他於 1999 年在倫敦開設了他的第一家軟件公司,並於 2005 年製作了他的首個 .NET 組件,專注於解決 Microsoft 生態系統內的複雜問題。

他的旗艦產品 IronPDF & Iron Suite .NET 庫在全球 NuGet 被安裝超過 3000 萬次,其基礎代碼繼續為世界各地的開發工具提供動力。擁有 25 年的商業經驗和 41 年的編碼專業知識,Jacob 仍專注於推動企業級 C#、Java 及 Python PDF 技術的創新,同時指導新一代技術領袖。