ABC’s Letter Where How What PostBox(Address) (Binding) (Contract) End Point
Address : “the Where”– Where the network messages should be sent.HTTP: http://myserver/myserviceTCP: net.tcp://myserver:8080/myservice
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…
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.
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.
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.
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.
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.
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
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.
– 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.
– 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.
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.
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.
– 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
.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.
– 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.
– 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].
– 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.