You are on page 1of 11

WCF Tutorial

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

tnvbalaji.com

1

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

Binding is a specification of the communication mechanism (a binding) that described how messages should be sent tnvbalaji. Address .com 3 . Multiple Transports and Encodings 8. Binding . AJAX and REST Support 12. Durable Messages 10. reliable. Data Contracts 6. transacted solutions that integrate across platforms.Web Services into a single service-oriented programming model for distributed computing.microsoft. Service Orientation 2. Service Metadata 5. Endpoint Endpoint is a construct at which messages are sent or received (or both).Message Queues and 4. WCF interoperate between WCF-based applications and any other processes that communicate via SOAP (Simple Object Access Protocol) messages Features of WCF 1. Extensibility To know more about features of WCF see: http://msdn.com/en-us/library/ms733103.WCF Tutorial WCF Basics Definition of WCF Windows Communication Foundation (WCF) is a framework for building serviceoriented applications. Multiple Message Patterns 4.aspx Terms of WCF A WCF service is exposed to the outside world as a collection of endpoints.Address is a location that defines where messages can be sent B. Interoperability 3. Endpoint comprises of ABC What are ABC's of WCF ? A. Reliable and Queued Messages 9. WCF is a unified framework which provides 1. Using WCF we can build secure. Security 7. Transactions 11. NET Remoting 2.Distributed Transactions 3.

[OperationContract] double Add(double i. } } tnvbalaji.ServiceModel public interface IMath { [OperationContract] double Add(double i.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. all services expose contracts. Types of Contracts: 1) Operation Contract: An operation contract defines the parameters and return type of an operation. [OperationContract] Complex SubComplexNo(Complex i. [DataMember] public int Real { get. double j). 2) Service Contract: Ties together multiple related operations contracts into a single functional unit. Complex j). set. with each endpoint exposing one or more service operations. } 3) Data Contract: The descriptions in metadata of the data types that a service uses.Runtime. In WCF. // Use a data contract [DataContract] //using System. } [DataMember] public int Imaginary { get.com 4 . set. 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.Serialization public class Complex { private int real. double j). Contract . [OperationContract] double Sub(double i.WCF Tutorial C. [OperationContract] Complex AddComplexNo(Complex i. Complex j). Service A construct that exposes one or more endpoints. private int imaginary. [ServiceContract] //System. double j).

Open Visual Studio 2010 and File->NewProject 1. Give Name as wcfMathServiceLibrary 4. select WCF in ‘Recent Templates’ 2. select ‘WCF Service Library’ 3.com 5 . This ‘wcfMathSerLib’ will be tested by ‘ConsoleMathClient’ and with ‘WCF Test Client’ Steps for creating wcfMathSerLib 1.WCF Tutorial WCF step by step Tutorial This is the Basic WCF Tutorial ‘wcfMathSerLib’ will be created in a step by step approach. Click OK tnvbalaji.

Serialization.com 6 . tnvbalaji. [OperationContract] Complex SubComplexNo(Complex i. Complex j).cs and add the code listed below IMath. [OperationContract] double Sub(double i. Delete IService1.cs using System.cs 3. Add IMath. private int imaginary. double j). [OperationContract] Complex AddComplexNo(Complex i.cs and Service1. double j). Complex j). namespace WcfMathServLib { [ServiceContract] //System.Runtime. using System.WCF Tutorial 2.cs and MathService.ServiceModel.ServiceModel public interface IMath { [OperationContract] double Add(double i.Serialization public class Complex { private int real.Runtime. } // Use a data contract [DataContract] //using System.

} public double Sub(double i.Real = i.web> <!-. result.Real.Imaginary .j. } } } MathService.Modify the App.j.cs namespace WcfMathServLib { public class MathService : IMath { public double Add(double i.Imaginary = i.serviceModel> tnvbalaji. the content of the config file must be added to the host's app. Complex j) { Complex result = new Complex().Imaginary = i. return result.Imaginary.com 7 . result.Real.Imaginary + j. Complex j) { Complex result = new Complex().When deploying the service library project.config file.web> <compilation debug="true" /> </system.Imaginary. result. } public Complex AddComplexNo(Complex i. double j) { return (i .WCF Tutorial [DataMember] public int Real { get. return result.Real = i. result. System. set.config file as shown App. --> <system. } public Complex SubComplexNo(Complex i.Real . } } } 4. set.j).Real + j.Configuration does not support config files for libraries. double j) { return (i + j). } [DataMember] public int Imaginary { get.0" encoding="utf-8" ?> <configuration> <system.config <?xml version="1.

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

2. 2.WCF Tutorial Result Using WCF Test Client 1. 3. 5. 4. 3. 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 9 . 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. 4.

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

2)). no1.AddComplexNo(no1. no2.WriteLine("Add of 3+3i and 2+2i = {0}+{1}i".. Program.WriteLine("Press <Enter> to run the client. Console.").MathServiceReference.Real.WriteLine("Sub of 3 and 2 = {0}". no1. result.Real = 2.Imaginary = 2. result = math. result. MathClient math = new MathClient().ReadLine().Real. no2). Console. result. math. Complex result = new Complex().WCF Tutorial The service reference will be added now modify the program. result.Add(3.SubComplexNo(no1. namespace ConsoleMathClient { class Program { static void Main(string[] args) { Console. Console. math.ReadLine(). 2)). result = math.Sub(3. Console. Complex no1 = new Complex().cs as shown below. } } } Result Compile and Run the project to see the Result *********** THE END *********** tnvbalaji. using ConsoleMathClient. Console. no2).cs using System.Imaginary). Complex no2 = new Complex().WriteLine("Sub of 3+3i and 2+2i = {0}+{1}i".WriteLine("Add of 3 and 2 = {0}".Real = 3.Imaginary)...Imaginary = 3.com 11 . no2. Console.