A truely simple example to get started with WCF
A truely simple example to get started with WCF
Recently I needed to set up some simple code to demonstrate WCF (as an alternative to some other means of communication in distributed applications). But when I googled around, I could not find a really, really simple WCF example. Sure, there are lots of WCF introductions, but they all explain a lot of stuff I did not really want to know at that time. Also they most often spread the code across many files and even across languages (C# and XML). And that´s what I really, really hate! When I first try out a new API like WCF I want to have everything needed in one (!) place. I want all that´s required to be the minimum and in my favorite programming language. This way I can focus best on what´s really essential without getting distracted by syntax and unnecessary (but cool) "fluff".Finally I resorted to a book I wrote together with co-author Christian Weyer


Now, to make life easier for you, I present to you the most simple WCF application I can think of in one piece. Copy the pieces into a VS 2005 console project, reference the System.ServiceModel library from .NET 3.0


Although I don´t want to repeat what´s been said in our book or in others like "Programming WCF Services" by Juval Löwy




Service definition
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 using System.ServiceModel;
6
7
8 namespace WCFSimple.Contract
9 {
10 [ServiceContract]
11 public interface IService
12 {
13 [OperationContract]
14 string Ping(string name);
15 }
16 }
Defining a WCF service means defining its functionality in a so called service contract which is given as an interface here. That this interface is a service contract and which of its methods should be published is signified by the attributes.
Service implementation
19 namespace WCFSimple.Server
20 {
21 [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
22 class ServiceImplementation : WCFSimple.Contract.IService
23 {
24 #region IService Members
25
26 public string Ping(string name)
27 {
28 Console.WriteLine("SERVER - Processing Ping('{0}')", name);
29 return "Hello, " + name;
30 }
31
32 #endregion
33 }
See full details: http://weblogs.asp.net/ralfw/archive/2007/04/14/a-truely-simple-example-to-get-started-with-wcf.aspx
Comments