NServiceBus C#(開発者向けの仕組み)
NServiceBusは、分散システム開発を効率化するために設計された.NET Framework向けの強力で適応性のあるサービスバスです。 強力なメッセージングパターンを提供し、複数のマイクロサービスやアプリケーション間での信頼性のあるメッセージ処理と配信を保証します。 NServiceBusは基盤となるメッセージングアーキテクチャを抽象化し、開発者が分散システムの構築における複雑さではなくビジネスロジックに集中できるようにします。
一方、IronPDFはPDFファイルの生成、表示、修正を目的とした人気のある.NETライブラリです。 ASPXファイルやHTMLなど様々なソースからPDFを作成するのに使いやすく非常に効率的であることが知られています。
開発者は、NServiceBusとIronPDFを組み合わせることで、PDFドキュメントを生成し管理する一部として、信頼性のあるスケーラブルで保守性の高いソフトウェアシステムを構築できます。
この記事では、シンプルなC# NServiceBusプロジェクトを設定し、IronPDFと統合する方法を見ていき、分散アプリケーションアーキテクチャにおいてPDFドキュメントを管理および生成するための効率的なワークフローを構築できます。 この紹介チュートリアルを読んだ後、これらの2つの効果的な技術がどのように協力して、分散環境におけるPDF関連のタスクを簡素化できるか正確に理解することができます。
NServiceBus C#とは何ですか?
NServiceBusは、分散システムやサービス指向 for .NETアーキテクチャの作成を容易にする強力で適応性のあるフレームワークです。 NServiceBusを利用することで、さまざまなメッセージタイプを簡単に管理し、信頼性のある通信を確保できます。 これは特に、シームレスなメッセージルーティングと処理が不可欠なウェブアプリケーションや類似のアーキテクチャにおいて重要です。 NServiceBusのメッセージハンドラはメッセージの受信を効果的に処理し、すべての論理コンポーネントがスムーズに連携することを保証します。 NServiceBusには以下の重要な特徴があります:

NServiceBusの特徴
メッセージベースの通信
NServiceBusは、システム内の異なるサービスやコンポーネント間のメッセージベースの通信を促進します。 コンポーネントを分離させることで、スケールや管理が容易な設計を作成します。
信頼性のあるメッセージング
自動的にリトライ、死活キュー、その他の障害耐性技術を管理することにより、信頼性のあるメッセージ配信を保証します。 分散システムでは、ネットワーク障害や他の問題による失敗が頻発するため、この信頼性は不可欠です。
発行/購読モデル
NServiceBusは発行/購読パターンをサポートしており、サービスがイベントを発行し、他のサービスがこれらのイベントに登録できます。 これはイベント駆動型アーキテクチャを可能にし、システムの一部でのイベント変更が他のコンポーネントで反応を引き起こすことができるようになります。
サガ管理
NServiceBusはサガの統合サポートを提供することで長期的なビジネスプロセスを管理できます。 サガはサービスプラットフォームに状態を管理し、複数のサービス間での複雑な操作を調整することを可能にします。
拡張性とカスタマイズ
それは優れた拡張性を提供し、開発者がメッセージの取り扱いや処理、輸送プロセスをカスタマイズできるようにします。 その適応性により、様々なシナリオで活用できます。
さまざまなメッセージングプラットフォームとの統合
MSMQ、RabbitMQ、Azure Service Bus、Amazon SQSなどの多数のメッセージングシステムと統合できます。 これにより、開発者は要件に最適な通信インフラストラクションソリューションを選択できます。
Create and configure NServiceBus in C#
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パッケージをインストールするために次のコマンドを実行します。
トランスポートを選択する
NServiceBusがメッセージを受け取ったり送信したりするにはトランスポートが必要です。 使いやすく、テストや開発に適しているため、Learning Transportを選びます。
Install the package for Learning Transport by executing.
NServiceBusの設定
エンドポイントのセットアップ
NServiceBusエンドポイントの設定をProgram.csファイルに設定してください:
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
Class Program
Shared Async Function Main() As Task
Console.Title = "Sender"
Dim endpointConfiguration = 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 ClassImports NServiceBus
Imports System
Imports System.Threading.Tasks
Imports Messages
Class Program
Shared Async Function Main() As Task
Console.Title = "Sender"
Dim endpointConfiguration = 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
メッセージを作成する
メッセージを表す新しいクラスを追加します。
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 ClassPublic Class MyMessage
Implements IMessage
Public Property Content As String
End Classメッセージハンドラを作成する
メッセージを処理する新しいクラスを追加します。
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 ClassImports 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メッセージを送信する
エンドポイントからメッセージを送信します。 ハンドラを助けてメッセージを送信するためにメイン方法を適応させます。
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
Module Program
Sub Main()
MainAsync().GetAwaiter().GetResult()
End Sub
Private Async Function MainAsync() As Task
Console.Title = "Receiver"
Dim endpointConfiguration = 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 ModuleImports NServiceBus
Imports System
Imports System.Threading.Tasks
Module Program
Sub Main()
MainAsync().GetAwaiter().GetResult()
End Sub
Private Async Function MainAsync() As Task
Console.Title = "Receiver"
Dim endpointConfiguration = 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 Module
アプリケーションを起動し、プロジェクトをビルドします。 コンソールには"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をインストールします。
メッセージを持つ送信者の設定
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 ClassImports NServiceBus
Public Class GeneratePdfMessage
Implements IMessage
Public Property Content() As String
Public Property OutputPath() As String
End Class送信者プロジェクトと受信者プロジェクトの両方に、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 ClassImports 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エンドポイント構成:エンドポイントはnew EndpointConfiguration("SenderEndpoint")を呼び出して"SenderEndpoint"という名前で初期化されます。
endpointConfigurationはトランスポート構成です。 ローカルRabbitMQインスタンスに接続することにより、メソッドUseTransport()は、NServiceBusをRabbitMQをトランスポートメカニズムとして使用するように設定します。
失敗したメッセージを送信し、処理済みメッセージを監査するためのキューは、それぞれAuditProcessedMessagesTo("audit")によって設定されます。
メッセージ送信: endpointInstance.Send("ReceiverEndpoint", message)はGeneratePdfMessageを"ReceiverEndpoint"に送信します。
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
Module Program
Async Function Main() As Task
Console.Title = "Receiver"
Dim endpointConfiguration = 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 ModuleImports NServiceBus
Imports System
Imports System.Threading.Tasks
Module Program
Async Function Main() As Task
Console.Title = "Receiver"
Dim endpointConfiguration = 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 Moduleこの構成は、"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;
}
}Imports NServiceBus
Imports System
Imports System.Threading.Tasks
Imports Messages
Imports IronPdf
Public Class GeneratePdfMessageHandler
Implements IHandleMessages(Of GeneratePdfMessage)
Public Function Handle(message As GeneratePdfMessage, context As IMessageHandlerContext) As Task Implements IHandleMessages(Of GeneratePdfMessage).Handle
Console.WriteLine($"Received message to generate PDF with content: {message.Content}")
' Generate PDF
Dim renderer As 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 ClassImports NServiceBus
Imports System
Imports System.Threading.Tasks
Imports Messages
Imports IronPdf
Public Class GeneratePdfMessageHandler
Implements IHandleMessages(Of GeneratePdfMessage)
Public Function Handle(message As GeneratePdfMessage, context As IMessageHandlerContext) As Task Implements IHandleMessages(Of GeneratePdfMessage).Handle
Console.WriteLine($"Received message to generate PDF with content: {message.Content}")
' Generate PDF
Dim renderer As 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 ClassGeneratePdfMessageタイプのメッセージを処理します。

ハンドルメソッド: メッセージを受信後、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のライセンスページは$999で始まり、その機能とIron Softwareの公式サイトの柔軟なスイートのパフォーマンス、互換性、使いやすさをシームレスに融合し、追加のWebアプリケーションと機能、より効率的な開発を提供します。
プロジェクトの具体的な要件に合わせてカスタマイズされた、明確に定義されたライセンスオプションがあれば、開発者は自信を持って最適なモデルを選択できます。 これらの利点により、開発者は多くの困難を効果的かつ透析的に処理できます。
ジェイコブ・メラーはIron Softwareの最高技術責任者(CTO)であり、C# PDFテクノロジーを開拓する先見的なエンジニアです。Iron Softwareのコアコードベースを支えるオリジナル開発者として、彼は創業以来、会社の製品アーキテクチャを形成し、CEOのCameron Rimingtonとともに、会社をNASA、Tesla、および世界的な政府機関にサービスを提供する50人以上の会社に変えました。1999年にロンドンで最初のソフトウェアビジネスを開業し、2005年に最初 for .NETコンポーネントを作成した後、Microsoftのエコシステム全体で複雑な問題を解決することを専門としました。