WF4 is one part of .NET Framework 4.0, which means WF4 workflow can be hosted and run in any type of application running with the .NET framework. We can host a workflow as a WCF service. We can also invoke a workflow service from a workflow or host workflow in an ASP.NET application and handle all the business logic behind the page.
When we design workflow applications, please let workflow be workflow. Don’t couple workflow with other logic. For example, in this chapter, hosting workflow in ASP.NET is for conception demonstration only, not the best practice. In the real world, most of the time, workflow should be implemented as a workflow service hosted in IIS7 or AppFabric.
AppFabric is an IIS7 extension that includes many tools to help us host a workflow service. AppFabric is to workflow service like IIS7 is to ASP.NET website. However, we can run a workflow service in IIS7 without AppFabric installed. Although AppFabric is powerful, we need to spend some time to learn it. For more information about AppFabric, you can check this link: https://www.microsoft.com/en-us/download/details.aspx?id=27115
Hosting a workflow service in IIS7
The process of sending an e-mail would consume some time – maybe a few seconds or even minutes. It would be a waste of time and resources for our applications to stop and wait for an e-mail sending action to complete. Because sending e-mail is time-consuming, a better design is to strip this feature out as an independent WCF workflow service and host that service in IIS7.
We need the SendEmailActivity activity to send an e-mail.
How to do it…
- Create a WCF workflow service application:
Create a WCF workflow service application and name it
- Add SendEmailActivity to the toolbox:
In the Toolbox tab, right-click and select Choose Items. In the opening dialog, click Browse and navigate to the ActivityLibrary.dll
- Create a SendEmail workflow service:
- Delete Service1.xamlx, which is created by default, and add a new WCF workflow service to the project. Name it SendEmailService.xamlx. Drag a TransactedReceiveScope activity to the design panel, click the Variables button, and create a variable named emailMessage:
- Drag a Receive activity to the Request box of TransactedReceiveScope. Set the OperationName to SendEmail. Click the Content Definition link to create a parameter as shown here:
- Assign ISendEmailService to the ServiceContractName property. Check the CanCreateInstance property.
- Next, drag SendEmailActivity to the body of TransactedReceiveScope.
- Assign the following properties to SendEmailActivity:
The final workflow will look as shown in the following screenshot:
- Create a website in IIS7 for this WF service:
In IIS7 Manager Console, create a website and assign the website’s physical path to the project folder of HostingWorkflowServiceInIIS7. Assign it a new port number. By default, an ASP.NET application will run under the built-in network service account (or ApplicationPoolIdentity in IIS7.5). This account has the most limited permissions. For testing, we can shift the application pool’s identity to an administrator account.
- Use WCFTestClient.exe to test the WCF service:
Usually, we can find the WCFTestClient.exe tool in C:Program Files (x86)Microsoft Visual Studio 10.0Common7IDE.
We just need to open our mail. A new mail with subject Hello WF Service indicates that we have created and hosted the WF service successfully.
Click OK. We will find SendEmailActivity in the toolbox:
We should be able to find the following module and handlers in IIS7:
If we cannot, then we should reinstall .NET framework 4.0 or repair it. Here are the repair commands:
Repair command for 32-bit:
.NET Framework 4 Full (32-bit) – silent repair %windir%Microsoft.NETFrameworkv4.0.30319SetupCacheClient setup.exe /repair /x86 /x64 /ia64 /parameterfolder Client /q /norestart
Repair command for 64-bit:
.NET Framework 4 Full (64-bit) – silent repair %windir%Microsoft.NETFramework64v4.0.30319SetupCacheClientsetup.exe /repair /x86 /x64 /ia64 /parameterfolder Client /q /norestart
How it works…
Simply put, once we have set up the IIS7, we need to copy all the workflow service project files and folders to the IIS application folder and the workflow service will just work.
We can also host a WF4 service in IIS6 once we have installed .NET framework 4.0. Running a WF4 service in IIS6 is not recommended.