Click here to Skip to main content
Click here to Skip to main content

Simple Web Service using WCF (Windows Communication Framework)

By , 2 Jan 2007
 

Web Service running

Introduction

This article shows you how easy it is to setup a web service using WCF (Windows Communication Framework). Using the source I've provided as a template, you can quickly create your own web services. This article does not deal with a client consuming the web service (you will have to wait until part 2).

Background

I've been playing around with WCF for a while, since the early CTP's. I found it difficult to find good samples/examples. Either they didn't work or I was either too lazy or too stupid to get them working. All I wanted was something that worked with very little effort (or understanding) on my part. I just wanted something I could install and get running!!!

Of course at some point everyone will have to understand the ABC's (but that can wait for another day). When you start to learn a new technology, especially a Beta or CPT - you just want it to work, figuring out how it works can wait for another day.....

So I put together a simple WCF Web service, that you can just download and get running in a few minutes (for lazy developers - like myself!)

Getting Started

You are going to need Visual Studio 2005 (it might work with other versions of Visual Studio, but I've not tested in and I'm not going to!!), and .NET 3.0 (I would get the entire package from here instead).

Then just download the source from above.

Using the code

Download the example code and open up the solution in Visual Studio 2005.

The are two projects, the Web service and the Implementation of the class. [Fig 1]

Fig 1 - showing the 2 projects

I have chosen to use the dev web server that is built into Visual Studio, it's just easier, less setup and mucking around. But there is no reason not to use IIS (if you know how to). When your code goes into production you will be using IIS, but for now I'm going to leave it alone.

There are two parts to the web service, the .svc file and the web.config.

WCFService.svc

<% @ServiceHost Service="WCFSample.EchoImplementation"%>

Web.Config

<system.serviceModel>
    <services>
        <service name="WCFSample.EchoImplementation" 
            behaviorConfiguration="returnFaults">
        <endpoint contract="WCFSample.IEchoContract" 
            binding="basicHttpBinding"/>
        <endpoint contract="IMetadataExchange" 
            binding="mexHttpBinding" address="mex">
        </endpoint>
    </service>
</services>

The Service from the ServiceHost attribute in the WCFService.svc file should match one of the service names in the web.config. The service name and endpoint contract should both match the implementation and contracts from the WCF Project Template.

The WCF Project Template is made up of 3 parts, the contract [data contract or message], the implementation and the interface [ServiceContract] (yes, the ABC's had to come in somewhere).

The Service Contract

The WCFContract.cs contains the interface for this service.

[ServiceContract]
interface IEchoContract
{
    [OperationContract]
    EchoMessage Echo(EchoMessage Message);
}   

The Data Contract

The Message which gets sent around is contained in the WCFContract.cs

[DataContract]
public class EchoMessage
{
    private string _OutMessage;
    private string _ReturnMessage;

    [DataMember]
    public string OutMessage
    {
        get { return _OutMessage; }
        set { _OutMessage = value; }
    }

    [DataMember]
    public string ReturnMessage
    {
        get { return _ReturnMessage; }
        set { _ReturnMessage = value; }
    }
}   

The Implementation

The implementation of the web service is in WCFImplementation.cs

class EchoImplementation : IEchoContract
{
    public EchoMessage Echo(EchoMessage Message)
    {
        EchoMessage _returningMessage = new EchoMessage();

        _returningMessage.ReturnMessage = Message.OutMessage;

        return _returningMessage;
    }
}

For this example, I used the EchoMessage to pass the data between the client and the web service, but this could be any class that has [DataContract] as an attribute.

Your Go...

Now it's your turn. The code sample is a very simple service, but the structure can be copied for other web services, the example can be scaled out so there is a one to many or many to many relation between the .svc files and classes.

History

  • 1st Jan 2007 - Released Initial version

References

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Mark Pryce-Maher
Web Developer
United Kingdom United Kingdom
Member
I've been a programmer for longer than I care to remember. It all started with those BBC (model B's) at school, it all went down hill from there.....
 
This year, I am mostly coding in.. C# (WPF & WCF)
 
Blog Address http://markpm.blogspot.com
 

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberPatrick Harris1 Nov '12 - 5:52 
Excellent. Where's part 2?
 
Thanks.
GeneralMy vote of 5memberSChristmas11 Jun '12 - 3:41 
How to consume this WCF service. waiting for second article.
GeneralMy vote of 5memberPrasanta_Prince13 Sep '11 - 20:34 
Good One. 5 for that.
GeneralWhat Starts It Up? How Do You See It Working?memberhively4 May '11 - 8:12 
So, after building the code.... what next?
 
How do I test the code? What are the specific steps to make it do something?
GeneralGood Jobmemberjustinonday23 Feb '11 - 19:37 
Nice work ,expecting more from you like Web service Rest.....
GeneralBuilding a web service client with WCFmemberShameer Kunjumohamed17 Oct '10 - 5:54 
Sharing a post on building a web service client with WCF, at http://justcompiled.blogspot.com/2010/10/building-web-service-client-with-wcf.html[^]
Shameer

GeneralExcellent job! thx a lot!memberred_artikel8 Jul '10 - 11:36 
Wink | ;)
GeneralIncorrect file in articlememberhopingToCode27 May '09 - 4:34 
in the article you state
 
The WCFContract.cs contains the interface for this service.
 
When in fact this is in WCFInterface.cs
GeneralService WCF over C++ Clientmemberchemita_cpu6 Sep '07 - 0:49 
Is posible!!!!!OMG | :OMG:
 
One word witch two different miniuns funy ingles

Generaldbml & WCFmemberDogu Tumerdem13 Aug '07 - 4:46 
hi,
 
I am trying to write a wcf service function that return a dbml generated class. The result is that class has no data contract attribute. And dbml classes has no such a property. when edit the designer.cs manually it works fine. but first save for dbml file replaces that attributes from designer.cs. Are there any method to do that ?Confused | :confused:
GeneralRe: dbml & WCFmemberMark Pryce-Maher13 Aug '07 - 4:55 
interesting.... I'm not sure I can be any (real) help here.
 
How about having a (seperate/generic) wrapper class for the dbml, which contains the data contact info etc - this class would then expose the dbml class.
 
You could then update the dbml class without effecting your project or having to re-edit the dbml class.
 


GeneralRe: dbml & WCFmemberDogu Tumerdem13 Aug '07 - 5:37 
http://linqinaction.net/blogs/jwooley/archive/2007/05/14/wcf-with-the-linq-to-sql-designer.aspx[^]
GeneralSimplicity is BeautifulmemberDewey29 Jul '07 - 15:31 
Thanks for the simple example.
 
Not only did tis show me the way to do Webservices using WCF, it showed me how to convert some of my existing Webservices to WCF.
 
Thanks...
GeneralRe: Simplicity is BeautifulmemberMark Pryce-Maher7 Aug '07 - 21:18 
Thanks Big Grin | :-D
QuestionFail to make it workmemberniro@averna.com24 Jul '07 - 8:41 
The example is pretty sample but I was unable to make it work. First of all, shouldn't I have to share a Web virtual folder...
GeneralRe: Fail to make it workmemberMark Pryce-Maher7 Aug '07 - 21:17 
Firstly, Sorry for the late reply.
 
The website project should do all the setting up of virtual directories for you.
 
I'm not sure what the problem could be, how about you send me screen shots or the error messages you are getting.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 2 Jan 2007
Article Copyright 2007 by Mark Pryce-Maher
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid