none
How do I make a .Net object a DynamicValue? RRS feed

  • Question

  • I have workflows that I want to start using with Workflow Manager, however, from what I have heard, I can't just use my regular .Net types in there.  So for example, outside of WFM, my workflows have a reference to a .Net component that has classes like Employee, Customer etc...data types.

    Can I still use these, as is in WFM or do I need to somehow convert these to DynamicValue types?


    STom

    Thursday, April 25, 2013 1:04 PM

All replies

  • The good news is that you can add your types to the trusted surface. The bad news is that once added translation does not include the accessors needed to read/write your type's properties -- you have to write your own custom code activities, adding them to the trusted surface, in order to read/write the individual properties you need in the workflow. This may be fine for small types, otherwise using DynamicValue may prove the lesser evil in the long run.

    IMHO, this issue presents a serious impediment to Workflow Manager adoption for any shop that's already invested heavily in workflow development; being unable to consume and leverage existing workflows that use .Net types is a misstep that I hope the development team will redress.

    Thursday, April 25, 2013 5:10 PM
  • Oops - sorry, forgot to answer the first part of the question: how to convert a .Net type to a DynamicValue.

    One way is to serialize the .Net type to JSON. There are several approaches (and libraries like JSON.NET might help). See http://msdn.microsoft.com/en-us/library/bb412179.aspx for an example of how to define a data contract for a person object and serialize to JSON memory stream using just the .Net runtime.  

    To convert the stream into a string so we can supply it as an input parameter to an arbitrary workflow (say, MyWorkflowActivity) that we invoke takes just a few additional lines of code:

    var sr = new StreamReader(stream);
    var json = sr.ReadToEnd();
    
    IDictionary<string, object> inputs = new Dictionary<string, object>
                {
                    { "myJson",  json }
                };
    
    WorkflowInvoker.Invoke(new MyWorkflowActivity(), inputs);
    

    In MyWorkflowActivity, we would define an input argument ("myJson") of type string and then use the ParseDynamicValue activity to convert the input string to a DynamicValue (declared as a variable within the workflow) for subsequent use.

    I hope that helps.


    • Edited by Spaghetti Soup Thursday, April 25, 2013 8:23 PM Ugly code formatting.
    • Proposed as answer by Sohel Rana Thursday, May 9, 2013 3:25 AM
    Thursday, April 25, 2013 8:19 PM