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

On wpf application workflow doesn't run at first #1252

Open
heitorgiacomini opened this issue Apr 6, 2024 · 3 comments
Open

On wpf application workflow doesn't run at first #1252

heitorgiacomini opened this issue Apr 6, 2024 · 3 comments

Comments

@heitorgiacomini
Copy link

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.

@cjundt
Copy link

cjundt commented Apr 8, 2024

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

@brucezlata
Copy link
Contributor

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
Copy link
Contributor

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" });
    }
}

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

3 participants