0% found this document useful (0 votes)
56 views11 pages

WCF Tutorial: " For - Before Reading/using The Document Content

This document provides a tutorial on Windows Communication Foundation (WCF). It defines WCF as a framework for building service-oriented applications that provides remoting, transactions, messaging and web services in a single programming model. The tutorial then provides steps to create a basic WCF service library called 'wcfMathSerLib' that exposes math operations and data contracts. It also provides steps to create a console client application called 'ConsoleMathClient' that references and calls the WCF service. The service and client are then tested using the WCF Test Client and by running the console client application.

Uploaded by

Raju Baghel
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views11 pages

WCF Tutorial: " For - Before Reading/using The Document Content

This document provides a tutorial on Windows Communication Foundation (WCF). It defines WCF as a framework for building service-oriented applications that provides remoting, transactions, messaging and web services in a single programming model. The tutorial then provides steps to create a basic WCF service library called 'wcfMathSerLib' that exposes math operations and data contracts. It also provides steps to create a console client application called 'ConsoleMathClient' that references and calls the WCF service. The service and client are then tested using the WCF Test Client and by running the console client application.

Uploaded by

Raju Baghel
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

WCF Tutorial

Note: Please refer http://tnvbalaji.com/terms-of-use/ for TERMS OF USE before reading/using the document content.

tnvbalaji.com

WCF Tutorial

INDEX
INDEX .......................................................................................................................2 WCF Basics ...................................................................................................................3 Definition of WCF .....................................................................................................3 Features of WCF ........................................................................................................3 Terms of WCF ...........................................................................................................3 WCF step by step Tutorial .............................................................................................5 Steps for creating wcfMathSerLib .............................................................................5 Result Using WCF Test Client ..................................................................................9 Steps for creating ConsoleMathClient .......................................................................9

tnvbalaji.com

WCF Tutorial

WCF Basics

Definition of WCF
Windows Communication Foundation (WCF) is a framework for building serviceoriented applications. Using WCF we can build secure, reliable, transacted solutions that integrate across platforms. WCF is a unified framework which provides 1. NET Remoting 2.Distributed Transactions 3.Message Queues and 4.Web Services into a single service-oriented programming model for distributed computing. WCF interoperate between WCF-based applications and any other processes that communicate via SOAP (Simple Object Access Protocol) messages

Features of WCF
1. Service Orientation 2. Interoperability 3. Multiple Message Patterns 4. Service Metadata 5. Data Contracts 6. Security 7. Multiple Transports and Encodings 8. Reliable and Queued Messages 9. Durable Messages 10. Transactions 11. AJAX and REST Support 12. Extensibility

To know more about features of WCF see: http://msdn.microsoft.com/en-us/library/ms733103.aspx

Terms of WCF
A WCF service is exposed to the outside world as a collection of endpoints. Endpoint Endpoint is a construct at which messages are sent or received (or both). Endpoint comprises of ABC What are ABC's of WCF ? A. Address - Address is a location that defines where messages can be sent B. Binding - Binding is a specification of the communication mechanism (a binding) that described how messages should be sent

tnvbalaji.com

WCF Tutorial C. Contract - Contract is a definition for a set of messages that can be sent or received (or both) at that location (a service contract) that describes what message can be sent. Service A construct that exposes one or more endpoints, with each endpoint exposing one or more service operations. Contracts: A contract is an agreement between two or more parties for common understanding and it is a platform-neutral and standard way of describing what the service does. In WCF, all services expose contracts. Types of Contracts: 1) Operation Contract: An operation contract defines the parameters and return type of an operation.
[OperationContract] double Add(double i, double j);

2) Service Contract: Ties together multiple related operations contracts into a single functional unit.
[ServiceContract] //System.ServiceModel public interface IMath { [OperationContract] double Add(double i, double j); [OperationContract] double Sub(double i, double j);

[OperationContract] Complex AddComplexNo(Complex i, Complex j); [OperationContract] Complex SubComplexNo(Complex i, Complex j); }

3) Data Contract: The descriptions in metadata of the data types that a service uses.
// Use a data contract [DataContract] //using System.Runtime.Serialization public class Complex { private int real; private int imaginary; [DataMember] public int Real { get; set; } [DataMember] public int Imaginary { get; set; } }

tnvbalaji.com

WCF Tutorial

WCF step by step Tutorial


This is the Basic WCF Tutorial wcfMathSerLib will be created in a step by step approach. This wcfMathSerLib will be tested by ConsoleMathClient and with WCF Test Client

Steps for creating wcfMathSerLib


1. Open Visual Studio 2010 and File->NewProject 1. select WCF in Recent Templates 2. select WCF Service Library 3. Give Name as wcfMathServiceLibrary 4. Click OK

tnvbalaji.com

WCF Tutorial

2. Delete IService1.cs and Service1.cs

3. Add IMath.cs and MathService.cs and add the code listed below

IMath.cs
using System.Runtime.Serialization; using System.ServiceModel; namespace WcfMathServLib { [ServiceContract] //System.ServiceModel public interface IMath { [OperationContract] double Add(double i, double j); [OperationContract] double Sub(double i, double j);

[OperationContract] Complex AddComplexNo(Complex i, Complex j); [OperationContract] Complex SubComplexNo(Complex i, Complex j); } // Use a data contract [DataContract] //using System.Runtime.Serialization public class Complex { private int real; private int imaginary;

tnvbalaji.com

WCF Tutorial
[DataMember] public int Real { get; set; } [DataMember] public int Imaginary { get; set; } } }

MathService.cs
namespace WcfMathServLib { public class MathService : IMath { public double Add(double i, double j) { return (i + j); } public double Sub(double i, double j) { return (i - j); } public Complex AddComplexNo(Complex i, Complex j) { Complex result = new Complex(); result.Real = i.Real + j.Real; result.Imaginary = i.Imaginary + j.Imaginary; return result; } public Complex SubComplexNo(Complex i, Complex j) { Complex result = new Complex(); result.Real = i.Real - j.Real; result.Imaginary = i.Imaginary - j.Imaginary; return result; } } }

4.Modify the App.config file as shown App.config


<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.web> <compilation debug="true" /> </system.web> <!-- When deploying the service library project, the content of the config file must be added to the host's app.config file. System.Configuration does not support config files for libraries. --> <system.serviceModel>

tnvbalaji.com

WCF Tutorial
<services> <service name="WcfMathServLib.MathService"> <host> <baseAddresses> <add baseAddress = "http://localhost:8732/Design_Time_Addresses/WcfMathServLib/MathService/" /> </baseAddresses> </host> <!-- Service Endpoints --> <!-- Unless fully qualified, address is relative to base address supplied above --> <endpoint address ="" binding="wsHttpBinding" contract="WcfMathServLib.IMath"> <!-Upon deployment, the following identity element should be removed or replaced to reflect the identity under which the deployed service runs. If removed, WCF will infer an appropriate identity automatically. --> <identity> <dns value="localhost"/> </identity> </endpoint> <!-- Metadata Endpoints --> <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. --> <!-- This endpoint does not use a secure binding and should be secured or removed before deployment --> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> <behaviors> <serviceBehaviors> <behavior> <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> <serviceMetadata httpGetEnabled="True"/> <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> <serviceDebug includeExceptionDetailInFaults="False" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>

tnvbalaji.com

WCF Tutorial

Result Using WCF Test Client


1. 2. 3. 4. Run the WcfMathServLib project you will get the WCF Test Client Select each method say AddComplexNo Give the values in Request Click on Invoke button See the results in Response

Steps for creating ConsoleMathClient


1. 2. 3. 4. 5. Open Visual Studio 2010 and File->NewProject select Visual C#->Windows in Installed Templates select Console Application Give Name as ConsoleMathClient Click OK

tnvbalaji.com

WCF Tutorial

2. Go to Solution Explorer Right click on ConsoleMathClient -> Select Add Service Reference the below dialog will be displayed 1. Click on Discover button 2. Give namespace as MathServiceReference and click OK

tnvbalaji.com

10

WCF Tutorial The service reference will be added now modify the program.cs as shown below. Program.cs
using System; using ConsoleMathClient.MathServiceReference; namespace ConsoleMathClient { class Program { static void Main(string[] args) { Console.WriteLine("Press <Enter> to run the client...."); Console.ReadLine();

MathClient math = new MathClient(); Console.WriteLine("Add of 3 and 2 = {0}", math.Add(3, 2)); Console.WriteLine("Sub of 3 and 2 = {0}", math.Sub(3, 2)); Complex no1 = new Complex(); no1.Real = 3; no1.Imaginary = 3; Complex no2 = new Complex(); no2.Real = 2; no2.Imaginary = 2; Complex result = new Complex(); result = math.AddComplexNo(no1, no2); Console.WriteLine("Add of 3+3i and 2+2i = {0}+{1}i", result.Real, result.Imaginary); result = math.SubComplexNo(no1, no2); Console.WriteLine("Sub of 3+3i and 2+2i = {0}+{1}i", result.Real, result.Imaginary); Console.ReadLine(); } } }

Result Compile and Run the project to see the Result

***********

THE END ***********

tnvbalaji.com

11

You might also like