Skip to content

Commit

Permalink
Merge pull request #16 from altso/exceptions
Browse files Browse the repository at this point in the history
Log hosting exceptions to debug output
  • Loading branch information
altso authored Feb 17, 2022
2 parents 5611c40 + c2aa9f9 commit 58ee8fc
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
33 changes: 33 additions & 0 deletions Source/ExcelRna.Extensions.Hosting.Tests/HostedExcelAddInTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using ExcelDna.Integration;
Expand All @@ -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<ApplicationException>(addIn.AutoOpen);
Assert.Throws<AggregateException>(addIn.AutoClose);
Assert.Equal(2, invalidExcelAddIn.Exceptions.Count);
}

private class TestExcelAddIn : HostedExcelAddIn, IHostedService
{
public bool IsRunning { get; private set; }
Expand All @@ -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<Exception> 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);
}
}
}
37 changes: 30 additions & 7 deletions Source/ExcelRna.Extensions.Hosting/HostedExcelAddIn.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using ExcelDna.Integration;
using System;
using System.Diagnostics;
using ExcelDna.Integration;
using Microsoft.Extensions.Hosting;

namespace ExcelRna.Extensions.Hosting;
Expand All @@ -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;
}
}
}

0 comments on commit 58ee8fc

Please sign in to comment.