How to Consume Services from a Workflow?

WCF is a programming model for building service-oriented applications. It enables developers to build reliable, secure, transaction-enabled solutions across platforms. WCF also provides the ability to implement legacy systems. By using WCF and WF, you can exploit the advantages of WCF security and stability, and implement service-based workflow applications.

What Are WCF Services?

WCF is a unified platform that enables you to implement services. You can use WCF to communicate between services and clients, and integrate internal line-of-business applications from diverse programming paradigms, languages, and architectures.

WCF utilizes interfaces, which define the communication channels and data that pass between a service and any consuming applications.

Interfaces are detached from the internal logic of the service, which enables you to modify service functionality without the need to update client systems.

WF includes built-in functionality that enables you to consume workflow services in your workflow applications, in the form of the SendActivity activity.

The SendActivity activity enables your workflow to synchronously invoke a Web service operation.

When you implement a SendActivity activity, you create a specification for a proxy object. This proxy marshals any messages from the SendActivity activity and forwards them to the service for processing. The service passes any resultant data or messages back to the workflow instance that called the service.

The SendActivity activity enables you to use three communication methodologies. One-Way Send is where the activity sends a message to the service and then continues execution without expecting a response.

Send Request – Receive Reply is where the activity sends a message, pauses execution until a response returns, and then continues execution.

Send Request – Receive Fault is where the activity sends a message, waits until the service returns a response or an error, and then continues execution.

During this type of execution, the SendActivity activity throws all of the faults and exceptions that are received from the service, which should be handled by using FaultHandlerActivity activities

Defining a WCF Service

To create a WCF Service Library and define a WCF service:

  1. Define a service interface and annotate it with ServiceContract.

  2. Annotate the interface methods with OperationContract.

  3. Annotate parameter and return data types with DataContract.

  4. Define a class that implements the service interface.

The following attributes can be applied to members of your service interface.

Attribute

Description

ServiceContract

The ServiceContract attribute indicates that an interface or class exposes methods or events that an external application can consume. When you implement the service contract by using WCF, the service contract is exposed and can be used by client applications.

OperationContract

The OperationContract attribute indicates that a method defines an operation that is part of a service in WCF. You must annotate any methods in the service with this attribute to enable external applications to invoke the method; otherwise, the method is not exposed.

DataContract

The DataContract attribute specifies that a type is used to hold or manipulate data that is used in WCF communication. It is necessary to annotate types that are serialized or deserialized during WCF communication.

DataMember

The DataMember attribute specifies that the member is part of a data contract and will be serialized or deserialized when the member is used in WCF communications.

 

WCF provides several attributes that you can use when you design a WCF service. The following code examples show how to define a service that exposes a single method, retrieveCustomerData, that passes a custom Customer object back to the caller. The service class is annotated with the ServiceContract attribute, and the retrieveCustomerData method with the OperationContract attribute. The Customer object is a custom type and is passed as part of a WCF communication, so it is annotated with the DataContract attribute, and the data members that are serialized as part of the object are annotated with the DataMember attribute.

namespace Contoso.CustomerServices

{

    [ServiceContract]

    public interface ICustomerDataService

    {

        [OperationContract]

        Customer retrieveCustomerData(int value);

    }

 

    [DataContract]

    public class Customer

    {

        public Customer(string firstName,

                        string lastName,

                        DateTime dateOfBirth)

        {

            _firstName = firstName;

            _lastName = lastName;

            _dateOfBirth = dateOfBirth;

            _age = DateTime.Now.Year - dateOfBirth.Year;

        }

 

        [DataMember]

        public string _firstName;

        [DataMember]

        public string _lastName;

        [DataMember]

        public DateTime _dateOfBirth;

 

        private int _age;                        

    }

}

For more information about defining WCF services, see Designing Service Contracts.