Saturday, March 31, 2012

Node.JS Sample Application On Windows

I am I'm kind of new to  Node.js   world. I took nearly a week reading tutorials and downloading samples to say "Hello World" to Node.Js and to Socket.io. Finally today I have achieved it :). Let me document what I have done here before I forget all the things. Here I'm going to describe the first step running first Node server on windows. I will go through step by step starting from the downloading  Node.js .

Download and Install Node.js
We can download Node.JS server for windows from here . Nothing to configure... Just download and install it. You can check whether you have installed it correctly using command prompt. Type node and press Enter you will get the node prompt.

Host the sample script
This is the sample code for  hello world server.

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

Create new text file and save above code as sample.js

Run Sample Server
Then go to your folder (using command prompt) which has the sample.js file and execute it using node by executing command node sample.js


Access It Using Browser
Open your browser and enter the address we have given in the sample.js file.

That's It !
I will go more detail in to node.js and socket.io in the next post :).

Monday, March 26, 2012

Workflow Foundation 4 State Machine as a WCF Service

State machine workflow which we had in WF3.5 was not supported in WF4 (in .Net Framework4). But it released with .net framework 4 platform update 1 . State machine is very helpful to model long running workflows with higher external interactions. As an example in a long running order processing system where it needs several user inputs and approvals from external users.
There are several examples state machines in MSDN which describe the state machine well. But here I'm going to expose the state machine as a service. My example is a simple order processing system which has some human interaction to approve orders.

In this workflow there are three states(OrderRecived, To be Approved , Finished) and three triggers (Pay,IssueWithOrder,Resolve). We can create new order by giving an orderId and some amount.Then to complete the order we can pay using Pay trigger. Or else if there any issues with the order we can use another WCF call to workflow to change the state(to restrict pay) using IssueWithOrder trigger. The Resolve trigger is used to resolve the issues when they have any issues.

Download Sample Code



To expose the triggers as services what we need to do is to use send receive activities in triggers.
And the other important thing is correlation handling. Because when we need to call a already instantiated workflow instance we should have some identity to call the correct workflow instance. So in my example the correlation handler is the orderId. Because it is unique for an order which is unique for a workflow instance. So I have a state machine level correlation handler which uses the order id to correlate and each and every WCF call the workflow instance will use that variable as the correlation handler.
You can download my sample code here.

Sunday, February 19, 2012

Workflow Foundation 4 App-fabric Tracking Variables

Windows Server AppFabric provides a great set of tools and options to manage, scale and monitor application hosted in IIS. Especially for windows workflow applications AppFabric is a required tool to monitor , control and to scale the work-flows.

AppFabric Contoso HR Sample is a good tutorial to start with WF with AppFabric. In this post I'm going to describe how to add tracking variables to the Appfabric event log. This is a huge requirement when we are dealing with WorkFlows. Because in events written by AppFabrric contains a Guid to identify the workflow instance. But it is better if we can write our own id of the workflow in to the tracked events.

Let' say as an example in a order processing system. In this case the order we want to track the worrkflow instances by the order id. So here is how I did it,

First we need to define new tracking profile in the web config system.serviceModel section.

      
        
          
          
           
            
              
                
                  
                
              
            
            
             
              
                
                  
                
                
                  
                
              

              
                
                  
                
                
                  
                  
                  
                  
                
              

            
            
              
            
            
              
            
            
              
            
          
        

      
    

We can create several queries as we want. It may be queries for all activities or some specific activity. The query with activityName="*" will write orderid to the event log in all activities of the activity while query with activityName="Process New Order" will write several other variables in "Process New Order" activity to the events.
To enable this tracking profile for the service we need to go to the Appofabric configuration of the relevant service. Go to AppFabric dashboard of the service -> Services ->Select the service and click Configure .
And then go to Monitoring -> Configure , From the dropdown menu select our tracking profile (My Tracking Profile)

Then when the workflow is running we can see the tracked events with our our variables in tracked variables section.