Compartilhar via


Using the ReceiveActivity Activity

The ReceiveActivity activity is designed to implement a service contract operation within a workflow. Given a service contract, the ReceiveActivity activity will wait until a client connects to the service and then run its contained child activities.

The ReceiveActivity activity has the ability to implement a service contract operation that has already been defined using Windows Communication Foundation (WCF) (contract-first) or can build the service contract using the Windows Workflow Foundation classes (workflow-first). For more information about the different workflow service authoring styles, see Workflow Service Authoring Styles.

Implementing a Service Contract Using the ReceiveActivity Activity

When the ReceiveActivity activity is scheduled to run, it will listen for an incoming connection from a client on a service endpoint. The ReceiveActivity activity can implement only a single operation defined in a service contract. The operation is associated with a ReceiveActivity activity using a TypedOperationInfo object. The TypedOperationInfo class includes information about the ContractType and the name of the operation, as well as identity information that the client needs to successfully run the operation. The following example shows how to create a ReceiveActivity activity and associated a TypedOperationInfo object to implement an operation defined in a service contract. The service contract is the same contract shown in the previous section.

this.doWorkReceiveActivity = new ReceiveActivity();
TypedOperationInfo doWorkOperationInfo = new TypedOperationInfo();

this.doWorkReceiveActivity.CanCreateInstance = true;
this.doWorkReceiveActivity.Name = "doWorkReceiveActivity";
doWorkOperationInfo.ContractType = typeof(Microsoft.WorkflowServices.Samples.IContractFirstService);
doWorkOperationInfo.Name = "DoWork";
doWorkOperationInfo.PrincipalPermissionRole = "";
this.doWorkReceiveActivity.ServiceOperationInfo = doWorkOperationInfo;

An operation can support any number of parameters as well as a return value. To support this, the ReceiveActivity activity uses the parameter binding mechanism of Windows Workflow Foundation through its ParameterBindings property. The following example demonstrates how to use a parameter binding to associate a workflow class property with the return value of the operation shown in the contract earlier. In this example, an ActivityBind object is created using the ReturnValue string property defined in a workflow service class implementation. A WorkflowParameterBinding is then created and associated with the ActivityBind object using the SetBinding method. Finally, the parameter binding is associated with the ReceiveActivity activity by adding it to the ParameterBindings collection.

ActivityBind activitybind1 = new ActivityBind();

activitybind1.Name = "ContractFirstServiceWorkflow";
activitybind1.Path = "ReturnValue";
WorkflowParameterBinding returnValueBinding = new WorkflowParameterBinding();
returnValueBinding.ParameterName = "(ReturnValue)";
returnValueBinding.SetBinding(WorkflowParameterBinding.ValueProperty, ((ActivityBind)(activitybind1)));
this.doWorkReceiveActivity.ParameterBindings.Add(returnValueBinding);

Creating Service Bindings and Endpoints

For a client to connect to a workflow service, an endpoint needs to be configured using address, binding, and contract type information. This can be accomplished using an application configuration file. In addition to the endpoint information, specific behaviors that a service exhibits can also be used. For the contract used in this topic, the App.config file shown below will allow clients to connect to the service to exchange information as well as download contract information about the service itself.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="Microsoft.WorkflowServices.Samples.ContractFirstServiceWorkflow" behaviorConfiguration="ServiceBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="https://localhost:8888/ContractFirstService.svc" />
          </baseAddresses>
        </host>
        <endpoint address ="" binding="wsHttpContextBinding" contract="Microsoft.WorkflowServices.Samples.IContractFirstService" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true" />
          <serviceCredentials>
            <windowsAuthentication
                allowAnonymousLogons="false"
                includeWindowsGroups="true" />
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

There are certain advanced scenarios in which the ReceiveActivity activity can be used. In order to look at For more information, see Conversations with Workflow Services.

See Also

Reference

ReceiveActivity
ServiceContractAttribute
OperationContractAttribute
TypedOperationInfo
ActivityBind
WorkflowParameterBinding
ParameterBindings

Concepts

Workflow Service Authoring Styles