Building a Simple WCF Service Library

The following months I will be spending most of my study time on the Windows Communication Foundation (WCF). I participated on several projects using WCF in the past few years, mostly consuming services. Let’s crank this knowledge up a notch. I need a basic WCF Service Library for testing purposes.

Here’s what I came up with. I created a solution with both a WCF Service Library and a test project for my services. The ‘Hello World’ of communications for me is the Echo Service; somewhat reassembling the TCP Echo Protocol. A simple service given a EchoMessage returning an echo of that message (3x) to the client.

First I need a datacontract to call and return the EchoService with – EchoMessage.

using System;
using System.Runtime.Serialization;
 
namespace WcfServiceLibrary.Echo
{
    [DataContract]
    public class EchoMessage
    {
        public EchoMessage()
        {
            Created = DateTime.Now;
        }
 
        [DataMember]
        public string Text { get; set; }
 
        [DataMember]
        public DateTime Created { get; set; }
 
        [DataMember]
        public DateTime? Invoked { get; set; }
    }
}

Then I created the IEchoService interface, exposing the actual ServiceContract and operations of the service.

using System.ServiceModel;
 
namespace WcfServiceLibrary.Echo
{
    [ServiceContract]
    public interface IEchoService
    {
        [OperationContract]
        EchoMessage Echo(EchoMessage message);
    }
}

Finally I added the EchoService class implemenation of the IEchoService interface, with some trivial business logic for the Echo operation. I did not add any ServiceBehavior or OperationBehavior to further configure the service, effectively using the default values.

using System;
using System.Linq;
 
namespace WcfServiceLibrary.Echo
{
    public class EchoService : IEchoService
    {
        #region IEchoService Members
 
        public EchoMessage Echo(EchoMessage message)
        {
            message.Invoked = DateTime.Now;
            message.Text = String.Concat(
                Enumerable.Repeat(message.Text, 3));
 
            return message;
        }
 
        #endregion
    }
}

After a successful build and run the WCF Service Host and the WCF test client starts. We can now invoke the Echo operation of the EchoService to send back and forth EchoMessages.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>