Skip to content

Commit

Permalink
Merge pull request #350 from PHOENIXCONTACT/fix/shutdown-exception
Browse files Browse the repository at this point in the history
Fix disposed exception on shutdown
  • Loading branch information
Toxantron authored Oct 26, 2023
2 parents d356d90 + 23a7153 commit 10a3c7c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
7 changes: 5 additions & 2 deletions src/Moryx.Runtime.Kernel/KernelServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,27 @@ public static IConfigManager UseMoryxConfigurations(this IServiceProvider servic
return configManager;
}

private static IModuleManager _moduleManager;
/// <summary>
/// Boot system and start all modules
/// </summary>
/// <param name="serviceProvider"></param>
/// <returns></returns>
[Obsolete("Resolve IModuleManager and call StartModules directly")]
public static IModuleManager StartMoryxModules(this IServiceProvider serviceProvider)
{
var moduleManager = serviceProvider.GetRequiredService<IModuleManager>();
moduleManager.StartModules();
return moduleManager;
return _moduleManager = moduleManager;
}

/// <summary>
/// Stop all modules
/// </summary>
[Obsolete("Stopping modules on service collection causes an exception, call StopModules on the return value of StartModules")]
public static IModuleManager StopMoryxModules(this IServiceProvider serviceProvider)
{
var moduleManager = serviceProvider.GetRequiredService<IModuleManager>();
var moduleManager = _moduleManager ?? serviceProvider.GetRequiredService<IModuleManager>();
moduleManager.StopModules();
return moduleManager;
}
Expand Down
8 changes: 6 additions & 2 deletions src/StartProject.Asp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using Moryx.Tools;
using Moryx.Model;
using Moryx.Runtime.Endpoints;
using Microsoft.Extensions.DependencyInjection;
using Moryx.Runtime.Modules;

namespace StartProject.Asp
{
Expand All @@ -26,11 +28,13 @@ public static void Main(string[] args)
}).Build();

host.Services.UseMoryxConfigurations("Config");
host.Services.StartMoryxModules();

var moduleManager = host.Services.GetRequiredService<IModuleManager>();
moduleManager.StartModules();

host.Run();

host.Services.StopMoryxModules();
moduleManager.StopModules();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ public void SetupConfigurationToBeUsed()
}

[Test]
public void StartAllModules()
public void StartAndStopAllModules()
{
// Arrange
var moduleManagerMock = new Mock<IModuleManager>();
_serviceCollection.AddSingleton<IModuleManager>(moduleManagerMock.Object);
_serviceCollection.AddSingleton(moduleManagerMock.Object);
var provider = _serviceCollection.BuildServiceProvider();

// Act
Expand All @@ -98,22 +98,12 @@ public void StartAllModules()
// Assert
moduleManagerMock.Verify(m => m.StartModules(), Times.Once);
moduleManagerMock.VerifyNoOtherCalls();
}

[Test]
public void StopAllModules()
{
// Arrange
var moduleManagerMock = new Mock<IModuleManager>();
_serviceCollection.AddSingleton<IModuleManager>(moduleManagerMock.Object);
var provider = _serviceCollection.BuildServiceProvider();

// Act
provider.StopMoryxModules();

// Assert
moduleManagerMock.Verify(m => m.StopModules(), Times.Once);
moduleManagerMock.VerifyNoOtherCalls();
}
}
}

0 comments on commit 10a3c7c

Please sign in to comment.