WCF Tutorial: " For - Before Reading/using The Document Content
WCF Tutorial: " For - Before Reading/using The Document Content
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
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
tnvbalaji.com
WCF Tutorial
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; } } }
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
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(); } } }
***********
tnvbalaji.com
11