merken / Prise

A .NET Plugin Framework.
https://merken.github.io/Prise
MIT License
359 stars 39 forks source link

Why is TaskCanceledException not being caught but other exceptions are? #70

Open dansorescu-mhc opened 1 year ago

dansorescu-mhc commented 1 year ago

I am using Prise Plugin Framework and MailKit SMTP Client.

During the ConnectAsync call, if the async call timeouts, it will throw a TaskCanceledException. This will be caught in the caller and after logging it will be thrown down the call stack. But this will not be caught after the Execute method in the Prise plugin and the app will crash - other exceptions are being caught fine and don't crash the app.

try
{
    await _smtpClient.ConnectAsync(sendEmail.Host, sendEmail.Port);
    // other code
}
catch
{
    // logging
    throw;
}

This code is being called by the plugin with return await service.ExecuteAsync(parameters, context);

I'm expecting this to throw the exception to its parent but it doesn't happen.

The plugin looks as follows:

[Plugin(PluginType = typeof(IPlugin))]
public class Email : BasePlugin<EmailInputModel>
{
    [field: PluginService(ProvidedBy = ProvidedBy.Host, ServiceType = typeof(IServiceProvider))]
    private IServiceProvider ServiceProvider { get; set; }

    [PluginActivated]
    public void OnActivated()
    {
        InitializeServices(ServiceProvider);
    }

    protected override async Task<PluginExecutionResult> ExecutePluginAsync(
        InputModel parameters,
        PluginExecutionContext context,
        IServiceProvider provider)
    {
        IEmailPluginService service = provider.GetRequiredService<IEmailPluginService>();
        return await service.ExecuteAsync(parameters, context);
    }
}
dansorescu-mhc commented 1 year ago

We can close this issue since I managed to fix it by catching the OperationCanceledException and throw a custom exception in the catch block