Configuring Serialization in a Workflow Service

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 run time 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 SerializerOption property in the Properties Window.

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.

Screenshot of the KnownTypes property dialog box.

This displays the Type Collections Editor that allows you to search for and specify known types.

Screenshot of the Type Collections Editor.

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