WCF Jumpstart - p-patel/software-engineer-knowledge-base GitHub Wiki
https://app.pluralsight.com/courses/03bd1447-905b-415c-8e57-bdd08853ab34/table-of-contents
Hello WCF!
...
Demo: Hello WCF Service
- In Properties | WCF Options 'Start WCF Service Host when debugging another project in the same solution' is selected that enables the WCF Class Library project to be started and debugged
- (Class libraries can also be debugged by using the 'Start external program:' option in Debug and selecting a program that uses the library) - this is what the above option is doing implicitly using the program
WCFServiceHostSVC.exe
- Debugging also starts up 'WCFTestClient.exe' by setting the Start Options | Command Line arguments
Demo: Hello WCF Client
- Use Add | Service Reference to create client proxy
Demo: Service App Solution Tour
- A distributed system for a pizza ordering system across a distributed chain
Summary
Provide a summary of WCF
WCF allows easy connection of clients and services for remote communications
What defines the service boundary?
Service Contract, Data Contracts and configuration
What are endpoints composed of?
(ABC) Address, Binding and Contract
What are behaviours used for?
They are used to configure aspects of how the service behaves when service messages are received
What are Bindings for?
They configure aspects of the communication pipe shared with the client
Implementing Services
Service Implementation Overview
- Create a WCF Service library project (a client project is also useful in the solution)
What are the high level steps to implementing a WCF Service?
Define Service Contract, define Data Contracts and implement Service class
Demo: Service Contracts
What type of VS.NET project is used to create a WCF Service library?
A WCF Service Library - lol
- WCF is an attribute-centric (declarative) programming model
ServiceContract
,OperationContract
attributes
Demo: Data Contracts
How do you define complex type service operation parameters and return types?
As Data Contracts using Entities (aka custom .NET types that expose properties)
DataContract
,DataMember
attributes
How do you define an implicit data contract and what is the limitation of this approach?
Define a public class with public properties (no attributes required). This is easier to define, however you have less control at the 'over-the-wire' level
Demo: Service Implementation
What is a service?
A service is just a class that implements an interface
- Instancing lifetime of WCF service modes:
- (Default) "Session-ful" Instance maintained while the client proxy is still communicating with the server side
- "Per call" New instance is created for each call. More suitable for a scalable (stateless) distributed system, avoiding holding onto state in the service on a per-client basis.
- Add attribute
[ServiceBehavior(InstanceContextMode=InstanceContextMode."PerCall|PerSession|Single")]
- Operations can be wrapped in a transaction declaratively using
[OperationBehaviour(TransactionScopeRequired=true)]
. Continuing WCF's declarative model, this will commit the transaction if the operation completes normally and rollback the transaction if the operation exits abnormally (i.e. an exception). Useful in operations that call an EF context'sSaveChanges()
which involves multiple queries and saving records to multiple tables.
Summary
-
define service using an annotated interface (ServiceContract, OperationContract)
-
define data contracts for operation parameters and return types (usually simple entities to go across the wire)
-
define service implementation class
-
Expose advanced aspects of services with other properties of the attributes discussed
Hosting Services
Introduction
- Hosting involves significant configuration
** What are the WCF hosting options?**
WCF service library hosting, self hosting and IIS hosting
Service Hosting Overview
- Services need to run in a host process
What does the service host need to do?
- Listen for incoming messages, instantiate the service class and dispatch calls to the appropriate methods in the instance.
- The service host also configures the protocols the service will be available on and the configuration of the individual endpoints (their behaviours and bindings)
- Self Host
- IIS Host - supports HTTP by default defined through an ASP.NET web app project, can handle other protocols such as NetTCP for Sockets, MSMQ and Named Pipes by using Windows Process Activation Service (WAS)
- WCF Service Library
What are the three WCF hosting options?
- Self Host in any .NET process (but typically a Windows service or console app)
- IIS Host
- WCF Service Library (for quick debugging / smoke testing)
Demo: Configuring Endpoints
What app.config elements are defined to configure an endpoint?
Emmet (kinda): 'system.serviceModel'>services>service[name]>endpoint*2[address binding contract]
Demo: Configuring Behaviours
What app.config elements are defined to configure service behaviour?
Emmet (kinda): 'system.serviceModel'>behaviors>serviceBehaviors>behavior>serviceDebug[includeExceptionDetailInFaults="true"]+serviceMetadata[httpGetEnabled="true"]
Demo: Configuring Bindings
What app.config elements are defined to configure bindings?
Emmet: bindings>(basicHttpBinding>binding[maxReceivedMessageSize="2147483647" maxBufferSize="2147483647"]>readerQuotas[maxArrayLength="2147483647" maxStringContentLength="2147483647"])+netTcpBinding>binding[maxReceivedMessageSize="2147483647" maxBufferSize="2147483647"]>readerQuotas[maxArrayLength="2147483647" maxStringContentLength="2147483647"]
- There are dozens of other binding configuration properties
Demo: Hosting in WCF Service Library
- Add connectionString to app.config for db access
- Ensure 'Start WCF Service Host' option is selected in project setting and 'Start Options' | 'Command line arguments' is set to '/client: "WcfTestClient.exe"'
- Ensure VS.NET is running as Administrator and then run the WCF project
Demo: Hosting in Console App (Self Hosting 1)
- Console app hosting is convenient for debugging