Wednesday, June 20, 2012

Web services vs WCF (Windows Communication Foundation) services

When we are talking about WCF and Web Services, are we talking about the same thing? The answer is NO. Today I will be explain the differences between these two and the advantages that WCF has over Web services.

ASP.NET provides .NET Framework class libraries and tools for building Web services, as well as facilities for hosting these services within Internet Information Services (IIS) while WCF provides .NET Framework class libraries, tools and hosting facilities for enabling software entities to communicate using any protocols, including those used by Web services.

WCF supports more protocols for transporting messages than Web services. Web services only support sending messages using the HTTP. WCF supports sending messages using HTTP, as well as the TCP, Named Pipes, and Microsoft Message Queuing (MSMQ). Most importantly, WCF can be extended to support additional transport protocols.

Since Web Services use standard XML, they are platform-independent and language-independent. They don't care about the consuming application as long as they are calling the Web services over HTTP. WCF can be hosted only under the Windows environment, but it can be consumed by clients of different languages and different platforms.

Another significant thing is a WCF service can have one or more WCF endpoints, so one address for each end point. But a Web service can have only one address at one point at a time.

For more information,
   Migrating ASP.NET Web Services to WCF
   Multiple endpoints -WCF

Happy Coding.

Regards,
Jaliya

Sunday, June 3, 2012

Asynchronous Programming and Callback Methods with Delegates in C#

My previous post was about Delegates in C# and today I thought of writing about one nice technique which is available with Delegates, and that is "Asynchronous Programming". In one of my previous posts, I wrote about Asynchronous Programming with C# 5.0 and that is with "async" modifier and "await" operator. 

Using Delegates we can call a synchronous method in an asynchronous manner and Callback methods are used to notify the caller, when an asynchronous work is completed. First let's get an idea on how these synchronous and asynchronous methods works.

When we call a synchronous method it will get executed on the same thread as the caller. But what happens here is, caller thread gets blocked while the called method is active. When we call a method in an asynchronous way, the .NET framework obtains a thread from the thread pool for the method invocation and delivers the in parameters passed by the calling code. The asynchronous thread can then run the method in parallel to the calling thread. Here what happens is caller thread is not blocked. The call returns immediately to the caller. If the asynchronous method returns some value, the calling thread must be able to handle it. The .NET asynchronous feature supports two mechanisms: that calling thread can either ask for the results, or the asynchronous method can deliver the results to the calling thread when the results are ready.

Delegate's do this asynchronous call invocation using delegate's BeginInvoke method.
First we have to define a delegate with the same signature as the method we want to call. The common language runtime automatically defines BeginInvoke and EndInvoke methods for this delegate, with the appropriate signatures. If a callback method has been specified in the call to the BeginInvoke method, the callback method is called when the target method ends. In the callback method, the EndInvoke method obtains the return value and any input/output or output-only parameters. If no callback method is specified when calling BeginInvoke, EndInvoke can be called from the thread that called BeginInvoke.

I think it's better if we go by examples.

Let's take the following method under the class i have created named "MyClass".
public class MyClass
{
    public string MyMethod(string s)
    {
        Thread.Sleep(5000);
        return "Hello" + s;
    }
}
I have a public method, which will return a string and accepts a string. Then I am creating a delegate for this method.
public delegate string MyDelegate(string s);
Then I am creating my delegate object.
MyClass oMyClass = new MyClass();
MyDelegate oMyDelegate = new MyDelegate(oMyClass.MyMethod);
Now I am all done for calling the method asynchronously. As I have told you, the CLR automatically defines a BeginInvoke method for this delegate object which would have a signature like this.
IAsyncResult oMyDelegate.BeginInvoke(string s, AsyncCallback callback, Object @object);
In here, the first parameter is the methods parameter. The second parameter is an AsyncCallback delegate that references a method to be called when the asynchronous call completes. The third parameter is a user-defined object that passes information into the callback method.

Let's take a complete example.
using System;
using System.Threading;

namespace MyDelegateExample
{
    public delegate string MyDelegate(string s);

    class Program
    {
        static void Main(string[] args)
        {
            MyClass oMyClass = new MyClass();
            MyDelegate oMyDelegate = new MyDelegate(oMyClass.MyMethod);
            Console.WriteLine("Going to call the method Asynchronously.");
            oMyDelegate.BeginInvoke("Jaliya", new AsyncCallback(oMyClass.MyCallbackMethod), oMyDelegate);
            Console.WriteLine("Back on Main.");
            Thread.Sleep(15000);
        }
    }

    public class MyClass
    {
        public string MyMethod(string s)
        {
            Thread.Sleep(5000);
            return "Hello " + s;
        }

        // call back method to capture results
        public void MyCallbackMethod(IAsyncResult iar)
        {
            // cast the state object back to the delegate type
            MyDelegate del = (MyDelegate)iar.AsyncState;

            // call EndInvoke on the delegate to get the results
            string result = del.EndInvoke(iar);

            // display the results
            Console.WriteLine("Delegate returned result: {0}", result);
            Console.WriteLine("Asynchronous Call Completed.");
        }
    }
}

Output :

Output
In here what happens is in my BeginInvoke call, I have mentioned my Callback method. My main program will continue running in parallel and when the asynchronous call is completed, it will execute the "MyCallbackMethod". In that method, I have called the EndInvoke method to retrieve the results of the asynchronous call. I have put a thread sleep in my Main to keep the program running. So I can see the output from my asynchronous call.

Let's take this example.
using System;
using System.Threading;

namespace MyDelegateExample
{
    public delegate string MyDelegate(string s);

    class Program
    {
        static void Main(string[] args)
        {
            MyClass oMyClass = new MyClass();
            MyDelegate oMyDelegate = new MyDelegate(oMyClass.MyMethod);
            Console.WriteLine("Going to call the method Asynchronously.");
            IAsyncResult IAsyncResult = oMyDelegate.BeginInvoke("Jaliya", null, null);
            Console.WriteLine("Back on Main.");
            IAsyncResult.AsyncWaitHandle.WaitOne();
            string result = oMyDelegate.EndInvoke(IAsyncResult);
            Console.WriteLine(result);
            Console.ReadLine();        
        }
    }

    public class MyClass
    {
        public string MyMethod(string s)
        {
            Thread.Sleep(5000);
            return "Hello " + s;
        }
    }
}

Output :

Output

The difference here is there is no callback method.
AsyncResult.AsyncWaitHandle.WaitOne();
will hold the Main from executing further until asynchronous call completes. After that completes only it will move executing forward.

I hope you all got a good understanding in how Asynchronous Programming and Callback Methods works with Delegates.

Appreciate your feedback.

Happy Coding.

Regards,
Jaliya