跳過到頁腳內容
.NET幫助

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

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

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

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

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

什麼是 [NServiceBus](https://docs.particular.net/) 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 整合。 這可讓開發人員選擇最適合其需求的通訊基礎架構解決方案。

Create and configure NServiceBus in C#

在開始在 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; }
}
Public Class MyMessage
    Implements IMessage

    Public Property Content As String
End Class
$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) 向"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);
    }
}
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 的授權頁面(從 $999 開始)無縫融合了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, Team Iron 首席技術官
首席技術官

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技術的創新,同時指導下一代技術領導者。

鋼鐵支援團隊

我們每週 5 天,每天 24 小時在線上。
聊天
電子郵件
打電話給我