Skip to content

Commit 2121387

Browse files
committed
Updated events listening
1 parent 4b15dfc commit 2121387

4 files changed

Lines changed: 238 additions & 95 deletions

File tree

net/developer-guide/advanced-usage/conversion-events.md

Lines changed: 64 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,56 @@ url: conversion/net/conversion-events
44
title: Conversion events
55
linkTitle: Conversion events
66
weight: 4
7-
description: "Subscribe to conversion lifecycle events (completed, failed, by-page-failed) through the typed ConversionEvents aggregator in GroupDocs.Conversion for .NET."
8-
keywords: ConversionEvents, OnConversionCompleted, OnConversionFailed, OnConversionByPageFailed, WithEvents, conversion lifecycle, event handlers
7+
description: "Subscribe to pipeline lifecycle and per-result conversion events through the typed ConversionEvents aggregator in GroupDocs.Conversion for .NET."
8+
keywords: ConversionEvents, OnConversionStarted, OnConversionProgress, OnConversionCompleted, OnDocumentConverted, OnDocumentFailed, OnPageConverted, OnPageFailed, OnCompressionCompleted, WithEvents, conversion lifecycle, event handlers
99
productName: GroupDocs.Conversion for .NET
1010
hideChildren: False
1111
toc: True
1212
---
1313

14-
Starting with **GroupDocs.Conversion for .NET v26.6**, conversion lifecycle event handlers are aggregated into a single typed object — [ConversionEvents](https://reference.groupdocs.com/conversion/net/groupdocs.conversion/conversionevents/). This replaces the previous practice of setting individual handlers on [ConverterSettings](https://reference.groupdocs.com/conversion/net/groupdocs.conversion/convertersettings/) or chaining `.OnConversionCompleted(...).OnConversionFailed(...)` after `WithOptions(...)` in the fluent API.
14+
Starting with **GroupDocs.Conversion for .NET v26.6**, conversion event handlers are aggregated into a single typed object — [ConversionEvents](https://reference.groupdocs.com/conversion/net/groupdocs.conversion/conversionevents/). This replaces three previously separate registration paths:
1515

16-
The aggregator exposes the following handlers:
16+
* The [IConverterListener](https://reference.groupdocs.com/conversion/net/groupdocs.conversion.reporting/iconverterlistener/) interface assigned to [ConverterSettings.Listener](https://reference.groupdocs.com/conversion/net/groupdocs.conversion/convertersettings/listener/) — for pipeline lifecycle callbacks.
17+
* Per-result handler properties on `ConverterSettings` (`OnConversionFailed`, `OnConversionByPageFailed`, `OnCompressionCompleted`).
18+
* The fluent chain methods placed after `WithOptions(...)` or `Compress(...)` (`.OnConversionCompleted(...)`, `.OnConversionFailed(...)`, `.OnCompressionCompleted(...)`).
19+
20+
The aggregator is registered with the [Converter](https://reference.groupdocs.com/conversion/net/groupdocs.conversion/converter/) constructor via a new `events:` factory parameter, or with the fluent API via the entry-stage `FluentConverter.WithEvents(...)` method.
21+
22+
## Event groups
23+
24+
`ConversionEvents` exposes two distinct groups of handlers.
25+
26+
### Pipeline lifecycle
27+
28+
Fire **once per conversion run**, regardless of how many documents or pages the run produces. These replace the `Started` / `Progress` / `Completed` callbacks of `IConverterListener`.
1729

1830
| Handler | Signature | When it fires |
1931
|---------|-----------|---------------|
20-
| `OnConversionCompleted` | `Action<ConvertedContext>` | After the entire conversion completes successfully |
21-
| `OnConversionFailed` | `Action<ConvertContext, Exception>` | When a conversion run throws |
22-
| `OnConversionByPageFailed` | `Action<ConvertContext, Exception>` | When a single page fails during page-by-page conversion |
32+
| `OnConversionStarted` | `Action` | Once, when the conversion pipeline starts |
33+
| `OnConversionProgress` | `Action<byte>` | While the pipeline is running, with the current progress percentage (0–100) |
34+
| `OnConversionCompleted` | `Action` | Once at the end of the pipeline, regardless of success or failure |
35+
36+
{{< hint style="warning" title="Name reuse — read before upgrading" >}}
37+
The `OnConversionCompleted` name is **reused with new semantics** in v26.6. The pre-v26.6 fluent chain method `.OnConversionCompleted(Action<ConvertedContext>)` (per-document) is now `OnDocumentConverted` on the aggregator. The new `ConversionEvents.OnConversionCompleted` is a lifecycle handler — its signature is `Action` (no parameters) and it fires once at pipeline end. If you copy a v26.5-and-earlier example expecting per-document context, switch to `OnDocumentConverted`.
38+
{{< /hint >}}
39+
40+
### Per-result
41+
42+
Fire **once per produced item** — each converted document, each converted page, each compressed archive — including failures.
43+
44+
| Handler | Signature | When it fires |
45+
|---------|-----------|---------------|
46+
| `OnDocumentConverted` | `Action<ConvertedContext>` | After each document conversion completes successfully |
47+
| `OnDocumentFailed` | `Action<ConvertContext, Exception>` | When a document conversion throws |
48+
| `OnPageConverted` | `Action<ConvertedPageContext>` | After each page is converted in page-by-page conversions |
49+
| `OnPageFailed` | `Action<ConvertContext, Exception>` | When a single page fails during page-by-page conversion |
2350
| `OnCompressionCompleted` | `Action<Stream>` | After contents have been converted and packaged into a compressed archive (see [Extract and Convert Archive Contents]({{< ref "conversion/net/developer-guide/advanced-usage/converting/conversion-options-by-document-family/convert-contents-of-rar-or-zip-document-to-different-formats-and-compress.md" >}})) |
2451

52+
See [Context Objects — Complete Guide]({{< ref "conversion/net/developer-guide/basic-usage/context-objects-complete-guide.md" >}}) for the properties exposed by `ConvertedContext`, `ConvertContext`, and `ConvertedPageContext`.
53+
2554
## Registering events with the classic API
2655

27-
Pass a `ConversionEvents` factory as the third argument to the [Converter](https://reference.groupdocs.com/conversion/net/groupdocs.conversion/converter/) constructor:
56+
Pass a `ConversionEvents` factory as the third argument to the `Converter` constructor:
2857

2958
```csharp
3059
using GroupDocs.Conversion;
@@ -33,9 +62,15 @@ using GroupDocs.Conversion.Options.Convert;
3362

3463
var events = new ConversionEvents
3564
{
36-
OnConversionCompleted = ctx => Console.WriteLine($"Done: {ctx.SourceFileName} -> {ctx.ConvertedFormat}"),
37-
OnConversionFailed = (ctx, ex) => Console.WriteLine($"Failed: {ctx.SourceFileName} ({ex.Message})"),
38-
OnConversionByPageFailed = (ctx, ex) => Console.WriteLine($"Page failed: {ex.Message}"),
65+
// Pipeline lifecycle
66+
OnConversionStarted = () => Console.WriteLine("Conversion started"),
67+
OnConversionProgress = percent => Console.WriteLine($"... {percent}% ..."),
68+
OnConversionCompleted = () => Console.WriteLine("Conversion finished"),
69+
70+
// Per-result
71+
OnDocumentConverted = ctx => Console.WriteLine($"Done: {ctx.SourceFileName} -> {ctx.ConvertedFormat}"),
72+
OnDocumentFailed = (ctx, ex) => Console.WriteLine($"Failed: {ctx.SourceFileName} ({ex.Message})"),
73+
OnPageFailed = (ctx, ex) => Console.WriteLine($"Page failed: {ex.Message}"),
3974
};
4075

4176
using (var converter = new Converter(
@@ -58,9 +93,13 @@ using GroupDocs.Conversion.Options.Convert;
5893
FluentConverter
5994
.WithEvents(e =>
6095
{
61-
e.OnConversionCompleted = ctx => Console.WriteLine($"Done: {ctx.SourceFileName}");
62-
e.OnConversionFailed = (ctx, ex) => Console.WriteLine($"Failed: {ex.Message}");
63-
e.OnConversionByPageFailed = (ctx, ex) => Console.WriteLine($"Page failed: {ex.Message}");
96+
e.OnConversionStarted = () => Console.WriteLine("Conversion started");
97+
e.OnConversionProgress = percent => Console.WriteLine($"... {percent}% ...");
98+
e.OnConversionCompleted = () => Console.WriteLine("Conversion finished");
99+
100+
e.OnDocumentConverted = ctx => Console.WriteLine($"Done: {ctx.SourceFileName}");
101+
e.OnDocumentFailed = (ctx, ex) => Console.WriteLine($"Failed: {ex.Message}");
102+
e.OnPageFailed = (ctx, ex) => Console.WriteLine($"Page failed: {ex.Message}");
64103
})
65104
.Load("sample.docx")
66105
.ConvertTo("converted.pdf").WithOptions(new PdfConvertOptions())
@@ -76,17 +115,17 @@ Handlers registered on `ConversionEvents` are **global** — they live for the l
76115
```csharp
77116
using (var converter = new Converter("source.docx", () => new ConverterSettings(), () => events))
78117
{
79-
converter.Convert("page1.pdf", new PdfConvertOptions()); // events.OnConversionCompleted fires
118+
converter.Convert("page1.pdf", new PdfConvertOptions()); // events.OnDocumentConverted fires
80119
converter.Convert("page2.tiff", new TiffConvertOptions()); // same global handler fires again
81120
}
82121
```
83122

84-
`Convert(...)` overloads that accept an `Action<ConvertedContext>` register a **per-call** result handler that wins over the global `OnConversionCompleted` for that single call:
123+
`Convert(...)` overloads that accept an `Action<ConvertedContext>` register a **per-call** result handler that wins over the global `OnDocumentConverted` for that single call:
85124

86125
```csharp
87126
using (var converter = new Converter("source.docx", () => new ConverterSettings(), () => events))
88127
{
89-
// Per-call handler — overrides events.OnConversionCompleted for THIS call only
128+
// Per-call handler — overrides events.OnDocumentConverted for THIS call only
90129
converter.Convert(
91130
new PdfConvertOptions(),
92131
(ConvertedContext ctx) =>
@@ -97,12 +136,12 @@ using (var converter = new Converter("source.docx", () => new ConverterSettings(
97136
}
98137
});
99138

100-
// No per-call handler — events.OnConversionCompleted fires
139+
// No per-call handler — events.OnDocumentConverted fires
101140
converter.Convert("page2.pdf", new PdfConvertOptions());
102141
}
103142
```
104143

105-
The precedence rule is `(perCall ?? global)?.Invoke(...)`: if a per-call handler is supplied, the global `OnConversionCompleted` does not fire for that call. `OnConversionFailed` and `OnConversionByPageFailed` are always global — there is no per-call override.
144+
The precedence rule is `(perCall ?? global)?.Invoke(...)`: if a per-call handler is supplied, the global `OnDocumentConverted` does not fire for that call. The other per-result handlers (`OnDocumentFailed`, `OnPageConverted`, `OnPageFailed`, `OnCompressionCompleted`) and all lifecycle handlers are always global — there is no per-call override.
106145

107146
Between consecutive `Convert(...)` calls on the same `Converter`, only the per-call slot is reset. The global `ConversionEvents` bag is preserved across runs.
108147

@@ -137,13 +176,14 @@ The previous registration mechanisms continue to work, but are obsolete and plan
137176

138177
| Obsolete (still works in v26.6) | Replacement |
139178
|---------------------------------|-------------|
140-
| `ConverterSettings.OnConversionFailed` | `ConversionEvents.OnConversionFailed` |
141-
| `ConverterSettings.OnConversionByPageFailed` | `ConversionEvents.OnConversionByPageFailed` |
179+
| `ConverterSettings.Listener` (`IConverterListener.Started` / `Progress` / `Completed`) | `ConversionEvents.OnConversionStarted` / `OnConversionProgress` / `OnConversionCompleted` |
180+
| `ConverterSettings.OnConversionFailed` | `ConversionEvents.OnDocumentFailed` |
181+
| `ConverterSettings.OnConversionByPageFailed` | `ConversionEvents.OnPageFailed` |
142182
| `ConverterSettings.OnCompressionCompleted` | `ConversionEvents.OnCompressionCompleted` |
143-
| Fluent chain `.OnConversionCompleted(...)` after `WithOptions(...)` | `FluentConverter.WithEvents(e => e.OnConversionCompleted = ...)` |
144-
| Fluent chain `.OnConversionFailed(...)` after `WithOptions(...)` | `FluentConverter.WithEvents(e => e.OnConversionFailed = ...)` |
183+
| Fluent chain `.OnConversionCompleted(...)` after `WithOptions(...)` | `FluentConverter.WithEvents(e => e.OnDocumentConverted = ...)` |
184+
| Fluent chain `.OnConversionFailed(...)` after `WithOptions(...)` | `FluentConverter.WithEvents(e => e.OnDocumentFailed = ...)` |
145185
| Fluent chain `.OnCompressionCompleted(...)` after `.Compress(...)` (`IConversionCompressResultCompleted`) | `FluentConverter.WithEvents(e => e.OnCompressionCompleted = ...)` |
146186

147-
When values are set on both the obsolete locations and on `ConversionEvents`, the aggregator wins. Handlers set on `ConverterSettings` are merged into the internal events bag at converter construction.
187+
When values are set on both the obsolete locations and on `ConversionEvents`, the aggregator wins. Handlers set on `ConverterSettings` (including a `Listener` instance) are forwarded into the internal events bag at converter construction.
148188

149189
See the [migration notes]({{< ref "conversion/net/developer-guide/migration-notes.md" >}}) for a step-by-step upgrade guide.

net/developer-guide/advanced-usage/listening.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ keywords: Track conversion process, Subscribe to conversion process events, trac
99
productName: GroupDocs.Conversion for .NET
1010
hideChildren: False
1111
---
12+
{{< hint style="important" title="Obsolete since v26.6 — use ConversionEvents instead" >}}
13+
The `IConverterListener` interface and the `ConverterSettings.Listener` property are marked obsolete in v26.6 and are planned for removal in v26.9. New code should subscribe to `OnConversionStarted` / `OnConversionProgress` / `OnConversionCompleted` on the [ConversionEvents]({{< ref "conversion/net/developer-guide/advanced-usage/conversion-events.md" >}}) aggregator instead.
14+
15+
Existing code that assigns a listener continues to work in v26.6 — the `Started` / `Progress` / `Completed` callbacks are forwarded to the corresponding `ConversionEvents` handlers at converter construction.
16+
{{< /hint >}}
17+
1218
In some cases, there is a need to monitor the conversion process and to receive updates upon a start, progress and completion of a conversion. For such situations, [**GroupDocs.Conversion**](https://products.groupdocs.com/conversion/net) exposes an extension point where the client application may hook up and receive updates. 
1319

1420
To enable listening, follow these steps:

0 commit comments

Comments
 (0)