WCF: Introduction to WCF

Introduction to WCF
Windows Communication Foundation (Code named Indigo) is a programming platform and runtime system for building, configuring and deploying network-distributed services. It is the latest service oriented technology; Interoperability is the fundamental characteristics of WCF. It is unified programming model provided in .Net Framework 3.0. WCF is a combined features of Web Service, Remoting, MSMQ and COM+. WCF provides a common platform for all .NET communication.
Below figures shows the different technology combined to form WCF.
  1. WCF is interoperable with other services when compared to .Net Remoting,where the client and service have to be .Net.
  2. WCF services provide better reliability and security in compared to ASMX web services.
  3. In WCF, there is no need to make much change in code for implementing the security model and changing the binding. Small changes in the configuration will make your requirements.
  4. WCF has integrated logging mechanism, changing the configuration file settings will provide this functionality. In other technology developer has to write the code.

Development Tools

WCF application can be developed by the Microsoft Visual Studio. Visual studio is available at different edition. You can use Visual Studio 2008 Expression edition for the development.
Visual Studio 2008 SDK 1.1

Microsoft Visual Studio 2008

Microsoft Visual studio 2008 provides new features for WCF compared to Visual Studio 2005. These are the new features added to VS 2008.

1.     Multi-targeting

You can create application in different framework like Framework 2.0, 3.0 and 3.5

2.     Default template is available for WCF

3.     WCF - Test Client tools for testing the WCF service.

Microsoft provides inbuilt application to test the WCF application. This can be done by opening the Visual Studio command prompt and type the wcfClient Serviceurl shows below. This will help the developer to test the service before creating the client application.
4.      WCF services can be debugged now in Visual Studio 2008. Wcfsvchost.exe will do it for you because service will be self hosted when you start debugging.

Difference between WCF and Web service

Web service is a part of WCF. WCF offers much more flexibility and portability to develop a service when comparing to web service. Still we are having more advantages over Web service, following table provides detailed difference between them.
Web Service
It can be hosted in IIS
It can be hosted in IIS, windows activation service, Self-hosting, Windows service
[WebService] attribute has to be added to the class
[ServiceContraact] attribute has to be added to the class
[WebMethod] attribute represents the method exposed to client
[OperationContract] attribute represents the method exposed to client
One-way, Request- Response are the different operations supported in web service
One-Way, Request-Response, Duplex are different type of operations supported in WCF
System.Xml.serialization name space is used for serialization
System.Runtime.Serialization namespace is used for serialization
XML 1.0, MTOM(Message Transmission Optimization Mechanism), DIME, Custom
XML 1.0, MTOM, Binary, Custom
Can be accessed through HTTP, TCP, Custom
Can be accessed through HTTP, TCP, Named pipes, MSMQ,P2P, Custom
Security, Reliable messaging, Transactions

RTF to HTML Convertor

// Very primitive RTF 2 HTML reader
// Converts tiny subset of RTF (from VS IDE) into html.
// Gets input RTF from clipboard.
using System;
using System.Collections.Generic;
using System.Text;using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;
namespace ClipBoard1{
    class Program    {
        static void Main(string[] args)
            Console.WriteLine("Get RTF from the clipboard.");
            IDataObject iData = Clipboard.GetDataObject();
            string[] f = iData.GetFormats();
            string rtf = (string)iData.GetData(DataFormats.Rtf);
            // We assume the colortable and fontable are a standard preset used by VS.            
            // Avoids hassle of parsing them.            
            // Skip past {\colortbl.*;} and to the start of the real data            
            // @todo - regular expression would be good here.
            int i1 = rtf.IndexOf(@"{\colortbl");
            if (i1 <= 0) throw new ArgumentException("Bad input RTF.");
            int i2 = rtf.IndexOf(";}", i1);
            if (i2 <= 0) throw new ArgumentException("Bad input RTF.");
            string data = rtf.Substring(i2 + 2, rtf.Length - (i2 + 2) - 1);
            TextWriter tw = new StreamWriter("out.html");
            Format(tw, data);
        // Default color table used by VS's IDE.
        static string[] m_colorTable = new string[]
            // rrGGbb
            "#000000", // default, starts at index 0
            "#000000", // real color table starts at index 1
        // Escape HTML chars
        static string Escape(string st)
            st = st.Replace("&", "&");
            st = st.Replace("<", "<");
            st = st.Replace(">", ">");
            return st;
        // Convert the RTF data into an HTML stream.
        // This rtf snippet is past the font + color tables, so we're just transfering control words now.
        // Write out HTML to the text writer.
        static void Format(TextWriter tw, string rtf)
            // Example: \fs20 \cf2 using\cf0  System;
            // root --> ('text' '\' ('control word' | 'escaped char'))+
            // 'control word'  --> (alpha)+ (numeric*) space?
            // 'escaped char' = 'x'. Some characters \, {, } are escaped: '\x' --> 'x'
            // @todo - handle embedded groups (begin with '{')
            int idx = 0;
            while (idx < rtf.Length)
                // Get any text up to a '\'.
                Regex r1 = new Regex(@"(.*?)\\", RegexOptions.Singleline | RegexOptions.IgnoreCase);
                Match m = r1.Match(rtf, idx);
                if (m.Length == 0) break;
                // text will be empty if we have adjacent control words
                string stText = m.Groups[1].ToString();
                idx += m.Length;
                // check for RTF escape characters. According to the spec, these are the only escaped chars.
                char chNext = rtf[idx];
                if (chNext == '{' || chNext == '}' || chNext == '\\')
                    // Escaped char
                // Must be a control char. @todo- delimeter includes more than just space, right?
                Regex r2 = new Regex(@"([\{a-z]+)([0-9]*) ", RegexOptions.Singleline | RegexOptions.IgnoreCase);
                m = r2.Match(rtf, idx);
                string stCtrlWord = m.Groups[1].ToString();
                string stCtrlParam = m.Groups[2].ToString();
                if (stCtrlWord == "cf")
                    // Set font color.
                    int iColor = Int32.Parse(stCtrlParam);
                    // close previous span, and start a new one for the given color.
                else if (stCtrlWord == "fs")
                    // Sets font size. ignore
                else if (stCtrlWord == "par")
                    // This is a newline. ignore
                    // @todo- I think the only reason we can ignore this is because the \par in our input are always followed by
                    // a '\r\n' and we're accidentally writing that.
                    throw new ArgumentException("Unrecognized control word '" + stCtrlWord + stCtrlParam + "'after:" + stText);
                idx += m.Length;
            tw.Write(Escape(rtf.Substring(idx))); // rest of string
        } // end Format()