Saturday, June 29, 2013

one two three – to windows communication foundation – Part 1

That was a chilly Saturday night, after having a bottle or two of beer I sat down to code. It was 3 am when I looked up the clock, well I stare it for a while making sure it isn’t stopped. I have watched all the exorcism movies and believe me clock stopping at 3 am, ain’t a good sign. Anyway, I was coding hard. Was stuck in that one code block that had taken a while long. 4 am, as the clock bell count, I ran the solution for one last time to check in case of any flaw, and damn it worked.

I was working on one of my WCF service for a windows 8 application. It was really plausible to see how two works in just the precise symphony. It will take eyes of a programmer to see the elegancy in the code written for both of them, so much lines in their, yet all so damn clear.

In this post, I am scribbling text and code that help understanding WCF easily. This ain’t a book, so we are not going deep into theory, rather we, with both foot, jump to coding.

Establishing the project

In order to create a WCF service, you need to setup the web service project at the first place.
To begin with, open visual studio and create new project

image

Since we are going to start from scratch. Delete the two files IService1.cs (which is an interface) and Service1.svc (which is our service).

image

Now we are going to add our fresh service file, HelloWorldService.

image

Once the service is added, we will have two files, one is our interface IHelloWorldService.cs and another is our service HelloWorldService.svc.
The interface, acts as a contract of our service, meaning, it will describe every chunks of functions and classes that our service possess. Which will be later used to create wsdl document which a client download and read to get the glimpse of what the linked service actually do. Here is the code of the interface.

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Runtime.Serialization;
   5: using System.ServiceModel;
   6: using System.Text;
   7:  
   8: namespace HelloWorld
   9: {
  10:     [ServiceContract]
  11:     public interface IHelloWorldService
  12:     {
  13:         [OperationContract]
  14:         void DoWork();
  15:     }
  16: }

Coding Service

In the code above, we are going to replace the function’s definition void DoWork() to something like string SayHello() as we need our service to return something, and that something must be a string. Which will result in following code block:

   1: namespace HelloWorld
   2: {
   3:     [ServiceContract]
   4:     public interface IHelloWorldService
   5:     {
   6:         [OperationContract]
   7:         string SayHello();
   8:     }
   9: }

Moving to the service part, the HelloWorldServic.svc, delete the function DoWork() and implement the interface again.

image

This will result in creation of a function of return type string. We will return a string Hello World! in this function. Here is the overall code of the service file.

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Runtime.Serialization;
   5: using System.ServiceModel;
   6: using System.Text;
   7:  
   8: namespace HelloWorld
   9: {
  10:     public class HelloWorldService : IHelloWorldService
  11:     {
  12:         public string SayHello()
  13:         {
  14:             return "Hello World!";
  15:         }
  16:     }
  17: }

Build the project and we are done with the service part. This is how we create services, a simple one though. In the next part we will see how to consume the service we just have created.

Happy Reading!!!

one two three – to windows communication foundation – Part 2

Share this post

0 comments

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

 
© 2013 Neelesh Vishwakarma
Posts RSS Comments RSS
Back to top