Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Workflow Foundation - Literal only supports value types and the immutable type System.String

I have the following unit test for a WF code activity called MyCodeActivity:

[ExpectedException(typeof(ArgumentException))]
[TestMethod]
public void ShouldRequireParam()
{
    //arrange
    var invoker = new WorkflowInvoker(new MyCodeActivity()
    {
        MyInt = 2,
        MyComplexObject = _complexObject
    });

    //act
    invoker.Invoke();

    //assert
    Assert.Fail("Expected ArgumentException");
}

When I run the test I get the following exception

'Literal< MyComplexObject>': Literal only supports value types and the immutable type System.String. The type MyComplexObject cannot be used as a literal.

like image 592
profMamba Avatar asked May 14 '13 22:05

profMamba


People also ask

Can Workflow arguments be added to a tree of in-memory objects?

This includes workflow arguments passed to the root activity. In imperative code, workflow arguments are specified as properties on a new CLR type, and in XAML they are declared by using x:Class and x:Member. Because there is no new CLR type created when a workflow definition is created as a tree of in-memory objects, arguments cannot be added.

How do I add Workflow arguments in XAML?

In imperative code, workflow arguments are specified as properties on a new CLR type, and in XAML they are declared by using x:Class and x:Member. Because there is no new CLR type created when a workflow definition is created as a tree of in-memory objects, arguments cannot be added. However, arguments can be added to a DynamicActivity.

What is a workflow definition?

A workflow definition is a tree of configured activity objects. This tree of activities can be defined many ways, including by hand-editing XAML or by using the Workflow Designer to produce XAML. Use of XAML, however, is not a requirement. Workflow definitions can also be created programmatically.

When do I need to compile C# expressions in a workflow?

C# expressions must be compiled before the workflow containing them is invoked. If the C# expressions are not compiled, a NotSupportedException is thrown when the workflow is invoked with a message similar to the following: Expression Activity type 'CSharpValue`1' requires compilation in order to run.


1 Answers

To fix the immediate problem:

MyComplexObject = _complexObject

to

MyComplexObject = new InArgument<MyComplexObject>((ctx) => _complexObject)

Further reading : http://msdn.microsoft.com/en-us/library/ee358749.aspx .

Note: You should also use the Microsoft.Activities.UnitTesting package available on NuGet. It makes IOC alot easier (seeing as WF works with the Service Locator pattern and not Dependency Injection)

like image 171
profMamba Avatar answered Oct 08 '22 02:10

profMamba