Add CancellationToken support to async methods #9

Introduce support for `CancellationToken` across various methods to allow for task cancellation and improve responsiveness.
- Update `DecodeAsync` method in `DocxContentDecoder.cs`, `PdfContentDecoder.cs`, `TextContentDecoder.cs`, and `IContentDecoder.cs` to include an optional `CancellationToken` parameter.
- Modify endpoint handlers in `Program.cs` to accept and pass `CancellationToken` parameters.
- Update methods in `ChatService.cs` to include `CancellationToken` parameters.
- Update methods in `DocumentService.cs` to include `CancellationToken` parameters.
- Update methods in `VectorSearchService.cs` to include `CancellationToken` parameters.
These changes ensure that long-running operations can be canceled if needed, improving the application's ability to handle cancellation requests gracefully.
This commit is contained in:
Marco Minerva
2025-02-10 16:20:35 +01:00
parent d0fce6ffd2
commit a0f1755c85
8 changed files with 67 additions and 65 deletions
@@ -6,7 +6,7 @@ namespace SqlDatabaseVectorSearch.ContentDecoders;
public class DocxContentDecoder : IContentDecoder
{
public Task<string> DecodeAsync(Stream stream, string contentType)
public Task<string> DecodeAsync(Stream stream, string contentType, CancellationToken cancellationToken = default)
{
// Open a Word document for read-only access.
using var document = WordprocessingDocument.Open(stream, false);
@@ -2,5 +2,5 @@
public interface IContentDecoder
{
Task<string> DecodeAsync(Stream stream, string contentType);
Task<string> DecodeAsync(Stream stream, string contentType, CancellationToken cancellationToken = default);
}
@@ -6,7 +6,7 @@ namespace SqlDatabaseVectorSearch.ContentDecoders;
public class PdfContentDecoder : IContentDecoder
{
public Task<string> DecodeAsync(Stream stream, string contentType)
public Task<string> DecodeAsync(Stream stream, string contentType, CancellationToken cancellationToken = default)
{
var content = new StringBuilder();
@@ -2,10 +2,10 @@
public class TextContentDecoder : IContentDecoder
{
public async Task<string> DecodeAsync(Stream stream, string contentType)
public async Task<string> DecodeAsync(Stream stream, string contentType, CancellationToken cancellationToken = default)
{
using var readStream = new StreamReader(stream);
var content = await readStream.ReadToEndAsync();
var content = await readStream.ReadToEndAsync(cancellationToken);
return content;
}