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の特徴
メッセージベースの通信
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を選択します。

以下のプロジェクトテンプレート参照リストから"Console App"または"Console App (.NET Core)"テンプレートを選択できます。
"Name"フィールドにプロジェクト名を入力します。

プロジェクトの保存場所を選択します。
"Create"をクリックするとコンソールアプリケーションプロジェクトが開始されます。

NServiceBusパッケージのインストール
NuGetパッケージマネージャコンソールを開くためにTools > NuGet Package Manager > Package Manager Consoleに移動します。
NServiceBus NuGetパッケージをインストールするために次のコマンドを実行します。
Install-Package NServiceBus
トランスポートを選択する
NServiceBusがメッセージを受け取ったり送信したりするにはトランスポートが必要です。 使いやすく、テストや開発に適しているため、Learning Transportを選びます。
Install the package for Learning Transport by executing.
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);
}
}
メッセージを作成する
メッセージを表す新しいクラスを追加します。
public class MyMessage : IMessage
{
public string Content { get; set; }
}public class MyMessage : IMessage
{
public string Content { get; set; }
}メッセージハンドラを作成する
メッセージを処理する新しいクラスを追加します。
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;
}
}メッセージを送信する
エンドポイントからメッセージを送信します。 ハンドラを助けてメッセージを送信するためにメイン方法を適応させます。
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);
}
}
アプリケーションを起動し、プロジェクトをビルドします。 コンソールには"Received message: Hello, NServiceBus!"と表示されます。
開始方法
C#プロジェクトにおいて、NServiceBusをRabbitMQおよびIronPDFと統合するには、NServiceBusとRabbitMQ間のメッセージの設定とIronPDFを使用したPDFの作成が含まれます。 ここでは、始めるための詳細なハウツーを紹介します:
IronPDFとは?
IronPDFは、PDFファイルを作成、読み取り、編集、変換するために設計された.NETライブラリです。 これにより、プログラマは強力で直感的なツールを用いて、C#またはVB.NETアプリケーションでPDFファイルを扱うことができます。 IronPDFの特徴と能力は以下に詳細に説明されています:

IronPDF の機能
HTMLからのPDF生成
JavaScript、HTML、CSSをPDFに変換します。 メディアクエリやレスポンシブデザインなどの現代Web標準に対応しています。 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; }
}送信者プロジェクトと受信者プロジェクトの両方に、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);
}
}エンドポイント構成:エンドポイントは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);
}
}この構成は、"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;
}
}GeneratePdfMessageHandlerはGeneratePdfMessageタイプのメッセージを処理するためにIHandleMessagesインターフェースを使用します。

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

結論
NServiceBusは、RabbitMQおよびIronPDFとC#内で統合されることで、動的かつ信頼性のある方法でPDFを生成する必要がある分散システムに対しスケーラブルで安定したソリューションを提供します。 この組み合わせはNServiceBusのメッセージ処理能力、メッセージブローカとしてのRabbitMQの信頼性と柔軟性、そしてIronPDFの強力なPDF編集ツールを活用します。 結果として生じるアーキテクチャは、サービス間の分離を保証し、自律的な進化とスケーラビリティを可能にします。
RabbitMQはまた、ネットワークやアプリケーションの障害があってもメッセージの配信を保証します。 NServiceBusはメッセージルーティングと処理を簡素化し、IronPDFはHTMLテキストを高品質なPDFドキュメントに変換します。 この統合はシステムの保守性と信頼性を向上させるだけでなく、洗練された大規模アプリケーションを構築するための柔軟なフレームワークも提供します。
最後に、.NETプログラミング用のツールキットにIronPDFとIron Softwareを追加することで、バーコードの処理、PDFの生成、OCRの実行、Excelとの統合を効率的に行うことができます。 IronPDFのライセンスページ、そこでは$799から始まり、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におけるサガ管理は、複数のサービスにまたがる長時間実行されるビジネスプロセスの調整に重要であり、複雑なワークフローが分散システム内で正確かつ一貫して実行されることを保証します。








