workflow-core icon indicating copy to clipboard operation
workflow-core copied to clipboard

On wpf application workflow doesn't run at first

Open heitorgiacomini opened this issue 10 months ago • 3 comments

Describe the bug On wpf application workflow doesn't run at first. On btn click it didnt execute workflow, but if i click in the button again, then the workflow runs.

To Reproduce

public partial class MainWindow : Window
{
    private readonly IWorkflowHost _workflowHost; 
    public MainWindow()
    {
          InitializeComponent();
        
          _workflowHost = ((App)Application.Current).serviceProvider.GetService<IWorkflowHost>();
          _workflowHost.RegisterWorkflow<CodeGenerationWorkflow, ContainerModel>();
    }
    private void btn_RunCodeGenerator_Click(object sender, RoutedEventArgs e)
    {
        _workflowHost.Start();
        Console.ReadLine();
          _workflowHost.StartWorkflow("CodeGenerationWorkflow", 1, containerModel);
          Console.ReadLine();
          _workflowHost.Stop();
    }
}

Expected behavior On btn click execute pipeline.

Additional context Wpf application.

heitorgiacomini avatar Apr 06 '24 18:04 heitorgiacomini

Hi Heitor,

Host should run continously, in background, generally as a service. You're not supposed to start/stop it each time you want to run a workflow. In your case, the workflow instance has juste the time to register and not to start before you stop the host service. At least if your host is embeded in your WPF application, it should be running the whole time your application is running.

Yours, Christian

cjundt avatar Apr 08 '24 08:04 cjundt

Hi Heitor, In App.xmal.cs, create Microsoft.Extensions.Hosting

    private static readonly IHost _host = Host.CreateDefaultBuilder()
        .ConfigureAppConfiguration(c =>
        {
            c.SetBasePath(AppContext.BaseDirectory)
                .AddJsonFile(LoggingConfigFileSystem, optional: false, reloadOnChange: true);
        })
        .ConfigureServices(
            (_, services) =>
            {
                // Workflow core
                services.AddWorkflow();

                // Add workflow steps
                services.AddTransient<CxxxWriterStep>();
            }
        )
        .Build();

Here start host and register workflow

   private void OnStartup(object sender, StartupEventArgs e)
   {
       _host.Start();

       // RegisterWorkflow
       _wokflowhost = GetRequiredService<IWorkflowHost>();
       _wokflowhost.RegisterWorkflow<MyWorkflow, MyData>();
       _wokflowhost.Start();
   }
    private void OnExit(object sender, ExitEventArgs e)
    {
        _host.StopAsync().Wait();

        _wokflowhost.Stop();
        _host.Dispose();
    }

brucezlata avatar Apr 15 '24 10:04 brucezlata

Also, you need to DI your pages, view models etc. in ConfigureServices. So you can use it in page.

public partial class TestControlPage : Page
{
    private readonly IWorkflowHost _host;

    public TestControlPage(IWorkflowHost host)
    {
        _host = host;
    }

    private void StartButtonClick(object sender, RoutedEventArgs e)
    {
        _ = _host.StartWorkflow("xxxx", 1, new MyData { xxx = "xxx" });
    }
}

brucezlata avatar Apr 15 '24 10:04 brucezlata