WCF in 10 minutes : lesson 3

November 16th, 2007 by eknowledger

This article applies to .Net Framework 3.0 ,Visual Studio 2005, WCF ,Windows Vista SDK ,C# 2.0.

WCF in 10 minutes : lesson 3

Hosting your Service in web based servers

At the last 2 lessons we discussed the concept of the WCF and how to build a simple WCF Service and host it on a Console based application , today we’ll try to host the same service on Web Based Server, for quick review please check lesson 2 of the series .

Create the Server Application :

Step 1 :

Open the visual studio and choose create new web project and select the following options

  • Project Template : WCF Service
  • Location : File System (this will use the integrated web server with the Visual Studio 2005  )
  • Language : C#

And press ok . As u can see the IDE will generates a predefined template for you  consist of the following files :

      • Web.Config
      • Service.svc
      • App_Code\Service.cs

These file is the main 3 files u need to run wcf service hosted in the web based servers , but we’ll modify the generated template to suite our case .

Step 2 :

We‘ll replace the generated service.cs code with our old AntiquesService Contracts and implementation which define the contracts for the IAntiquesService and provide the implementation for our  contracts .

The Service Contracts :

The Service Implementaion :

Notice:  when you host your application in web based servers there is no need to create the startup method to start up your hosting application instead of this you need to create the svc file which tell the hosting Web Server  to startup your application .

Step 3:

Now we need to modify the generated Service.svc to run our AntiquesService  , the generated Service.svc is very simple as that :

<% @ServiceHost Language=C# Debug=”true” Service=”MyService” CodeBehind=”~/App_Code/Service.cs” %>

In the service attribute place the name of your service implementation class in our case “EgyAntiques.Store.AntiquesService“ fully qualified class name , and in the CodeBehind attribute  place the path to the code file for your service in our case “~/App_Code/Service.cs“. after modifying the svc file it should look like that

<% @ServiceHost Language=C# Debug=”true” Service=”EgyAntiques.Store.AntiquesService” CodeBehind=”~/App_Code/Service.cs” %>

Step 4 :

This is the final step in developing our server application , Modify the configuration file for our service .

This step should be  very easy for you if you had read the past lesson we spend much time in explaining the concepts WCF service configuration files .

Open the generated web.config ,and find the tag <system.serviceModel> replace that tag with the following code

Run your WCF Web based service to make and make sure that your service metadata published to the explorer like in the following picture

The Client :

The process of creating the client is very easy and we goes in  its details in lesson 2 , there is no different between creating a WCF client for console hosted service and web server hosted service , We will create a console based client to interact with our service :

Add new console project to your solution name it client

Run your service then in the client right-click references and choose “ add service reference” then in the dialog copy the service URL in my case is  http://localhost:4819/EgyAntiques/Service.svc” and put it in the service URI field then  write  in the service reference name “AntiquesClient” this will be the service reference  name  , then press ok .

If you notice the WCF Framework generates for you the “AntiquesClient.map” file which is the proxy file that referencing your WCF Service ,also generates the app.config file for you which contain the client configurations for the service , and finally adds references to the

      • System.ServiceModel
      • System.Runtime.Serialization

Finally add the following simple piece of code to your Main method in the client :

        static void Main(string[] args)

        {

            Console.WriteLine(”Please press enter to get the list of our antiques :”);

            Console.ReadLine();

            AntiquesClient.AntiquesClient client = new AntiquesClient.AntiquesClient(”ep1″);

            try

            {

                Console.Write(client.DisplayAntiquesList());

                Console.WriteLine(”\n\n\nPlease select your Antique # to know it’s price :”);

                Console.WriteLine(”Your Antique Price is : ” + client.GetAntiquePrice(int.Parse(Console.ReadLine())));

                Console.WriteLine(”\npress enter to terminate the program … “);

                Console.ReadLine();

            }

            finally

            { }

        }

 Finally run the service , then start  new instance of our client application , try the client and enjoy the magic ;) :D

Resources :

  1. WCF in 10 minutes : lesson 1
  2. WCF in 10 minutes : lesson 2
  3. Download Demo Application Source Code

kick it on DotNetKicks.com Digg!

Hope this Helps,

Ahmed

WCF in 10 minutes : lesson 2

November 16th, 2007 by eknowledger

This article applies to .Net Framework 3.0 RC1 ,Visual Studio 2005, WCF ,Windows SDK RC1,C# .

WCF in 10 minutes : lesson 2

Your First WCF Service - EGYAntiques Service

 

Continue with you discovering the new inferastructure for Enterprise service development from microsoft , Windows Communication Foundation (WCF) . In this lesson we’ll develope a complete working wcf service , host and configure it not only that but we’ll learn how to publish the service metadata for the ousider consumer services and finally we’ll develope .net based client application and connect to our service .

 

 

 

Review Lesson 1

Read Lesson 2

Download Source Code

 

Best Regards,

Ahmed

WCF in 10 minutes : lesson 1

November 16th, 2007 by eknowledger

WCF in 10 minutes : lesson 1

Introducing Windows Communication Foundation (WCF)

(AL Salam Alykoum) This is hi in Arabic , today I’m gona take u in a quick ride to the Windows Communication Foundation [WCF] a very important part of the new .net 3.0 framework which is the core development engine for the new child of the windows family OS , windows vista . ok enough gossip let’s jump to our subject .

What is WCF ?

WCF [Windows Communication Foundation , aka Indigo] is the Microsoft service-oriented communication infrastructure and programming model. You can think of WCF as the Microsoft runtime for services. WCF make it easy to develop connected applications using a new SOA (Service Oriented Architecture) .

WCF supports large set of distributed application programming styles benefits in this of the layered architecture on top of the .net framework. WCF provides 2 level of programming models

  1. Low level Programming Model [untyped]: asynchronous, untyped message-passing primitives .Services based on that model include secure & reliable exchange.
  2. High level Programming Model [Typed] : a typed programming model including serialization facilities, queued and transacted exchanges, integration with other programming models such as MSMQ, COM+, ASP.NET Web Services, Web Services Enhancements (WSE), and other functions.

What is Service-Oriented Architecture (SOA) ?

Service orientation (SO) is an approach to software design and development that provide services based communication solution . In SO, message-oriented programs called services are the building blocks from which solutions are created. SO is complement to Object oriented (OO) not replacement to it , you can consider it the missing piece that fit in to complete the pattern. OO is not the best way to tie the application together , in OO an application is a tightly coupled collection of programs built from class libraries that have dependencies on each other. on the contrary SO composed of loosely coupled, autonomous service . The parts of the system communicate with messages. This system is loosely coupled. The parts can interact regardless of platform differences.

The people work with SOA defined four principle for the SO Applications , those principles known as the Four tenets for Service Orientation :

    1. Boundaries are explicit.
    2. Services are autonomous.
    3. Services share schemas and contracts, not classes and types.
    4. Compatibility is policy-based.

Case Scenario :

The diagram illustrate a case scenario that WCF addresses . Ancient Egypt Antique Furniture Stores decides to create a new application for ordering Antiques . the architect of this order application know that the core function of the system must be accessible by other software running both inside and outside the store . as a result he decided to build it in a service-oriented style ,which allow him to expose the system functionality to other software through a well defined set of services , to achieve that the system will use WCF .

The System designer know that the system will be accessed by 3 type of applications :

  1. A call center client application running on the Windows operating system desktops that are used by employees in the Store call center. Created specifically for the new order system, this application will also be built using the Microsoft .NET Framework and WCF.
  2. An existing order application built on a J2EE server running on a non-Windows system.
  3. Partner shop applications running on a variety of platforms, each located within a shop that has a business arrangement with the Antiques store.

WCF provide one unified solution for this type of scenarios with :

    • Unification of existing .NET Framework communication technologies.
    • Support for cross-vendor interoperability, including reliability, security, and transactions.
    • Explicit service orientation.

WCF Archecture :

Services describe themselves at the contract layer. The service runtime layer loads and runs services, managing many behaviors such as instancing and concurrency. The messaging layer provides the communication infrastructure, which includes various composable communication channels for transport, reliable messaging, security, and queuing. The activation and hosting layer allows services to run in a variety of environments, ranging from a self-hosted EXE to automatic activation and hosting by Microsoft Internet Information Services (IIS).

WCF at your hand :

Messaging Program in WCF are 3 types :

  1. A Service
  2. A Client
  3. An Intermediate (not cover in this quick tutorial)
      • A WCF Service is a program that exposes a collection of Endpoints. Each Endpoint is a gate for communicating with the outside world.
      • A Client is a program that exchanges messages with one or more Endpoints. A Client may also expose an Endpoint to receive Messages from a Service in a duplex message exchange pattern.

Endpoints :

Each Endpoint body consist of ABC parts , Address, Binding and contract .

    • Address : Where the Endpoint resides , a network address for the endpoint.
    • Binding : How the endpoint communicate with the outside world , by specifying things like transport protocol (e.g., TCP, HTTP), encoding (e.g., text, binary), and security requirements (e.g., SSL, SOAP message security).
    • Contract : What the endpoint communicate , essentially a collection of messages organized in operations that have basic Message Exchange Patterns (MEPs) such as one-way, duplex, and request/reply.

ServiceEndPoint Class represent the Endpoint

Endpoint Address :

basically a URI, an identity (used for security purposes), and a collection of optional headers.

Bindings:

A binding encapsulates all of the communication details required to make a service endpoint available. A binding includes information such as which transport the endpoint communicates over, what data encoding to use, whether security, reliable messaging, or transaction flow ought to be enabled .

binding is made up of a collection of binding elements. Each element describes some aspect of how the endpoint communicates with clients . each binding must contains at least one transport binding element, at least one message encoding binding element, and any number of other protocol binding elements.

Contract :

A Windows Communication Foundation (WCF) service contract is a collection of operations that specifies what the endpoint communicates to the outside world. Each operation is a simple message exchange, for example a request message and an associated reply message that form a request/reply message exchange.

The ContractDescription class is used to describe WCF contracts and their operations. Within a ContractDescription, each contract operation has a corresponding OperationDescription that describes

aspects of the operation, for example whether the operation is one-way or request/reply. Each OperationDescription also describes the messages that make up the operation using a collection of MessageDescription objects.

Summary :

This was a quick look to the Windows Communication Foundation WCF , I tried in this lesson to make u familiar with Service-orientation Concepts and Microsoft infrastructure for the SOA (WCF) so I hope this helps introducing you to the new WCF world .

References :

  1. Windows SDK RC1 Documentation
  2. Programming Indigo [MS Press]
kick it on DotNetKicks.com Digg!

Hello world!

November 16th, 2007 by eknowledger

Welcome to The Coder Blogs. This is your first post. Edit or delete it, then start blogging!