Thursday, February 5, 2009

My First WCF Experiment - In VS 2005

One Fine day, one friend of mine asked --' Hey so what do you know about WCF'..... Ehh I replied Yeah Why Not , I certainly know.... "World Computer Foundation"... And he quietly went away saying anything.

Funny thing apart, I felt somewhat shameful in this and a 2 year lag from other techies and finally determined to research what is this stuff.

Made a search in the web to found out What is WCF ? How this works? How to run a sample demo?

I am going to share my first WCF experience of creating the first WCF service with you.... So that someone else can benefit from this......

I used my system with Visual Studio 2005 enterprise edition installed for this. Please see below the step by step process I did:---

1. Started ... OOps where is my .NET 3.0. Lesson1 -> You need Microsoft.NET 3.0 or higher to work with WCF. You can download that easily from
http://www.microsoft.com/downloads/details.aspx?familyid=10cc340b-f857-4a14-83f5-25634c3bf043&displaylang=en

2. We can start building the WCF service first. Let us call it by the name MyFirstWCFService.

3. Start VS 2005 and Create new ASP.NET Webservice.. hope u all know this . If you have installed WCF extensions for VS 2005, then You will get a Create new WCF service template inbuilt. But I am creating a new ASP.NET webservice for creating the WCF service.

For installing WCF extensions for VS 2005, I went to Microsoft site, but it was not there- removed and now it comes with .NET 3.5- Outdated hmmm...
At last I found that from an extensive web search and here you go...
Please click here http://rapidshare.com/files/194386807/vsextwfx.msi

4. Now you have created a new ASp.NET web service, first we can remove the service.svc file-- We don't need . It is for way old ASP.NET web service and now we are modern and d0n't need the old damn thing hehe...

5. Add reference to the God, the heart , the soul and all-in-all of a WCF service ---
System.ServiceModel.dll.
If you are using a CTP or RC1 you will probably need to browse to this file. If not it will be listed on the .NET tab. Otherwise browse to the
Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation directory
to find this cutie...

6. Now you have added the dll file, Select the Service.cs underneath the App_Code folder. Wrote the following code.....



7. Save the service.cs file now and now we can add our next file needed Service.svc file . For this there will not be a entry in add new item in project context menu. For this selet a text file form context menu and name it to Service.svc. Now add the following code to this


8. Having saved this we can create our last file needed web.config file. Add a new configuration file from the add new item menu from project menu. and add the following removing the default entries to web config that VS 2005 makes .





9. Now the coding part is over and now we can move on to the deployment part. For this open IIS Admin page - InteMgr.exe and add a new virtual directory named MyFirstWCFService and assign a new folder to hold this virtual directory.


10. Now right click on the VS 2005 ASP.NET webservice project that we created and then click Publish Website giving the virtual directory as the one we set in the step 9. Please select Local IIS tab in the left to get all the virtual directory options in the Publish New Website Wizard.

11. Hurrah and we have finished and deployed our first WCF service and we are going to run it. Open any browser and type


http://localhost/MyFirstWCFService/Service.svc

Ohh NOOO!!! IT just displays the code that is in the Service.svc file. Damn... Started the research in google and at last found the solution to this. Download the following file


This is a magic file named CleanIISScriptMaps.exe. run this in command prompt and it just does the trick for you!!!!!.

Now having run this run the url again and yes ... My first WCF service have started working showing me the WSDL associated with the service. I am top of the world!!!!!!!!!!

12. Now I need to test the service I have created. For this create a sample windows application or console application . Add the soul and heart - yes System.ServiceModel.dll to that project. Then add "Service Reference"== this is a new term ... you will get this menu item when you install the WCF extension for VS 2005 as specified in step 3. In the service reference add the service URL as


http://localhost/MyFirstWCFService/Service.svc?WSDL

Give the name as "WCFServiceDemo". Now click Ok to create the service reference.

13. Step 12 will run the SVCUtil in the background for creating the client and we can use it to call the service.

14. Now in any event - if sample windows application create a button and label and then add the following code to the button click event.

WCFServiceDemo.TestWCFService client = new
WCFServiceDemo.TestWCFService ();
lbl.Text = client.MyFirstFunction(“Your Name”);

Intellisense will help you write this code.:-).

Now click the button and yes you can see your first virgin WCF result in the client.......

Hello: "Your Name"

This completes the task of creating the first WCF project. For specific terms and other doubts please read the WCF articles in MSDN or read some good Books..............

Will be back soon with my first WPF and Silverlight experience ...... Wait for that guys!!!!

No comments:

Post a Comment