WCF Service HelloWorld

WCF Architecture
WCF provides a single programming model that can be used to creat services on the .NET platform for organization
. It is the next version of several  existing products  – ASP.NET’s web methods (ASMX) and microsoft web services enhancements (WSE) fro Microsoft .Net
Contracts :specification that define the interface: (contract- Service contract – Operation Contract- Message Contracts – Data Contract – Fault Contract)
Service runtime – Behavior is a type to extend the functionality of the original type many types of behavior: channel – service – binding – contract – security
Messaging – channels , a WCF service has to be hosted in a application on the server side. On the client side, the client application have to specify the bindings to connect to the WCF services. The binding elelments are implementation of a binding element is called a channel
Activation and Hosting ex: WAS= windows activation services Hosting – or IIS hosting
http://: transport

www.myweb,com/ : machine address

myWCFService/SampleService : Service Path

A WCF service is a class library, which defines one or more WCF Services interface contracts:  HelloWorldService.cs
  • System.ServiceModel assembly is referenced by all of the WCF service projects
  • The implementations of a WCF service are regular C# classes
  • A WCF service must be hosted in a hosting application (HelloWorld
  • VS2008 has a built-in hosting application for WCF services, which is called ASP.NET Development Server (HostDevServer)
  • A client application uses a proxy to communication with WCF services (HelloWorldServiceRef..cs)
  • A configuration file can be used to specify settings for WCF services (app.config)
Modus operandi
Create blank solution – File – New – project – Other project types – Visual studio Solutions – template: Blank Solution (Helloworld)
Add new – project Visual C# – (Template: class library or wcf Service application), I choose Library clas for the demo name HelloworldService.csproj
click Show all files button on the solution explorer
change the default namespace of te project : select property from the context menu : on th application tab change the default namespace: MyWCFServices
Add reference : System.Service.Model
Creating the Helloservice service contract interface
In the solution explorer, right click the HelloWorldService project – select Add/ New Item : HelloWorldService.cs (template : interface)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace MyWCFServices
    public interface IHelloWorldService
        String GetMessage(String name);
Implementing the HelloWorldService service contract
Create file HelloWorldService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyWCFServices
    public class HelloWorldService: IHelloWorldService
        public String GetMessage(String name)
            return "Hello world from " + name + "!";
Hosting the WCF service in ASP.NET Development Server
Create an emptywebsite -solution explorer , right click on the solution file and select add new web site template: empty web site  – name HostDevServer
we will host the HelloWorldService from this web site, we add a HellWorldService reference to the website
in solution exploreer – right click  on the web site and select add reference
test the host application:
start HostDevServer
if you will use a static port 80 instead a dynamic one , you can change  it to use a static port via the properties page of the web site. Change the Use dynamic ports setting to false a specify a static port: 8080
Adding an svc file to the host application
we must specify which service  this service should host, or an entry point for this web site:
this is a svc file
add new item Template: TextFile name: HelloWorldService.svc


@ServiceHost Service="MyWCFServices.HelloWorldService"%

adding a web config file to the host application
<?xml version="1.0"?>
  <add key="HTTPBaseAddress" value=""/>
   <service name="MyWCFServices.HelloWorldService" behaviorConfiguration="MyServiceTypeBehaviors">
    <endpoint address="" binding="wsHttpBinding" contract="MyWCFServices.IHelloWorldService"/>
    <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex"/>
    <behavior name="MyServiceTypeBehaviors">
     <serviceMetadata httpGetEnabled="true"/>
    <compilation debug="true"/>
Starting the host application
ctrl F5
Creating a client to consume the WCF service
Creating the client application project
in the solution explorer, right-click on the solution HellWorld and select add new project template : console application Name : HelloWorld
Generating the proxy and configuration files
1) strat the web server: ctrl F5
2) "C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\SvcUtil.exe" http://localhost:8080/HostDevServer/HelloWorldService.svc?wsdl out:HelloWorldServiceRef.cs / config:app.config
two files are generated: one fore the proxy : HelloWorldServiceRef.cs and the other for the configuration: app.config

Customizing the cleient application
add the two generated files to the project: In the solution explorer click show all files , right click on file HelloWorldServiceRef ans selct include in the project , dito fro app.config
add reference : from the solution explorer , rightclick on the HelloWorldClient project, select add reference and choose .NET Sysem.ServiceModel.
Modifiy pogram.cs
namespace HelloWorldClient
    class Program
        static void Main(string[] args)
            HelloWorldServiceClient client = new HelloWorldServiceClient();
            Console.WriteLine(client.GetMessage("Mike Liu"));
Cet article a été publié dans DotNet - WCF. Ajoutez ce permalien à vos favoris.

Laisser un commentaire

Entrez vos coordonnées ci-dessous ou cliquez sur une icône pour vous connecter:

Logo WordPress.com

Vous commentez à l'aide de votre compte WordPress.com. Déconnexion /  Changer )

Photo Google+

Vous commentez à l'aide de votre compte Google+. Déconnexion /  Changer )

Image Twitter

Vous commentez à l'aide de votre compte Twitter. Déconnexion /  Changer )

Photo Facebook

Vous commentez à l'aide de votre compte Facebook. Déconnexion /  Changer )


Connexion à %s