Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AsyncCommnad not Cancelable #59

Open
carsten-riedel opened this issue Mar 28, 2024 · 1 comment
Open

AsyncCommnad not Cancelable #59

carsten-riedel opened this issue Mar 28, 2024 · 1 comment

Comments

@carsten-riedel
Copy link

AsyncCommnad not Cancelable

appLifetime.ApplicationStopping will never stop even if called by an other background service or strg+c. (return 99 in my example)

I didn't found a solution right now, do you have some suggestion ?

        public class MyAsyncCommand : AsyncCommand<MyAsyncCommand.Settings>
        {
            private readonly DependencyService  dependencyService;
            private readonly IHostApplicationLifetime appLifetime;

            public MyAsyncCommand(DependencyService service, IHostApplicationLifetime appLifetime)
            {
                this.dependencyService = service;
                this.appLifetime = appLifetime;
            }

            public class Settings : CommandSettings
            {
                [Description("The shell commandline command")]
                [CommandArgument(0, "<CommandName>")]
                public string? CommandName { get; init; }
            }

            public override async Task<int> ExecuteAsync(CommandContext context, Settings settings)
            {
                dependencyService.PerformAction();
                try
                {
                    await Task.Delay(20000, appLifetime.ApplicationStopping);
                }
                catch (Exception ex)
                {
                    return 99;
                }
                return 1;
            }
        }

        public class DependencyService
        {
            public void PerformAction()
            {
                Console.WriteLine("Action performed.");
                AnsiConsole.Write(new Markup("[bold yellow]Hello[/] [red]World![/]"));
            }
        }
    }
    ```
@snargledorf
Copy link

@carsten-riedel I've created a similar library that doesn't have this issue if you want to check it out.

https://github.com/snargledorf/SpectreConsoleHost

@JuergenRB fyi, this issue is due to the fact that currently you are building your own copy of the IServiceProvider

This service provider is independent from the service provider built by the host, which means the worker service is getting its IHostApplicationLifetime from the hosts service provider, while the commands are getting theirs from the service provider your TypeRegistrar built.

I solved this by implementing a ITypeRegistrar and ITypeResolver which have their own registry of type factories, but then fall back to an IServiceProvider which is sourced from the compiled host.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants