WF Tutorial, Part 3: Create and host Workflow services
This tutorial is the third part of Dispatcher Timer Windows Workflow Foundation tutorial series.
<< Create a simple Workflow
This tutorial demonstrates how to create a simple Workflow Service and host it in a Web Application. This sample is using Visual Studio 2013 with .NET 4.5.1. The example simply gets a value as the service parameter, squares it, and returns it to the service caller.
In this tutorial:
Create a Workflow service activity
Create a Workflow service activity
- Open Visual Studio and create a New Class Library.
Remove the Class1.cs which Visual Studio creates by default, from the project.
- Add a new Workflow Activity to the project and name it SquareActivityService.xaml
Note that the following references will be added to the project. These references are necessary for Workflow projects.
- Open the SquareActivityService.xaml and from the Toolbox drag and drop the ReceiveAndSendReply Activity under Messaging group to the designer.
The following activities will be added to the designer.
- Select the Sequence, open the variable tab, and add the following variables:
_number will be used as input to the service and after calculation the result will be stored in _result and will be returning to the service caller.
Drag and drop an Assign activity under Primitives in to the designer between the Receive and SendReplyToReceive activities.
In the To type: _result
In the C# expression box type: _number * _number
- Click on the Receive activity and change the OperationName to “Calculate”.
- In the Receive activity click on the box next to Content. Expand the Parameters and Add a new parameter as below.
In this step we are defining our service parameters. In this case we create a parameter number on type Int32 and assigned it to the _number variable which we have created before. The same way other parameters can be added to the workflow service.
- In the SendReplyToService activity click on the box next Content. Expand the parameter and Add a new parameter as below:
In this case we are defining the data which our service gonna return back to the caller. Here we have added a parameter called result of type Int32 which will be store the data from the variable _result.
- Click on the Receive activity and open the Property Window (Press F4). Modify the properties as below.
- Take note that the Operation Name is “Calculate”. This will be the same name as the operation in WCF. The caller will see this as a method so she can call it from the service proxy.
- Change the ServiceContractName to “ISquareService”. This will be the service contract which is the same as WCF service contract. Make sure to put an “I” before the name as in WCF service contracts are an Interface (just to make our naming standard).
- Check the CanCreateInstance check box. This is to enable the workflow service to be able to return the result through the proxy back to the caller.
- That’s all J
Host the Workflow Service
- In the same solution create a New ASP.NET Empty Web Application and name it “SquareService.Host”.
- Add a reference to SquareService project.
The solution looks like below:
- Open the Web.Config and modifies it as below
<system.serviceModel> <serviceHostingEnvironment multipleSiteBindingsEnabled="true"> <serviceActivations> <add factory="System.ServiceModel.Activities.Activation.WorkflowServiceHostFactory" relativeAddress="./SquareService.svc" service="SquareService.SquareActivityService" /> </serviceActivations> </serviceHostingEnvironment> <services> <service name="SquareActivityService"> <endpoint name="basicHttpWorkflowService" address="" binding="basicHttpBinding" contract="ISquareService" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> <tracking> <profiles></profiles> </tracking> <behaviors> <serviceBehaviors> <behavior name=""> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="true" /> <workflowUnhandledException action="Cancel" /> <dataContractSerializer maxItemsInObjectGraph="2147483647" /> </behavior> </serviceBehaviors> </behaviors> <bindings> <basicHttpBinding> <binding name="basicHttpService"/> </basicHttpBinding> </bindings> </system.serviceModel>
Take note of the highlighted sections:
- The relative address in the service activation is the service address which we can access to the service.
- The service section is the full qualified service name, which the same name as our activity name.
- In the service endpoint section the contract name is the same name we have specified in the Receive activity properties.
Test the Workflow Service
- Set the web application as startup project in the solution and run it (by either press F5, or pressing Ctrl+F5).
- The service will be opened in a browser. Enter the service relative address in the browser.
You will see the service is up and available. You can also look in to the WSDL definition by click on the links provided in the page.
- Open Visual Studio WCF Test Client to test the service. Add the service in the WCF Test Client.
You will see that the Calculate operation of our work flow service is available.
- Double click on Calculate operation to open it. In the Request section enter any number and press the Invoke button. You will see the result of the calculation will be appearing in the Response section.
Take note that the variable name is the same as what we have define in the list of parameters in our Receive activity.
11 thoughts on “WF Tutorial, Part 3: Create and host Workflow services”
I try to create Workflow service and hosted it using given steps but its not work properly because my webconfig error my web config only show following lines,what happen ? and i want to know what is the reason for it.
Hi Mohan, can you provide me with the error in your config file? You should face to any problem if you follow the exact steps.
It is best article for beginner who want to learn Windows workflow.Please create some realistic examples so that beginner and experienced developer can be beneficial.
Lot of Thanks for your Efforts
Hi Hari, you are welcome. I am happy the content is helpful. Even thought it’s been quite a while I haven’t worked with WF, I will try to polish some existing projects I have done and make them presentable in a blog post.
Do you have the project source code, I cannot run on my project,
I am afraid I don’t have have the project at the moment. I will try to look if I can find it.
Please send the source code for me too, I have problems while running the solution. I Want to ask if we change all the web.config or just add code?
I hope you can help me.
Hi, I don’t have its source code anymore, this post is from 2015. If you follow exactly what it said in the series the code should work.
the first error is:
then when I access http://localhost:16001/SquareService.svc ,the error is :
workflowservicehostfactory cannot resolve ‘squareservice.squareactivityservice’ to an activity or service type.
You should’t get any error if you follow the exact steps. I will look through your error and see if I can help.
I try to create Workflow service and hosted it using given steps but it’s not working properly, THE ERROR MESSAGE IS BELOW ” A project With an output type of class library cannot be started directly.
in order to debug this project, add an executable project to this solution with references to the library project. set the executable project as the startup project.” Regards Atif