フッターコンテンツにスキップ
.NETヘルプ

NServiceBus C#(開発者向けの仕組み)

NServiceBusは、分散システム開発を効率化するために設計された.NET Framework向けの強力で適応性のあるサービスバスです。 強力なメッセージングパターンを提供し、複数のマイクロサービスやアプリケーション間での信頼性のあるメッセージ処理と配信を保証します。 NServiceBusは基盤となるメッセージングアーキテクチャを抽象化し、開発者が分散システムの構築における複雑さではなくビジネスロジックに集中できるようにします。

一方、IronPDFはPDFファイルの生成、表示、修正を目的とした人気のある.NETライブラリです。 ASPXファイルやHTMLなど様々なソースからPDFを作成するのに使いやすく非常に効率的であることが知られています。

開発者は、NServiceBusとIronPDFを組み合わせることで、PDFドキュメントを生成し管理する一部として、信頼性のあるスケーラブルで保守性の高いソフトウェアシステムを構築できます。

この記事では、シンプルなC# NServiceBusプロジェクトを設定し、IronPDFと統合する方法を見ていき、分散アプリケーションアーキテクチャにおいてPDFドキュメントを管理および生成するための効率的なワークフローを構築できます。 この紹介チュートリアルを読んだ後、これらの2つの効果的な技術がどのように協力して、分散環境におけるPDF関連のタスクを簡素化できるか正確に理解することができます。

NServiceBus C#とは何ですか?

NServiceBusは、分散システムやサービス指向の.NETアーキテクチャの作成を容易にする強力で適応性のあるフレームワークです。 NServiceBusを利用することで、さまざまなメッセージタイプを簡単に管理し、信頼性のある通信を確保できます。 これは特に、シームレスなメッセージルーティングと処理が不可欠なウェブアプリケーションや類似のアーキテクチャにおいて重要です。 NServiceBusのメッセージハンドラはメッセージの受信を効果的に処理し、すべての論理コンポーネントがスムーズに連携することを保証します。 NServiceBusには以下の重要な特徴があります:

NServiceBus C# (開発者向けの動作): 図1 - NServiceBus C#

NServiceBusの特徴

メッセージベースの通信

NServiceBusは、システム内の異なるサービスやコンポーネント間のメッセージベースの通信を促進します。 コンポーネントを分離させることで、スケールや管理が容易な設計を作成します。

信頼性のあるメッセージング

自動的にリトライ、死活キュー、その他の障害耐性技術を管理することにより、信頼性のあるメッセージ配信を保証します。 分散システムでは、ネットワーク障害や他の問題による失敗が頻発するため、この信頼性は不可欠です。

発行/購読モデル

NServiceBusは発行/購読パターンをサポートしており、サービスがイベントを発行し、他のサービスがこれらのイベントに登録できます。 これはイベント駆動型アーキテクチャを可能にし、システムの一部でのイベント変更が他のコンポーネントで反応を引き起こすことができるようになります。

サガ管理

NServiceBusはサガの統合サポートを提供することで長期的なビジネスプロセスを管理できます。 サガはサービスプラットフォームに状態を管理し、複数のサービス間での複雑な操作を調整することを可能にします。

拡張性とカスタマイズ

それは優れた拡張性を提供し、開発者がメッセージの取り扱いや処理、輸送プロセスをカスタマイズできるようにします。 その適応性により、様々なシナリオで活用できます。

さまざまなメッセージングプラットフォームとの統合

MSMQ、RabbitMQ、Azure Service Bus、Amazon SQSなどの多数のメッセージングシステムと統合できます。 これにより、開発者は要件に最適な通信インフラストラクションソリューションを選択できます。

C#でNServiceBusを作成および構成する

C#プロジェクトでNServiceBusを使用するには、まず開発環境を設定し、基本的なプロジェクトを作成し、基本的なメッセージングサービスとシナリオを構築する必要があります。 ここでは、始めるためのステップバイステップガイドを紹介します。

新しいVisual Studioプロジェクトを作成

Visual Studio内でのコンソールプロジェクトの作成プロセスはシンプルです。 Visual Studio環境でこれらの簡単な手順を使用してコンソールアプリケーションを開始してください:

PCにVisual Studioがインストールされていることを確認してください。

新しいプロジェクトを開始する

Fileをクリックし、次にNewを選択し、最後にProjectを選択します。

NServiceBus C# (開発者向けの動作): 図2 - New をクリック

以下のプロジェクトテンプレート参照リストから"Console App"または"Console App (.NET Core)"テンプレートを選択できます。

"Name"フィールドにプロジェクト名を入力します。

NServiceBus C# (開発者向けの動作): 図3 - プロジェクト名と場所を指定

プロジェクトの保存場所を選択します。

"Create"をクリックするとコンソールアプリケーションプロジェクトが開始されます。

NServiceBus C# (開発者向けの動作): 図4 - Create をクリック

NServiceBusパッケージのインストール

NuGetパッケージマネージャコンソールを開くためにTools > NuGet Package Manager > Package Manager Consoleに移動します。

NServiceBus NuGetパッケージをインストールするために次のコマンドを実行します。

Install-Package NServiceBus

トランスポートを選択する

NServiceBusがメッセージを受け取ったり送信したりするにはトランスポートが必要です。 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# (開発者向けの動作): 図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

メッセージを作成する Handler

メッセージを処理する新しいクラスを追加します。

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# (開発者向けの動作): 図6 - コンソールの出力例

アプリケーションを起動し、プロジェクトをビルドします。 コンソールには"Received message: Hello, NServiceBus!"と表示されます。

開始方法

C#プロジェクトにおいて、NServiceBusをRabbitMQおよびIronPDFと統合するには、NServiceBusとRabbitMQ間のメッセージの設定とIronPDFを使用したPDFの作成が含まれます。 ここでは、始めるための詳細なハウツーを紹介します:

IronPDFとは?

IronPDFは、PDFファイルを作成、読み取り、編集、変換するために設計された.NETライブラリです。 これにより、プログラマは強力で直感的なツールを用いて、C#またはVB.NETアプリケーションでPDFファイルを扱うことができます。 IronPDFの特徴と能力は以下に詳細に説明されています:

NServiceBus C# (開発者向けの動作): 図7 - IronPDF: The C# PDF Libraryホームページ

IronPDFの機能

HTMLからのPDF生成

JavaScript、HTML、およびCSSをPDFに変換します。 メディアクエリおよびレスポンシブデザインといった現代のウェブ標準をサポートします。 HTMLとCSSを使用して動的にスタイル付けされたPDF文書、請求書、レポートを生成するために有用です。

PDF編集

既存のPDFにテキスト、画像、およびその他の素材を追加します。 PDFファイルからテキストや画像を抜き取ります。 複数のPDFを1つのファイルに結合します。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; }
}
Imports NServiceBus

Public Class GeneratePdfMessage
	Implements IMessage

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

送信者プロジェクトと受信者プロジェクトの両方に、Messagesプロジェクトへの参照を含めます。

送信者プロジェクトの中で、メッセージ配信にRabbitMQを使う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");
        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生成のために受信者を設定する

受信者プロジェクトの中で、メッセージをRabbitMQ経由で受け取り、IronPDFを使用してPDFを生成するようにNServiceBusエンドポイントを設定します。

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"の受信者エンドポイントに対して、送信者の設定と同様です。

メッセージハンドラ

受信者プロジェクトで、新しいクラス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

GeneratePdfMessageHandlerGeneratePdfMessageタイプのメッセージを処理するためにIHandleMessagesインターフェースを使用します。

NServiceBus C# (開発者向けの動作): 図8 - コンソールの出力例

ハンドルメソッド: メッセージを受け取った後、Handle関数はIronPDFを使用してPDFを作成します。 メッセージ内のHTMLコンテンツはHtmlToPdfレンダリングコードによってPDFに変換され、指定された出力パスに保存されます。

NServiceBus C# (開発者向けの動作): 図9 - NServiceBusとRabbitMQの使用に伴うPDF出力とIronPDF

結論

NServiceBusは、RabbitMQおよびIronPDFとC#内で統合されることで、動的かつ信頼性のある方法でPDFを生成する必要がある分散システムに対しスケーラブルで安定したソリューションを提供します。 この組み合わせはNServiceBusのメッセージ処理能力、メッセージブローカとしてのRabbitMQの信頼性と柔軟性、そしてIronPDFの強力なPDF編集ツールを活用します。 結果として生じるアーキテクチャは、サービス間の分離を保証し、自律的な進化とスケーラビリティを可能にします。

RabbitMQはまた、ネットワークやアプリケーションの障害があってもメッセージの配信を保証します。 NServiceBusはメッセージルーティングと処理を簡素化し、IronPDFはHTMLテキストを高品質なPDFドキュメントに変換します。 この統合はシステムの保守性と信頼性を向上させるだけでなく、洗練された大規模アプリケーションを構築するための柔軟なフレームワークも提供します。

最後に、.NETプログラミング用のツールキットにIronPDFとIron Softwareを追加することで、バーコードの処理、PDFの生成、OCRの実行、Excelとの統合を効率的に行うことができます。 IronPDF’s Licensing Page, which starts at $799, seamlessly blends its features and the performance, compatibility, and ease of use of Iron Softwareの公式サイトの柔軟なスイートの特徴や性能、互換性、使いやすさをシームレスに融合して、追加のウェブアプリケーションや能力、効率的な開発を提供します。

プロジェクトの具体的な要件に合わせてカスタマイズされた、明確に定義されたライセンスオプションがあれば、開発者は自信を持って最適なモデルを選択できます。 これらの利点により、開発者は多くの困難を効果的かつ透析的に処理できます。

よくある質問

C#で分散システム開発のためにNServiceBusをどのように使用できますか?

NServiceBusはメッセージングアーキテクチャを抽象化することで、C#での分散システム開発を簡素化します。これにより、開発者はビジネスロジックに集中しながら、マイクロサービス間で信頼性のあるメッセージ処理と配信を確保できます。

NServiceBusとPDF管理ライブラリの統合にはどんな利点がありますか?

IronPDFのようなPDF管理ライブラリとNServiceBusを統合することで、分散アプリケーション内で効率的なPDF生成と管理が可能となり、ソフトウェアシステムのスケーラビリティと保守性を高めます。

NServiceBusとRabbitMQを使用してC#プロジェクトをどのようにセットアップしますか?

NServiceBusとRabbitMQを使用してC#プロジェクトをセットアップするには、Visual Studioで新しいコンソールアプリケーションを作成し、NServiceBusとRabbitMQのNuGetパッケージをインストールして、コード内でエンドポイントとメッセージングトランスポートを構成します。

NServiceBusはメッセージベースの通信をどのように強化しますか?

NServiceBusは発行/購読モデルやサガ管理などの信頼性のあるメッセージングパターンを提供することで、分散システム全体でメッセージが適切に配信・処理されるようにすることで、メッセージベースの通信を強化します。

NServiceBusを使用する分散システムにおいてIronPDFはどのような役割を果たしますか?

IronPDFは、NServiceBusを使用する分散システムにおいて、メッセージ駆動型のワークフローに統合することができる堅牢なPDF生成および操作機能を提供することで、文書処理プロセスの自動化を行う重要な役割を果たします。

C#を使用した分散システムで信頼性のあるPDF生成をどのように確保しますか?

C#を使用した分散システムでの信頼性のあるPDF生成は、メッセージ処理にNServiceBusを、PDF生成にIronPDFを統合し、RabbitMQのメッセージング機能を利用してタスクを調整し、一貫性を確保することで達成できます。

NServiceBusにおける発行/購読モデルはどのように機能しますか?

NServiceBusにおける発行/購読モデルは、サービスが他のサービスが購読できるイベントを発行できるようにします。これにより、コンポーネントの変更が他にアクションを起こさせることができるイベント駆動型アーキテクチャが可能になり、システムの応答性とスケーラビリティが向上します。

NServiceBusにおけるサガ管理の重要性は何ですか?

NServiceBusにおけるサガ管理は、複数のサービスにまたがる長時間実行されるビジネスプロセスの調整に重要であり、複雑なワークフローが分散システム内で正確かつ一貫して実行されることを保証します。

Curtis Chau
テクニカルライター

Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。

開発以外にも、CurtisはIoT(Internet of Things)への強い関心を持ち、ハードウェアとソフトウェアの統合方法を模索しています。余暇には、ゲームをしたりDiscordボットを作成したりして、技術に対する愛情と創造性を組み合わせています。