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
: ".NET 3.0 kompakt
" (German). As you can imagine, I did not write the chapter on WCF, though ;-) In our book I found the most simple/gentle introduction to WCF and finally was able to set up my sample program within some 20 minutes. I only had to experiment a little for getting the client code to run without resorting to XML, since Christian did not show imperative code for it in the book.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
and off you go. The code should run right out of the box. (Or download it here
.)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
or elsewhere on the web about WCF (e.g. here http://msdn2.microsoft.com/en-us/library/aa480190.aspx
or here http://www.devx.com/dotnet/Article/29414
or here http://bloggingabout.net/blogs/dennis/archive/2006/10/18/WCF-Part-0-_3A00_-Introduction.aspx
), I think it´s in order to give you a quick tour through my code: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