What's New in Windows Communication Foundation (WCF) 4.0 Part- I

Posted by: Mahesh Sabnis , on 9/10/2009, in Category .NET Framework
Views: 56545
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
Give a +1 to this article if you think it was well written. Thanks!
Recommended Articles
Mahesh is having 10 years of experience in IT education and development. He is a Microsoft Certified Trainer (MCT) since 2005 and has conducted various Corporate Training programs for .NET Technologies (all versions). Follow him on twitter @maheshdotnet


Page copy protected against web site content infringement by Copyscape


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..
Comment posted by Bhushan on Wednesday, November 3, 2010 3:39 AM
I want to learn all the distributed technologies prior to wcf and i would like also to know what r the disadavantages in previous distributed technologies with few examples. Please tell me the way or provide some links.
Thanks,
Nagabhushan
Comment posted by Yazid on Thursday, November 4, 2010 9:06 AM
Hello,

Nice article, I downloaded the code and it does not seem to work.
I think the uri is wrong

ServiceHost Host = new ServiceHost(typeof(WCF_NewService.Service),new Uri("http://localhost:7777/Services/Hello") );

Regards
Yazid
Comment posted by Sanjay on Tuesday, August 2, 2011 5:54 AM
Nice article. Thank you
Comment posted by David Martin on Saturday, February 4, 2012 10:40 AM
Nice article and explanation.
Comment posted by randy smith on Friday, March 30, 2012 2:21 PM
CODE DOESN'T WORK!!!!! Would the author please debug first, and then repost?  Is it asking of too much to check your code before posting!????
Comment posted by Irene Lee on Friday, June 1, 2012 12:26 AM
Nice article to illustrate the concept.
There are two typos
In ConsoleHost, missing IService after Hello
ServiceHost Host = new ServiceHost(typeof(WCF_NewService.Service),new Uri("http://localhost:7777/Services/Hello") );
Should be ...
ServiceHost Host = new ServiceHost(typeof(WCF_NewService.Service),new Uri("http://localhost:7777/Services/Hello/IService") );

In Client, NetTcpBinding_IService should be WSHttpBinding_IService
MyRef.ServiceClient Proxy = new WCF_Client.MyRef.ServiceClient("NetTcpBinding_IService");


Comment posted by Irene Lee on Friday, June 1, 2012 12:30 AM
I am new to the WCF,  How do you update Service Reference 'MyRef' in the WCF_Client when you add one more[OperationContract] in IService.cs and implementation of the new [OperationContract] in Service1.cs.  
Any help will be appreciated. Thanks
Comment posted by Nishant on Friday, June 1, 2012 5:37 AM
+1
Comment posted by Pranil Gatate on Thursday, October 4, 2012 7:53 AM
Nice Article Mahesh!
Comment posted by Vallab on Wednesday, July 24, 2013 4:31 AM
Nice Article.

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