How to Invoke a WCF Service from a Workflow?

Before you can invoke methods on the service, you must add a service reference to your project. To do this, right-click the workflow project node, and then click Add Service Reference. Use the Add Service Reference dialog box to retrieve the service information and add the reference to the project.

When you add a service reference to a workflow project, Visual Studio generates an application configuration file that is named App.config that stores the endpoint and communication behavior properties of the service. A service endpoint provides client access to the WCF service and contains an address for the service, the communication binding, and a contract that defines the available methods.

To invoke a WCF operation from a workflow:

  1. Add a reference to the WCF service in the workflow project.

  2. Add a SendActivity activity to the workflow.

  3. Set the ServiceOperationInfo property to an operation on the WCF service.

  4. Create a ChannelToken object to specify the endpoint and owner activity. When you specify the ChannelToken property, you specify the EndpointAddress subproperty, which can be found in the application configuration file.

Note: The service must be running when you create the service reference, so that the service is available to return the .wsdl document. The .wsdl document is the Web Services Description Language (WSDL) representation of the service, which Visual Studio requires to generate the proxy object.

SendActivity contains several properties that you can use when you implement functionality in your workflow. OwnerActivityName is the activity to which the workflow runtime will pass any response messages and it is usually SendActivity that calls the service. You can use the BeforeSend and AfterResponse events to execute functionality before or after the service method is called.

For more information about the SendActivity activity, see SendActivity Class