Windows Communication Foundation

421 views

Published on

Windows Communication Foundation

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
421
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Missings:Message Exchange Patterns from ContractsKnownTypes – 104 Page.
  • //Method – 1using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ServiceModel;namespace StockService{ [ServiceContract] public interface IStockService { [OperationContract] double GetPrice(string ticker); } public class StockService : IStockService { public double GetPrice(string ticker) { return 94.58; } } class Program { static void Main(string[] args) {ServiceHostserviceHost = new ServiceHost(typeof(StockService), new Uri("http://localhost:8000/StockService"));serviceHost.AddServiceEndpoint(typeof(IStockService), new BasicHttpBinding(), "");serviceHost.Open();Console.WriteLine("Press <ENTER> to terminate.\\n\\n");Console.Read();serviceHost.Close(); } }}//Method - 2using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ServiceModel;namespace StockService{ [ServiceContract] public interface IStockService { [OperationContract] double GetPrice(string ticker); } public class StockService : IStockService { public double GetPrice(string ticker) { return 94.58; } } class Program { static void Main(string[] args) {ServiceHostserviceHost = new ServiceHost(typeof(StockService));serviceHost.Open();Console.WriteLine("Press <ENTER> to terminate.\\n\\n");Console.Read();serviceHost.Close(); } }}<?xml version="1.0" encoding="utf-8" ?><configuration> <system.serviceModel> <services> <service name="StockService.StockService"behaviorConfiguration="myServiceBehaviour"> <host> <baseAddresses> <add baseAddress="http://localhost:8000/StockService"/> </baseAddresses> </host> <endpoint address="" binding="basicHttpBinding" contract="StockService.IStockService"/> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> <behaviors> <serviceBehaviors> <behavior name="myServiceBehaviour"> <serviceMetadatahttpGetEnabled="True"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel></configuration>
  • //Method – 1using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ServiceModel;namespace StockService{ [ServiceContract] public interface IStockService { [OperationContract] double GetPrice(string ticker); } public class StockService : IStockService { public double GetPrice(string ticker) { return 94.58; } } class Program { static void Main(string[] args) {ServiceHostserviceHost = new ServiceHost(typeof(StockService), new Uri("http://localhost:8000/StockService"));serviceHost.AddServiceEndpoint(typeof(IStockService), new BasicHttpBinding(), "");serviceHost.Open();Console.WriteLine("Press <ENTER> to terminate.\\n\\n");Console.Read();serviceHost.Close(); } }}//Method - 2using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ServiceModel;namespace StockService{ [ServiceContract] public interface IStockService { [OperationContract] double GetPrice(string ticker); } public class StockService : IStockService { public double GetPrice(string ticker) { return 94.58; } } class Program { static void Main(string[] args) {ServiceHostserviceHost = new ServiceHost(typeof(StockService));serviceHost.Open();Console.WriteLine("Press <ENTER> to terminate.\\n\\n");Console.Read();serviceHost.Close(); } }}<?xml version="1.0" encoding="utf-8" ?><configuration> <system.serviceModel> <services> <service name="StockService.StockService"behaviorConfiguration="myServiceBehaviour"> <host> <baseAddresses> <add baseAddress="http://localhost:8000/StockService"/> </baseAddresses> </host> <endpoint address="" binding="basicHttpBinding" contract="StockService.IStockService"/> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> <behaviors> <serviceBehaviors> <behavior name="myServiceBehaviour"> <serviceMetadatahttpGetEnabled="True"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel></configuration>
  • //Method – 1using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ServiceModel;namespace StockService{ [ServiceContract] public interface IStockService { [OperationContract] double GetPrice(string ticker); } public class StockService : IStockService { public double GetPrice(string ticker) { return 94.58; } } class Program { static void Main(string[] args) {ServiceHostserviceHost = new ServiceHost(typeof(StockService), new Uri("http://localhost:8000/StockService"));serviceHost.AddServiceEndpoint(typeof(IStockService), new BasicHttpBinding(), "");serviceHost.Open();Console.WriteLine("Press <ENTER> to terminate.\\n\\n");Console.Read();serviceHost.Close(); } }}//Method - 2using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ServiceModel;namespace StockService{ [ServiceContract] public interface IStockService { [OperationContract] double GetPrice(string ticker); } public class StockService : IStockService { public double GetPrice(string ticker) { return 94.58; } } class Program { static void Main(string[] args) {ServiceHostserviceHost = new ServiceHost(typeof(StockService));serviceHost.Open();Console.WriteLine("Press <ENTER> to terminate.\\n\\n");Console.Read();serviceHost.Close(); } }}<?xml version="1.0" encoding="utf-8" ?><configuration> <system.serviceModel> <services> <service name="StockService.StockService"behaviorConfiguration="myServiceBehaviour"> <host> <baseAddresses> <add baseAddress="http://localhost:8000/StockService"/> </baseAddresses> </host> <endpoint address="" binding="basicHttpBinding" contract="StockService.IStockService"/> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> <behaviors> <serviceBehaviors> <behavior name="myServiceBehaviour"> <serviceMetadatahttpGetEnabled="True"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel></configuration>
  • Windows Communication Foundation

    1. 1. Windows Communication Foundation Vijay Krishna Parasi
    2. 2. AGENDA• Why WCF Service & SOA Architecture• ABC’s of WCF• Hosting• MEX Endpoint• Contracts – WSDL – SOAP
    3. 3. SOA Architecture Example
    4. 4. ABC’s Letter Where How What PostBox(Address) (Binding) (Contract) End Point
    5. 5. Address : “the Where”– Where the network messages should be sent.HTTP: http://myserver/myserviceTCP: net.tcp://myserver:8080/myservice
    6. 6. Binding : “the How”– How the messages should sent.– Defines the channel used to communicate with an endpoint.– Channels will have series of binding elements.– The lowest level of binding element is Transport.– The built in transport protocols are HTTP, TCP, Named Pipes etc…
    7. 7. Contract : “the What”– Defines capability of endpoint.– Defines the operations that endpoint exposes.– Also defines the message formats that the operations require.– They maps to the class methods that endpoints.
    8. 8. WCF Service End Point Messages WCF Client End Point End PointService: Can have more than one end points. Listens for messages on the address specified by the endpoint(s). Expects the messages to arrive in specified format.
    9. 9. Hosting– To make service up and running.– And to make service respond to the client requests.– Hosting can be anything Console App, Web Application, IIS etc...– Hosting in IIS requires svc file.
    10. 10. Metadata Exchange (MEX) Endpoint– For Service Discoverability.– Helps the clients to obtain the information of ABC’s in the form of WSDL (Web Service Description Language).– When user clicks on Add Service Reference or use svcutil.exe, MEX endpoint will be called.– Client gets WSDL  Below artifacts will be generated • Proxy class in the language of the project. • Application configuration file– To configure MEX Endpoint, • Enable the “HTTP Get” behavior. Allows the user to access MEX endpoint via browser. • Add a MEX endpoint including the other endpoints.
    11. 11. WCF Service End Point Messages WCF Client End Point End Point MEX End PointMEX Endpoint: Must for a service to expose ABC’s to client.
    12. 12. Calling WCF Service from Client– Can be called in two ways • Channels - Using Custom Code • Svcutil.exe Using Tools– Channels: • Interface definition is shared b/w the client & service. • Client creates a ChannelFactory class to create a channel using ABC’s. • Using the channel object client calls the service methods.– Svcutil.exe • Add Service Reference from Visual Studio. • Internally calls the svcutil.exe
    13. 13. DEMO– Implementing a WCF service includes. • Implement a .NET class • Decorate the class with System.ServiceModel attributes.– System.ServiceModel namespace will be installed with.NET 3.0– WCF Service can be implemented, • Writing entirely in Code • Writing in Code & Configuration filesIf we specify an address in the endpoint it overrides thebase address.
    14. 14. – ServiceContract • Marks a class as Contract. • Expressed in standards based on WSDL.– OperationContract • Defines methods that can be invoked on the class. • Defines the messages that are passed to and from service methods. • Operations & Messages– Within the Endpoint definition we will specify the Address, Binding & Contract.
    15. 15. – When service is started by host • WCF starts the listener listens incoming requests on the configured address. • In case of IIS hosting, the listener is likely already started, WCF registers its listener with IIS.– When a message arrives, the service host • First, based on the channel stack defined by binding it applies decompression, decryption and security rules. • Second, based on the contract it deserializes the incoming message into .NET types and invokes the proper operation.
    16. 16. Once again ABC’s– Each endpoint constitute ABC’s • An addressable location on the network where messages are sent. • A binding that describes how messages are sent. • A contract that describes the message formats.
    17. 17. Let’s understand “Contracts”– Contract is binding agreement • B/w parties that specifies the supply of goods or services for a known price. • In WCF context, it’s an agreement between parties that specifies the message that can be exchanged based on the terms and conditions.– Description of the messages that are passed to and from service endpoints.– Endpoint must specify the operations it implement and the data formats it expect. These specifications make up the Contract.
    18. 18. – Types of Contracts: • Service Contracts • Data Contracts • Message Contracts– Systems should interoperate, so we need a common platform. So WSDL is chosen. • Its an XML grammar defines the functionality offered by a service and the format of messages. • Defines what services (operations) are available in service. • It defines the methods, parameter names, parameter data types and return data types.– SOAP – Simple Object Access Protocol • Communication protocol • Format for sending messages
    19. 19. .NET Types  XML XML  .NET Types.NET CLR Types WCF Service inside HostWCF Client End Points WCF Message Service Handler– How Contracts convert to CLR Types – 3 stage process • Client query the service to know ABC’s (endpoints). • This raises the metadata infrastructure on the service to generate WSDL and finally the proxy class. • Finally at runtime, when client calls the service the .NET types are serialized into XML and sends over wire.
    20. 20. – Service Contracts: • They define the operations performed by service. • Two Types – ServiceContract – OperationContract • ServiceContract – Define the Interface. • OperationContract – Define the methods inside interface. • By mentioning the [ServiceContract] & [OperationContract] they will be exposed in WSDL.
    21. 21. – Data Contracts: • They define which data types are passed to and from service. • WCF defines implicit contracts for built-in types such as int and string. • Can easily define explicit opt-in data contracts for custom types • Two Types – DataContract – DataMember • DataContract – Define which calls should be represent in XSD and include in WSDL. • DataMember – Define which class members should be represent in XSD and include in WSDL. • At runtime, the DataContractSerializer class serializes objects to XML using the rules defined in [DataContract] & [DataMember].
    22. 22. – Message Contracts: • Allows the service to interact directly with messages. • Describe the structure of SOAP messages sent to and from a service. • Gives complete control over the SOAP message sent to and from a service by providing access to the SOAP headers and bodies directly. • They define which data types are passed to and from service. • WCF defines implicit contracts for built-in types such as int and string. • Can easily define explicit opt-in data contracts for custom types • Two Types – DataContract – DataMember • DataContract – Define which calls should be represent in XSD and include in WSDL.

    ×