Skip to content

Commit 58ee8fc

Browse files
authored
Merge pull request #16 from altso/exceptions
Log hosting exceptions to debug output
2 parents 5611c40 + c2aa9f9 commit 58ee8fc

File tree

2 files changed

+63
-7
lines changed

2 files changed

+63
-7
lines changed

Source/ExcelRna.Extensions.Hosting.Tests/HostedExcelAddInTests.cs

+33
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System;
2+
using System.Collections.Generic;
13
using System.Threading;
24
using System.Threading.Tasks;
35
using ExcelDna.Integration;
@@ -24,6 +26,19 @@ public void HostedExcelAddIn_should_start_and_stop_host()
2426
Assert.False(testExcelAddIn.IsRunning);
2527
}
2628

29+
[Fact]
30+
public void HostedExcelAddIn_should_call_OnException()
31+
{
32+
// ARRANGE
33+
var invalidExcelAddIn = new InvalidExcelAddIn();
34+
IExcelAddIn addIn = invalidExcelAddIn;
35+
36+
// ACT & ASSERT
37+
Assert.Throws<ApplicationException>(addIn.AutoOpen);
38+
Assert.Throws<AggregateException>(addIn.AutoClose);
39+
Assert.Equal(2, invalidExcelAddIn.Exceptions.Count);
40+
}
41+
2742
private class TestExcelAddIn : HostedExcelAddIn, IHostedService
2843
{
2944
public bool IsRunning { get; private set; }
@@ -43,4 +58,22 @@ public Task StopAsync(CancellationToken cancellationToken)
4358
protected override IHostBuilder CreateHostBuilder() => Host.CreateDefaultBuilder()
4459
.ConfigureServices(services => services.AddHostedService(_ => this));
4560
}
61+
62+
private class InvalidExcelAddIn : HostedExcelAddIn, IHostedService
63+
{
64+
public List<Exception> Exceptions { get; } = new();
65+
66+
public Task StartAsync(CancellationToken cancellationToken) => throw new ApplicationException();
67+
68+
public Task StopAsync(CancellationToken cancellationToken) => throw new ApplicationException();
69+
70+
protected override IHostBuilder CreateHostBuilder() => new HostBuilder()
71+
.ConfigureServices(services => services.AddHostedService(_ => this));
72+
73+
protected override void OnException(Exception e)
74+
{
75+
base.OnException(e);
76+
Exceptions.Add(e);
77+
}
78+
}
4679
}

Source/ExcelRna.Extensions.Hosting/HostedExcelAddIn.cs

+30-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using ExcelDna.Integration;
1+
using System;
2+
using System.Diagnostics;
3+
using ExcelDna.Integration;
24
using Microsoft.Extensions.Hosting;
35

46
namespace ExcelRna.Extensions.Hosting;
@@ -15,19 +17,40 @@ protected virtual void AutoClose(IHost host)
1517
{
1618
}
1719

20+
protected virtual void OnException(Exception e)
21+
{
22+
Debug.WriteLine(e);
23+
}
24+
1825
protected abstract IHostBuilder CreateHostBuilder();
1926

2027
void IExcelAddIn.AutoOpen()
2128
{
22-
_host = CreateHostBuilder().Build();
23-
_host.StartAsync().GetAwaiter().GetResult();
24-
AutoOpen(_host);
29+
try
30+
{
31+
_host = CreateHostBuilder().Build();
32+
_host.StartAsync().GetAwaiter().GetResult();
33+
AutoOpen(_host);
34+
}
35+
catch (Exception e)
36+
{
37+
OnException(e);
38+
throw;
39+
}
2540
}
2641

2742
void IExcelAddIn.AutoClose()
2843
{
29-
AutoClose(_host);
30-
_host.StopAsync().GetAwaiter().GetResult();
31-
_host.Dispose();
44+
try
45+
{
46+
AutoClose(_host);
47+
_host.StopAsync().GetAwaiter().GetResult();
48+
_host.Dispose();
49+
}
50+
catch (Exception e)
51+
{
52+
OnException(e);
53+
throw;
54+
}
3255
}
3356
}

0 commit comments

Comments
 (0)