Intellisense Support and Improved Easier Configuration in Windows Communication Foundation (WCF) 4.5

Posted by: Pravinkumar Dabade , on 11/28/2012, in Category Windows Communication Foundation (WCF)
Views: 22308
Abstract: In this WCF 4.5 series, we will see how WCF 4.5 has made configuration much easier in the first part.

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 -

  1. Default Endpoints
  2. Default Behavior Configuration
  3. 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 -

blank-solution

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 -

system-servicemodel

Step 3: Add an interface with the name IHelloService

ihello-service

Step 4: Now import the namespace System.ServiceModel and add the code in our interface as shown below -

interface-code

Step 5: Rename the class with "HelloService" and implement the above interface in our class as shown below -

implement-interface

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:

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 -

wcf-service-host

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 -

servicemodel-configuration

Services -

services-config

Service Name Intellisense Support -

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 -

endpoint-binding

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 - 

contract-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 ]

behavior-config-intellisense

 

Run your host and it should display your console app output as shown below -

console-app-output

 

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

service-reference

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 -

hello-service

Now Paste the base address and click the "GO" button and add a Service Reference with the name HelloServiceProxy as shown below -

client-appconfig

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.

clientappconfig

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 -

proxy-code

Make sure your "Host" is running and run your client. You will see the following output -

running-host

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 -

service-interface-name

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 -

error-list

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.

Conclusion

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

Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+
Further Reading - Articles You May Like!
Author
Pravinkumar, works as a freelance trainer and consultant on Microsoft Technologies. He is having over 10 years of experience in IT and is also a Microsoft Certified Trainer(MCT). He has conducted various corporate trainings on all versions of .NET Technologies including .NET, SharePoint Server, Microsoft SQL Server, Silverlight, ASP.NET, Microsoft PerformancePoint Server 2007 (Monitoring). He is passionate about learning new technologies from Microsoft. You can contact Pravinkumar at dabade[dot]pravinkumar [attherate] gmail[dot]com


Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!

Categories

JOIN OUR COMMUNITY

POPULAR ARTICLES

FREE .NET MAGAZINES

Free DNC .NET Magazine

Tags

JQUERY COOKBOOK

jQuery CookBook