What's New in Windows Communication Foundation (WCF) 4.0 Part- I
Posted by: Mahesh Sabnis ,
on 9/10/2009,
in
Category .NET Framework
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:
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:
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:
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:
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
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
Mahesh Sabnis is a DotNetCurry author and a Microsoft MVP having over two decades 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), and Front-end technologies like Angular and React. Follow him on twitter @
maheshdotnet or connect with him on
LinkedIn