Partager via


Workflow Service Authoring Styles

Windows Workflow Foundation supports the ability to connect to and exchange information from an existing service based on the contract information received during design time using a service discovery tool such as Svcutil.exe. However, Windows Workflow Foundation also supports the ability to generate a service contract definition and implementation using the new Windows Workflow Foundation classes specifically designed to create communication-based services.

Contract-first Workflow Services

A contract-first workflow service is a workflow that uses preexisting service contract information. You should make sure that the contract is available and included in your workflow service project. After this contract information is created, the ReceiveActivity activity provided by Windows Workflow Foundation can be used to implement a specific service contract operation.

A SendActivity activity is required to consume a service in the workflow, and a preexisting service contract is required by the SendActivity activity. There is no provision within Windows Workflow Foundation that allows you to create a service contract and call a service using that contract within the same project.

Defining a Service Contract in a Workflow Project

You can define a service contract interface within your workflow project and implement an operation from that contract using the ReceiveActivity activity. The following example shows how to define a service contract using the ServiceContractAttribute for the contract and an OperationContractAttribute for each operation supported by that contract.

using System;
using System.ServiceModel;

namespace Microsoft.WorkflowServices.Samples
{
    [ServiceContract(Namespace = "http://Microsoft.WorkflowServices.Samples")]
    public interface IContractFirstService
    {
        [OperationContract()]
        string DoWork();
    }
}

A ReceiveActivity activity can use the service definition shown above to implement, for example, the DoWork operation defined in this contract. Additionally, each ReceiveActivity activity can implement only a single operation for a contract.

Workflow-first Workflow Services

In the previous section, a contract-first service is defined as a workflow service that implements an operation or several operations for a preexisting service contract.

Windows Workflow Foundation also supports the ability to define a service contract programmatically using the Windows Workflow Foundation classes. Just as in a contract-first service, however, a SendActivity activity cannot create its own service contract to communicate with an external service. This behavior is used by the ReceiveActivity activity to implement an operation defined by a service contract.

The OperationInfo class in Windows Workflow Foundation can be used to define a workflow service. The contract information itself is defined using the ContractName property and the operation name is set using the Name parameter. If the operation supports parameters, including a return value, use the OperationParameterInfo class and add each parameter to the Parameters collection of the OperationInfo object.

Note

For return values, use the value "(ReturnValue)" for the Name property and the value -1 for the Position property of the OperationParameterInfo object you are creating.

The following example shows how to create a workflow-first workflow service using the OperationInfo and OperationParameterInfo classes. The service contract is named WorkflowFirstContract and it contains a single operation named DoWork.

OperationInfo doWorkOperation = new OperationInfo();
OperationParameterInfo returnValueParameter = new OperationParameterInfo();

doWorkOperation.ContractName = "WorkflowFirstContract";
doWorkOperation.Name = "DoWork";
returnValueParameter.Attributes = ((System.Reflection.ParameterAttributes)((System.Reflection.ParameterAttributes.Out | System.Reflection.ParameterAttributes.Retval)));
returnValueParameter.Name = "(ReturnValue)";
returnValueParameter.ParameterType = typeof(string);
returnValueParameter.Position = -1;
doWorkOperation.Parameters.Add(operationparameterinfo1);

See Also

Reference

ServiceContractAttribute
ReceiveActivity
SendActivity
OperationInfo