Sie sind auf Seite 1von 14

MSMQ Activation for WCF Workflow Service Application

Summary: This article describes how to set up MSMQ activation for WCF Workflow Service Application
hosted in Windows Activation Service (WAS). This page is specific to
Microsoft Visual Studio
2008/.NET Framework 4
Introduction Beta 2.

In .NET Framework 4 and Visual Studio 2010, you can build pure Windows Communication Foundation
(WCF) services, pure Workflow Foundation (WF) applications, or a workflow service, which is a workflow
application exposed as a WCF service. The out-of-box messaging activities, e.g. Send and Receive
activities, come with implicit WCF endpoints, which can be employed for external communication to the
workflow, including WCF activation of the workflow instance when the first message arrives.

A workflow service, like any application with a communication component, can benefit from the
advantages of queuing technologies. Adding a queue element to application design can improve
robustness. The queue can also provide load-balancing support and improve scalability.

Microsoft Messaging Queue (MSMQ) provides easy-to-use, high throughput message queuing
capabilities that send and receive messages between applications that run on separate servers or
processes. MSMQ implements a queue, which acts as a temporary storage for messages to go through.
Due to the nature of its design, the canonical scenario for MSMQ is the asynchronous messaging pattern.
For example, the recipient of the message becomes unavailable, due to a technical issue or during
scheduled off hours, the incoming messages will be stored in the in-memory queue on the MSMQ server.
Once the recipient becomes available, the messages will be sent to the recipient from the queue in the
FIFO order or priority-based order. This enables reliable communication between heterogeneous
networks and between computers that may not always be connected.

MSMQ by default stores messages in memory, thus, it provides near real-time message exchange
throughput. The server can crash and messages can be lost, therefore the default setting is good for
scenarios that do not need guarantee of reliability. You can choose to store messages on disk by turning
on recoverable messaging. This gives you improved reliability, in exchange of performance.

MSMQ also supports transactions, either internal transactions using MessageQueueTransaction class, or
external transactions with Distributed Transaction Coordinator (DTC). This ensures that messages are
received exactly once, in order. If there is an error occurred, the transaction scope is rolled back.
Transactional queues are slower due to the transaction design and disk-access for queues.

In this article, for simplicity, we will use non-transactional in-memory MSMQ to enable reliable
asynchronous communication for a .NET 4 workflow service.

Prerequisites
Before you go through the following example, please check the following prerequisites.
- Microsoft Visual Studio 2010 (Beta 2) - Download
- Microsoft .NET Framework 4 (Beta 2) - Download
- Windows Features
o IIS 7.0/WAS enabled (http://learn.iis.net/page.aspx/29/installing-iis-70-on-windows-
server-2008/)
o MSMQ installed (http://msdn2.microsoft.com/en-us/library/aa967729.aspx)
- Windows Services
o Net.Msmq Listener Adapter service started

The screenshots of this article are taken from Windows 7 Enterprise environment.

Step-by-Step Instructions

Create a WCF Workflow Service Application Project


1. In Visual Studio 2010, create a new project using the WCF Workflow Service Application
template.
a. For the purpose of this exercise, name the project MyWFService.
Creating a new project using WCF Workflow Service Application template.

2. By default, the template creates a Sequential Service, which contains a pair of Receive and Send
activities called ReceiveRequest and SendResponse. This is a .NET 4 workflow service, which
contains a workflow with activities, some of which may be messaging activities with implicit
WCF endpoints for communicating with other services or applications.
Default .NET 4 Workflow Service

3. Modify the workflow service so that it implements a one-way communication pattern, by using
a Receive activity only instead of the default request/response two-way communication.
a. Open the service file named Service1.xamlx and remove the Send activity named
SendResponse. Save the project.
After the SendResponse activity is removed, the workflow service has one activity
ReceiveRequest wrapped in the Sequential Service.

4. Change the project Web settings to Local IIS Web Server to enable WAS hosting for the new
service
a. Right click on the project and select Properties
b. In Web tab, select the radio button for Use Local IIS Web Server.
Toggle from Visual Studio Development Server to Local IIS Web server

c. Click the button Create Virtual Directory.

Create a queue in MSMQ


1. Open the MSMQ administration console:
a. For Windows Server 2008, right-click on Computer and select Manage and Server
Manager window will appear.
b. Under Feature, expand Message Queuing and locate Private Queues.
MSMQ administration console
2. Create a new private queue
a. Right click on Private Queues and select New -> Private Queue.
IMPORTANT: When a message is sent to the service endpoint with net.msmq protocol,
MSMQ matches the queue name with the [app name]/[service name] pattern to identify
the correct service to activate. Therefore, when the queue is created, the name must be
in the format of [app name]/[service name]. In this example, the queue name must be
MyWFService/Service1.xamlx see screenshot.

Creating a new private queue

b. MSMQ supports transactional messaging scenarios. To keep this example simple, the
scenario is non-transactional, so leave the Transactional checkbox empty.
c. Click OK to create the queue. Expand Private Queue to confirm the new queue is
created.
New queue is created with two sub branches: Queue messages and Journal messages.

3. Enable Journal for logging and troubleshooting


a. Right click on the queue, go to Properties.
b. In the General tab, check Enabled checkbox for Journal. This will enable logging of
all messages that have passed through the queue in Journal messages view.

Enable journal for the queue.

4. Grant permission to Network Service


a. By default, the MSMQ activation service runs as Network Service. In the Security tab
of queue properties, add Network Service and allow Full Control.
Change queue permission.

Configure Windows Activation Service (WAS) to support MSMQ protocols


1. Open IIS Manager, expand Sites Default Web Site and identify our service MyWFService

MyWFService application is shown under Default Web Site.


2. Click on MyWFService and click Advanced Settings on the right action pane.
3. Under Behavior, for Enabled Protocols, you should already see http protocol enabled. Add
net.msmq as a protocol. Use comma as the separator and make sure theres no space after the
comma. For example, http,net.msmq
Enable http and net.msmq protocols.
4. Click OK. This will enable MSMQ protocol at the level of MyWFService.

Check if MSMQ services are up and running


1. Open Services.msc from Start Menu -> Run
2. Locate Net.Msmq Listener Adapter and make sure it is started.

Configure the endpoint in the WCF Workflow Service Application


1. Add explicit service endpoints to Web.Config file
a. In Visual Studio, open Web.Config file
b. Inside System.serviceModel tag, add the following

<system.serviceModel>
<services>
<service name="Service1" >
<endpoint
address="net.msmq://localhost/private/MyWFService/Service1.xamlx"
binding="netMsmqBinding"
bindingConfiguration=MyNetMsmqBinding
contract="IService"/>
<endpoint
address=""
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
<bindings>
<netMsmqBinding>
<binding name=MyNetMsmqBinding exactlyOnce=false>
<security mode=None/>
</binding>
</netMsmqBinding>
</bindings>

<behaviors>

2. Check if the service is up and running


a. In browser, go to http://localhost/mywfservice/service1.xamlx
b. You should see the standard service message that starts with You have created a
service
Service is up and running.

c. Please note that the service has two endpoints, one is mex endpoint, the other is msmq
endpoint. Through the web browser, the service exposes metadata via mex endpoint.
This page indicates that the service is up and running.

Verification of MSMQ Activation


1. Open WCF Test Client
a. Start Menu -> All Programs -> Microsoft Visual Studio 2010 -> Visual Studio Tools ->
Visual Studio 2010 Command Prompt
b. In the command prompt, type in WcfTestClient and hit enter
2. Add the service
a. In the WCF Test Client UI, right click on My Service Projects (the root node on the left
pane) and select Add Service
b. Type in the service address: http://localhost/mywfservice/service1.xamlx and click Ok.
c. Wait until the service is added on the left pane of the UI.
d. Please note that this is using the http mex endpoint. The http mex endpoint exposes
metadata for the WCF test client to add the service.
3. Test the service
a. Once the service is added, double click on the operation name. In our case, it is
GetData()
b. On the right pane of the UI, you can fill in an int to submit to the service. Click on (null)
next to int and a drop down list will appear in the cell. Select
System.Nullable<System.Int32> in the drop down list.
c. Expand int and next to Value, type in an integer of your choice to replace 0.

WCF Test Client is used to test the activation of the workflow service. Note that the
service contract indicates NetMsmqBinding.
d. Click Invoke to send the message
e. In Server Manager, locate the queue MyWFService/Service1.xamlx and expand it to
find Journal messages. Double click on Journal messages, you should see one
message.
Reference

Queuing in WCF an MSDN article describing how to use queued communication in WCF 3.5.

MSMQ Activation an MSDN article with sample demonstrating a two-way communication scenario
with WCF 3.5 and MSMQ.

Das könnte Ihnen auch gefallen