elsa-workflows / elsa-workflows/elsa-core
System.ObjectDisposedException within Integration Tests
- Dominant language
- C#
- Stars
- 7.9k
- Forks
- 1.5k
- Avg merge
- 15h 22m
- Merged PRs (30d)
- 114
Description
Hi,
I have an integration test for my whole app but sometimes the test fails because of an System.ObjectDisposedException thrown by some Elsa classes.
The test uses the TestServer class from Microsoft to host the whole application.
After the test has finished I call Dispose on the TestServer to cleanup everything.
Now sometimes I get the following error:
```
The active test run was aborted. Reason: Test host process crashed : Unhandled exception. System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'IServiceProvider'.
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ThrowHelper.ThrowObjectDisposedException()
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
at Elsa.Mediator.Middleware.Command.MiddlewareExtensions.<>c__DisplayClass0_0`1.b__0(CommandMiddlewareDelegate next)
at Elsa.Mediator.Middleware.Command.CommandPipelineBuilder.Build()
at Elsa.Mediator.Middleware.Command.CommandPipeline.Setup(Action`1 setup)
at Elsa.Mediator.Middleware.Command.CommandPipeline.CreateDefaultPipeline()
at Elsa.Mediator.Middleware.Command.CommandPipeline.get_Pipeline()
at Elsa.Mediator.Middleware.Command.CommandPipeline.InvokeAsync(CommandContext context)
at Elsa.Mediator.Services.DefaultMediator.SendAsync(ICommand command, ICommandStrategy strategy, CancellationToken cancellationToken)
at Elsa.Mediator.Services.DefaultMediator.SendAsync(ICommand command, CancellationToken cancellationToken)
at Elsa.Scheduling.ScheduledTasks.ScheduledCronTask.b__10_0(Object _, ElapsedEventArgs _)
at System.Threading.Tasks.Task.<>c.b__128_1(Object state)
at System.Threading.QueueUserWorkItemCallback.Execute()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()
```
Within my test I add a workflow programmatically with this code `elsa.AddWorkflow();`
```
public class ElsaTestWorkflow : WorkflowBase
{
protected override void Build(IWorkflowBuilder builder)
{
builder.Name = "Test Workflow2";
builder.Root = new Sequence
{
Activities =
{
new Cron
{
CronExpression = new("0 0/1 * * * ?"),
CanStartWorkflow = true
},
new WriteLine("Test Workflow triggered")
}
};
}
}
```
I think the problem is that something from Elsa will not be disposed correctly while calling TestServer.Dispose and therefore the Cron activity inside the workflow will trigger again and again, even if the IServiceProvider is already disposed.
Is it possible to stop Elsa completely or to dispose everything before I dispose the TestServer or is this a bug which should be fixed by you?
Elsa Version: 3.0.5
Contributor guide
Assessment
This issue has not been assessed yet.