If you have worked with WCF 3.0/3.5, you might have observed that configuring WCF services is a bit complex. You will have to always remember attaching the behavior and binding configurations explicitly to the respective services and end points.
When Microsoft released WCF 4.0, they came up with the default configuration concept. Some highlights were are as follows -
- Default Endpoints
- Default Behavior Configuration
- Default Binding Configuration
Now in WCF 4.5, Microsoft has added some new features. In this article, we will see how WCF 4.5 has made configuration much easier, adds default settings to run your WCF Service, adds new Intellisense support in configuration, supports Configuration Validation and Tool Tips, provides less client configuration code.
For this demonstration, I am using Windows 7 and Visual Studio 2012.
Let's first create a simple class library and add a reference to System.ServiceModel.dll file in our class library.
Step 1: Create a blank solution as shown below -
Step 2: Add a class library with the name HelloWCFService and add a reference to System.ServiceModel.dll into our class library as shown below -
Step 3: Add an interface with the name IHelloService
Step 4: Now import the namespace System.ServiceModel and add the code in our interface as shown below -
Step 5: Rename the class with "HelloService" and implement the above interface in our class as shown below -
Compile the library and make sure you have error free code.
Step 6: Let's create a host application [Console Application] with the name "Host" in our solution and add a reference of our class library as well as the System.ServiceModel.dll in our host application:
Step 7: Now let's import a namespace System.ServiceModel into our host application and write the code in our Main() function to host our WCF service as shown below -
Configure our service with the Basic Http Endpoint.
Step 8: Add configuration settings into our Host Application as shown below -
When you start adding configuration settings into the Configuration file [App.Config file which comes default with your console application], you will see some nice ToolTip support as shown below -
For System.ServiceModel -
Service Name Intellisense Support -
I have seen during the implementation of <Service name="">,people often make mistakes while writing the name of the service. But now no worries, as we got some neat and nice Intellisense support. Isn't istawesome?
Support for Endpoint Binding Names is as shown below -
So no more mistakes in remembering or typing the Binding names. That's really great.
Now let's take a look at intellisense support while configuring the Contract of Endpoint -
The best part is intellisense for Behavior Configuration as shown below -
[Please make a note that in WCF 4.0, the default behavior will attach the service behavior to the service. This is just a demonstration of the clever Intellisense support in WCF 4.5 ]
Run your host and it should display your console app output as shown below -
Create a Client and Add a Service Host
Now the next step is to create a client and add our service host as below -
Step 9: Add a new console application with the name "Client1" into our existing solution
Step 10: Make sure your server is running and now add a service reference as shown below -
Copy the base address [http://localhost:9090/metadatauri]from the configuration file. Right click the Client1 application and add a Service Reference as shown below -
Now Paste the base address and click the "GO" button and add a Service Reference with the name HelloServiceProxy as shown below -
Step 11: First of all we will look at the change in the client configuration file which is new in WCF 4.5. If you open Client App.Config file, you will see a less code configuration file. Not like earlier one which is very huge, complex and difficult to understand.
If you observe carefully, it looks similar to your server configuration file.
Now let's call our service method by writing some proxy code as shown below -
Make sure your "Host" is running and run your client. You will see the following output -
Now the last part of our WCF Service is the introduction of configuration "Validation". To validate our service configuration, let's follow these steps.
Step 12: Open your "Host" application's configuration file -
This time around, we will change the service name and interface name as shown below -
As soon as you change the name of the service or contract, you will see some nice tooltip support. If you compile your host, you will find the warnings in your "Error List" window as shown below -
Now we have built-in support for configuration validations. Any mistakes we make will be caught by the WCF 4.5 validation mechanism and we can see warnings right away.
In the second part of What's New in WCF 4.5 Part -II, we will see a new way of implementing Task based Asynchronous Programming.
In this article we have seen a couple of changes introduced in WCF 4.5 like, the Intellisense support, Less Client Configuration Code and Validation of Configuration while you configure the WCF Services
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!