diff --git a/Source/ExcelRna.Extensions.Hosting.Tests/HostedExcelAddInTests.cs b/Source/ExcelRna.Extensions.Hosting.Tests/HostedExcelAddInTests.cs index 0c314d0..ebaf51a 100644 --- a/Source/ExcelRna.Extensions.Hosting.Tests/HostedExcelAddInTests.cs +++ b/Source/ExcelRna.Extensions.Hosting.Tests/HostedExcelAddInTests.cs @@ -1,3 +1,5 @@ +using System; +using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using ExcelDna.Integration; @@ -24,6 +26,19 @@ public void HostedExcelAddIn_should_start_and_stop_host() Assert.False(testExcelAddIn.IsRunning); } + [Fact] + public void HostedExcelAddIn_should_call_OnException() + { + // ARRANGE + var invalidExcelAddIn = new InvalidExcelAddIn(); + IExcelAddIn addIn = invalidExcelAddIn; + + // ACT & ASSERT + Assert.Throws(addIn.AutoOpen); + Assert.Throws(addIn.AutoClose); + Assert.Equal(2, invalidExcelAddIn.Exceptions.Count); + } + private class TestExcelAddIn : HostedExcelAddIn, IHostedService { public bool IsRunning { get; private set; } @@ -43,4 +58,22 @@ public Task StopAsync(CancellationToken cancellationToken) protected override IHostBuilder CreateHostBuilder() => Host.CreateDefaultBuilder() .ConfigureServices(services => services.AddHostedService(_ => this)); } + + private class InvalidExcelAddIn : HostedExcelAddIn, IHostedService + { + public List Exceptions { get; } = new(); + + public Task StartAsync(CancellationToken cancellationToken) => throw new ApplicationException(); + + public Task StopAsync(CancellationToken cancellationToken) => throw new ApplicationException(); + + protected override IHostBuilder CreateHostBuilder() => new HostBuilder() + .ConfigureServices(services => services.AddHostedService(_ => this)); + + protected override void OnException(Exception e) + { + base.OnException(e); + Exceptions.Add(e); + } + } } diff --git a/Source/ExcelRna.Extensions.Hosting/HostedExcelAddIn.cs b/Source/ExcelRna.Extensions.Hosting/HostedExcelAddIn.cs index c6bfd14..94508b9 100644 --- a/Source/ExcelRna.Extensions.Hosting/HostedExcelAddIn.cs +++ b/Source/ExcelRna.Extensions.Hosting/HostedExcelAddIn.cs @@ -1,4 +1,6 @@ -using ExcelDna.Integration; +using System; +using System.Diagnostics; +using ExcelDna.Integration; using Microsoft.Extensions.Hosting; namespace ExcelRna.Extensions.Hosting; @@ -15,19 +17,40 @@ protected virtual void AutoClose(IHost host) { } + protected virtual void OnException(Exception e) + { + Debug.WriteLine(e); + } + protected abstract IHostBuilder CreateHostBuilder(); void IExcelAddIn.AutoOpen() { - _host = CreateHostBuilder().Build(); - _host.StartAsync().GetAwaiter().GetResult(); - AutoOpen(_host); + try + { + _host = CreateHostBuilder().Build(); + _host.StartAsync().GetAwaiter().GetResult(); + AutoOpen(_host); + } + catch (Exception e) + { + OnException(e); + throw; + } } void IExcelAddIn.AutoClose() { - AutoClose(_host); - _host.StopAsync().GetAwaiter().GetResult(); - _host.Dispose(); + try + { + AutoClose(_host); + _host.StopAsync().GetAwaiter().GetResult(); + _host.Dispose(); + } + catch (Exception e) + { + OnException(e); + throw; + } } }