Home > WCF > WCF # 2 – Implementation Of a Web Service (Writing/Implementing a WCF Service Entirely in Code)

WCF # 2 – Implementation Of a Web Service (Writing/Implementing a WCF Service Entirely in Code)

Hello Geeks,

In this article we will see how to implement a simple Web Service using WCF.

This section describes how to implement a simple service with WCF. By
simple, we’ll assume HTTP as the wire protocol, and we’ll assume a text based
representation of an XML document on the wire. For security, we’ll
assume it’s handled somehow in the application. We’ll assume a synchronous
request-reply conversation and that our service supports just one
operation, which takes a string as input and returns a double as output

To implement a service remember the Endpoint ABCs , In this section we will see the implementation of web service in two types

** Using Completely in code

** Using Configuration Files

Today we will see Writing a WCF Service Entirely in Code

* First we will implement the basic feature or functionality that we want to serve.

then we will think on hosting that code in an operating system process, and that process listens for requests and responds.


STEPS to implement a simple web service through code :-

1) Define a Data Contract

Write a .NET class that does something useful and decorate it with WCF attributes. The

* [ServiceContract]

This  attribute marks a class as a contract. Expressed in standards-based WSDL, the it defines a port type.

* [OperationContract]

This attribute defines methods that can be invoked on the class through the
service interface.It also defines the messages that are passed to and from those class methods.

2) Define an endpoint.

Within the endpoint definition, we’ll specify an address, binding, and contract by using the AddServiceEndpoint method on the ServiceHost class. The address is blank, which indicates
that the address of the endpoint is the same as the base address or the service.

The binding is basicHttpBinding which WS –I supportable.

3) Host the service in a process so it is listening for incoming requests..

Using following code we can host the service in a console application
by using the ServiceHost class. The service listens at http://localhost:8000/EssentialWCF.


To Make the following code run
1)IIS must be there on your machine
2)you have to take the CONSOLE APPLICATION
    and then need to add System.ServiceModel reference to it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace WCFusingCode
class Program
public interface IStockService
double GetPrice(string ticker);

        public class StockService : IStockService
public double GetPrice(string ticker)
{ return 9.99; }

        static void Main(string[] args)
ServiceHost servicehost = new ServiceHost(typeof(StockService), 
new Uri("http://localhost:8000/WCFusingCode"));
new BasicHttpBinding(), "");
Console.WriteLine("Press Enere to to terminate");


In next blog we will see Implementation using configuration files.


Categories: WCF
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: