Create new account I forgot my password    

What's New in Windows Communication Foundation (WCF) 4.0 Part- I
Rating: 3 user(s) have rated this article Average rating: 5.0
Posted by: Mahesh Sabnis , on 9/10/2009, in category ".NET 4.0"
Views: this article has been read 8496 times
Abstract: VS 2010 and .NET 4.0 has come out with many new features. Typically there are a lot of changes in Windows Workflow 4.0 (WF 4.0), Windows Presentation Foundation (WPF 4.0) and Windows Communication Foundation (WCF 4.0). In this article we will see some new features of WCF 4.0.

What's New In Windows Communication Foundation (WCF) 4.0 Part- I
 
VS 2010 and .NET 4.0 has come out with many new features. Typically there are a lot of changes in Windows Workflow 4.0 (WF 4.0), Windows Presentation Foundation (WPF 4.0) and Windows Communication Foundation (WCF 4.0). In this article we will see some new features of WCF 4.0.
Following are some of the new features provided with WCF 4.0:
- Simple Configuration
- Serialization Enhancements
-  Web Programming
- Service Discovery
- Router Service
- Workflow Service
Note: This article is based on the Beta 1 release of .NET 4.0 and can change in future.
Simple Configuration:
In WCF 3.x, when we design the host for the WCF service, we need to write the configuration file with Endpoints, Behavior etc. In WCF 4.0, new configuration defaults for default endpoints, binding and behavior are provided. In the following WCF project, we will explore this Simple Configuration feature in WCF 4.0.
Step 1: Open VS 2010 and create a new blank solution ‘WCF40_NewFeatures’. In this solution add a new WCF library project as shown below:
AddNewProject
Step 2: In the WCF library project, delete ‘App.Config’ file. Rename ‘IService1.cs’ to ‘Iservice.cs’. The code of the ‘IService.cs’ as below:
C#
[ServiceContract]
public interface IService
{
    [OperationContract]
    string GetData(int value);
}
VB.NET
<ServiceContract> _
Public Interface IService
      <OperationContract> _
      Function GetData(ByVal value As Integer) As String
End Interface
Step 3: The code in Service1.cs/ Service1.vb is as shown below:
C#
public class Service : IService
{
    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }
}
VB.NET
Public Class Service
      Implements IService
      Public Function GetData(ByVal value As Integer) As String
            Return String.Format("You entered: {0}", value)
      End Function
End Class
Step 4: In the solution, add a new console application and name it as ‘WCF_ConsoleHost’ as shown below:
AddNewProject_1
Step 5: To this project, add a reference to ‘System.ServiceModel’ and ‘WCF_NewService’. In Program.cs add the following code:
Note: Select version 4.0 for System.ServiceModel
C#
static void Main(string[] args)
{
ServiceHost Host = new ServiceHost(typeof(WCF_NewService.Service),new Uri("http://localhost:7777/Services/Hello") );
            Host.Open();
            Console.WriteLine("Started....");  
            Console.ReadLine();
            Host.Close();
            Console.ReadLine();
}
VB.NET
Shared Sub Main(ByVal args() As String)
Dim Host As New ServiceHost(GetType(WCF_NewService.Service),New Uri("http://localhost:7777/Services/Hello"))
                  Host.Open()
                  Console.WriteLine("Started....")
                  Console.ReadLine()
                  Host.Close()
                  Console.ReadLine()
End Sub
Step 6: In the host application, add an App.Config file. Add the following configuration in the ‘App.Config’ file:
 
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled ="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
 </system.serviceModel>
</configuration>
If you have worked with WCF 3.x configuration, you will find a huge difference in the above configuration file when compared to the configuration file of WCF 3.5.
The ‘ServiceBehavior’ specifies that metadata will be published from the service.
Step 7: Now put breakpoint on ‘Host.Open()’ and run the host application. When in the debug mode, on moving your mouse cursor on the ‘Host’ object, you will find the following default selection for binding as shown below:
Binding
The above picture clearly shows that ‘BasicHttpBinding’ is used by default. But now what if one of the secure bindings like ‘WSHttpBinding’ is to be used in the host. In this case, you will need to modify the ‘App.Config’. For using the new binding, make the following changes:
 
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled ="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="wsHttpBinding" scheme ="http"/>
    </protocolMapping>
 </system.serviceModel>
</configuration>
 
Here ProtocolMapping is used to set the mapping between Uri WCF binding and the transport protocol.
Step 8: Run the host application again and see the debug screenshot as shown below:
Debug
Here it shows that since protocol mapping is set to ‘wsHttpBinding’ and the scheme is set to ‘http’, it uses the binding ‘wsHttpBinding’.
Conclusion: WCF 4.0 has reduced the pain of WCF developers  by providing default configurations for hosting WCF service. It is now possible to host services free of any configurations.
In the next parts we will see the other new features of WCF 4.0. The entire source code of this article can be downloaded over here







Follow me on twitter

Page copy protected against web site content infringement by Copyscape


How would you rate this article?

User Feedback
Comment posted by Jyoti Ghadge on Saturday, September 12, 2009 4:57 AM
Gr8 article
Comment posted by Anup Hosur on Monday, September 14, 2009 1:00 PM
Great post.. As we compare with 3.5 framework.. It reduces lot of work for us.. Actually i need some more articles on wcf 4.0..

Post your comment
Name:
E-mail: (Will not be displayed)
Comment:
Insert Cancel