Configuring Serialization in a Workflow Service

This topic applies to Windows Workflow Foundation 4 (WF4).

Workflow services are Windows Communication Foundation (WCF) services and so have the option of using either the DataContractSerializer (the default) or the XmlSerializer. When writing non-workflow services the type of serializer to use is specified on the service or operation contract. When creating WCF workflow services you don’t specify these contracts in code, but rather they are generated at runtime by contract inference. For more information about contract inference, see Using Contracts in Workflow. The serializer is specified using the SerializerOption property. This can be set in the designer as shown in the following illustration.

Setting the serializer

The serializer can also be set in code as shown in the following example,

Receive approveExpense = new Receive
            {
                OperationName = "ApproveExpense",
                CanCreateInstance = true,
                ServiceContractName = "FinanceService",
                SerializerOption = SerializerOption.DataContractSerializer,
                Content = ReceiveContent.Create(new OutArgument<Expense>(expense))
            };

Known types can be specified on Workflow services as well. For more information about Known Types see Data Contract Known Types. Known types can be specified in the designer or in code. To specify known types in the designer, click the ellipsis button next to the KnownTypes property in the properties window for a Receive activity as shown in the following illustration.

KnownTypes property

This will display the Type Collections Editor that will allow you to search for and specify known types.

Adding Known Types

Click the Add new type link and use the drop down to select or search for a type to add to the known types collection. To specify known types in code use the KnownTypes property as shown in the following example.

Receive approveExpense = new Receive
            {
                OperationName = "ApproveExpense",
                CanCreateInstance = true,
                ServiceContractName = "FinanceService",
                SerializerOption = SerializerOption.DataContractSerializer,
                Content = ReceiveContent.Create(new OutArgument<Expense>(expense))
            };
            approveExpense.KnownTypes.Add(typeof(Travel));
            approveExpense.KnownTypes.Add(typeof(Meal));

To see a complete code example showing how to configure serialization for a workflow service see Formatting messages in Workflow Services.