R – WF — how to use a custom activity without creating it in a separate Workflow Activity Library

networkflow-activityworkflow-foundation

I am trying to accomplish something that seems like it should be very simple. I have a State Machine Workflow Console Application with a workflow in it. I have created a custom activity for it. This activity will NEVER be used ANYWHERE ELSE. I just want to use this activity on my workflow, but:

  1. It does not appear in the toolbox.
  2. I cannot drag it from the Solution Explorer onto the workflow designer.

I absolutely do not want to create a separate State Machine Workflow Activity Library, since that will just clutter my solution. Like I said, I will never use this activity in any other project, so I would like to keep it confined to this one…but I just can't figure out how to get it onto the designer! Am I going crazy!?

Here is the code for the activity:

public partial class GameSearchActivity: Activity
{
    public GameSearchActivity()
    {
        InitializeComponent();
    }

    public static DependencyProperty QueryProperty = System.Workflow.ComponentModel.DependencyProperty.Register("Query", typeof(string), typeof(GameSearchActivity));
    [Description("Query")]
    [Category("Dependency Properties")]
    [Browsable(true)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public string Query
    {
        get
        {
            return ((string)(base.GetValue(GameSearchActivity.QueryProperty)));
        }
        set
        {
            base.SetValue(GameSearchActivity.QueryProperty, value);
        }
    }

    public static DependencyProperty ResultsProperty = System.Workflow.ComponentModel.DependencyProperty.Register("Results", typeof(string), typeof(GameSearchActivity));
    [Description("Results")]
    [Category("Dependency Properties")]
    [Browsable(true)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public IEnumerable<Game_GamePlatform> Results
    {
        get
        {
            return ((IEnumerable<Game_GamePlatform>)(base.GetValue(GameSearchActivity.ResultsProperty)));
        }
        set
        {
            base.SetValue(GameSearchActivity.ResultsProperty, value);
        }
    }

    protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
    {
        IDataService ds = executionContext.GetService<IDataService>();
        Results = ds.SearchGames(Query);

        return ActivityExecutionStatus.Closed;
    }
}

Thanks.


EDIT:

OK, so I've discovered that if I change the project type from Console Application to Class Library, the custom activity appears in the toolbox. However, this is not acceptable. It needs to be a Console/Windows Application.

Anyone know a way around this?

Best Answer

It looks like you've uncovered a bug in Visual Studio. I'm sure you could hack it to make it work, but, have you considered going with it and keeping the workflow bits in a class library and referencing them from a simple console application? Yes, this creates an EXE and a DLL, but the cost of doing so is trivial and actually separates your layers better (UI versus business logic) and enables better reuse in the future.