Created
March 20, 2023 00:33
-
-
Save TheFo2sh/7086f50b74be507fcd10b63dc303b3d3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PdfGenerationRequestHandler : IRequestHandler<PdfGenerationCommand, PdfGenerationResponse> | |
{ | |
private readonly IPdfGenerator _pdfGenerator; | |
public PdfGenerationRequestHandler(IPdfGenerator pdfGenerator) | |
{ | |
_pdfGenerator = pdfGenerator; | |
} | |
public async Task<PdfGenerationResponse> Handle(PdfGenerationCommand command, CancellationToken cancellationToken, IProgress<int> progress) | |
{ | |
// Generate the PDF using the provided HTML content | |
var pdfBytes = await _pdfGenerator.GeneratePdfAsync(command.HtmlContent); | |
// Generate a unique document ID | |
var documentId = Guid.NewGuid().ToString(); | |
// Return the PDF content and document ID in the response | |
return new PdfGenerationResponse | |
{ | |
PdfContent = pdfBytes, | |
DocumentId = documentId | |
}; | |
} | |
} | |
public class PdfGenerationCommandHandler : CommandHandler<PdfGenerationCommand> | |
{ | |
private readonly IRequestHandler<PdfGenerationCommand, PdfGenerationResponse> _requestHandler; | |
private CancellationTokenSource _cancellationTokenSource; | |
public PdfGenerationCommandHandler(IRequestHandler<PdfGenerationCommand, PdfGenerationResponse> requestHandler) | |
{ | |
_requestHandler = requestHandler; | |
} | |
public override async Task Handle(PdfGenerationCommand command) | |
{ | |
// Create a CancellationTokenSource and pass its token to the request handler | |
_cancellationTokenSource = new CancellationTokenSource(); | |
var token = _cancellationTokenSource.Token; | |
var response = await _requestHandler.Handle(command, token, null); | |
// Pass the response to the calling process via the output pipe | |
var serializedResponse = JsonConvert.SerializeObject(response); | |
await Console.Out.WriteLineAsync(serializedResponse); | |
} | |
public override void Cancel() | |
{ | |
// Cancel the operation by cancelling the token source | |
_cancellationTokenSource?.Cancel(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment