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.
EDIT: I will add the plugin class and how it is used:
[Plugin(PluginType = typeof(IPlugin))]
[PluginDescription(Description = "Email plugin.")]
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(
EmailInputModel parameters,
PluginExecutionContext context,
IServiceProvider provider)
{
IEmailPluginService service = provider.GetRequiredService<IEmailPluginService>();
return await service.ExecuteAsync(parameters, context);
}
}
// method that calls the plugin execute method:
public async Task<PluginExecutionResult> ExecuteAsync(string parameters, PluginExecutionContext context)
{
try
{
var pluginParameters = JsonConvert.DeserializeObject<TInput>(parameters);
using var scope = _serviceProvider.CreateScope();
return await ExecutePluginAsync(pluginParameters, context, scope.ServiceProvider);
}
catch (Exception exception)
{
// this is where I expect the exception to be caught, but somehow it isn't
throw;
}
}
EDIT2: I managed to fix this by catching the exception OperationCanceledException
after it timeouts during connect and handling it to throw a custom exception in the catch
block.
I managed to fix this by catching the exception OperationCanceledException
after it timeouts during connect and handling it to throw a custom exception in the catch
block.