afedyanin
10/11/2017 - 9:41 AM

WCF Manual

WCF Manual & Samples References:

https://www.pluralsight.com/courses/wcf-end-to-end

Request/Response

  • Standard operation calls
  • Request AND Response SOAP message
  • Even for void-operations
  • Client blocks until call is complete
  • Even for void-operations

One-Way Operations

  • Fire-and-forget calls
  • For the most part
  • MUST have void-return-type
  • Marked with the IsOneWay property in OperationContract
  • No SOAP response message
  • No support for fault-handling
  • When transport session present, and proxy closes, client will block until call complete

Callbacks (Duplex Calls)

  • Ability for service to call back to the client
  • Requires a transport session
  • NetTcp, IPC, WS with Reliability or Security
  • Setup similar to normal WCF usage
  • Roles reversed but all rules apply (contracts, channels, etc.)
  • Because of different proxy base class, considered serious commitment
  • Very useful for reporting progress back to client
  • Callback can update UI
  • Must consider threading issues
  • Need to marshal
  • Need to async the call

Async Calls

  • Client-side pattern to execute WCF calls asynchronously
  • Service has no knowledge it’s being called asynchronously
  • Uses Task pattern
  • Also support for legacy Begin/End pattern
  • Client makes async call and returns immediate UI control
  • Can define callback (not the same as previous callback section) to be called when operation completes
  • Get return values a little different
  • Handle faults a little different

SOAP Faults

CLR exceptions will not cross machine boundaries

  • WCF uses “Soap Faults”
  • Specification defining fault message that is packaged and passed from service to client

WCF provides a .NET programming model to accommodate this without knowing too much about underneath

  • Service throws exeption
  • WCF packages it as SOAP fault, embeds it in response message
  • Client recreates received SOAP fault as CLR exception and throws it
  • Client proxy code can be wrapped in standard Try-Catch

Service Exception Handling Scenarios

Unhandled (IncludeExceptionDetailsInFault = false)

  • Client receives FaultException
  • Cannot receive any additional information
  • Proxy faulted

Unhandled (IncludeExceptionDetailsInFault = true)

  • Client receives FaultException
  • Can read exception message
  • Can read original exception type
  • Proxy faulted
  • Warning: this Service Behavior can be altered by admin

FaultException thrown

  • Client receives FaultException
  • Can read exception message
  • Proxy OK
  • Note: client can only catch one exception type

FaultException thrown

  • Client receives FaultExeption
  • Can read exception message
  • Proxy OK
  • Note: client can catch specific exceptions
  • T can be exception or custom fault contract
  • T must be known ahead of time and service contract tagged appropriately
  • FaultContract attribute

PerCall Instancing

  • Each call from a proxy (in any client) is serviced by a brand-new instance of the service
  • The host news-up a service instance, performs the operation, and disposes the instance (if IDisposable)
  • This is the case even if the client proxy stays open
  • Most scalable solution
  • Nothing left open and in memory
  • Typical in-and-out SOA calls
  • Service CANNOT hold in-memory state
  • Class-scoped variables

PerSession Instancing (default)

  • Host news-up service instance on first call from proxy
  • Or if proxy manually opened (explained in upcoming module)
  • All calls from the same proxy are serviced by same instance
  • Service can hold in-memory state
  • Class-scoped variables
  • If doing updates to state, must consider locking
  • When proxy is closed, service instance is disposed
  • On a final infrastructure-call
  • Transport session must be present
  • TCP Binding
  • IPC Binding (named pipe)
  • WS-Http Binding with Reliability or Security turned ON
  • Otherwise downgrade to per-call

Single Instancing

  • Host news-up service instance when opened
  • All calls from proxies, all clients, all countries, all planets, are serviced by same instance
  • Service can hold in-memory state
  • Class-scoped variables
  • If doing updates to state, must consider locking
  • When host is closed, service instance is disposed
  • Host can be tied to an existing service instance
  • Manually newed-up
  • Good for pre-hosting initialization

Demarcation

  • Service Contract can control who can start/end session
  • IsInitiating / IsTerminating properties
  • OperationContract attribute
  • IsInitiating defaults to true
  • IsTerminating defaults to false
  • Controls the creation and termination of transport session

Session Mode

Configures the requirement-mode of a Transport Session

  • TCP, IPC, WS with Reliability or Security
  • No Basic HTTP

Allowed (default)

  • Allows one but does not require it
  • No error thrown
  • PerSession services will behave like PerCall if no Transport Session

Required

  • Requires a Transport Session binding setup

NotAllowed

  • Does not allow a Transport Session binding setup

Concurrency

Single Concurrency

Service instance allows one call in at a time from each proxy

  • Remember this is per-instance – instance mode is relevant

PerCall

  • Each call serviced by new instance
  • Client can use same proxy on multiple threads (still new instances)
  • Service will still only allow one call at a time

PerSession

  • Each proxy serviced by new instance
  • Client can make proxy calls on multiple threads (same instance)
  • Service will allow only one call at a time

Single

  • Every call (from everywhere) serviced by same instance
  • Service will allow only one call at a time

Multiple Concurrency

WCF stays out of the way and provides NO locking Depending on scenario, you must provide your own locking

PerCall

  • Each call serviced by new instance
  • Client can use same proxy on multiple threads (still new instances)
  • All calls processed concurrently (but still new instance, no locking needed)

PerSession

  • Each proxy serviced by new instance
  • Client can make proxy calls on multiple threads (same instance)
  • All calls allowed in (even concurrently) so locking rules apply

Single

  • Every call (from everywhere) serviced by same instance
  • All calls allowed in (even from multiple clients) so locking rules apply

Reentrant Concurrency

Similar to Single Concurrency

  • WCF disregards lock when client is same one that placed it originally
  • Necessary only in case of callbacks

Metadata Exchange

Behavior can be exposed in two forms

  • Using HTTP through a base address
  • Requires behavior with httpGetEnabled property
  • Using a MEX endpoint on HTTP, TCP, or IPC
  • Requires behavior
  • MEX endpoint can also be added programmatically
  • Binding created with static helper method

Useful Binding Configurations

Reliability

  • End-to-end message transfer reliability. Overcomes potential transport failures across some networks
  • Provides for non-TCP bindings what TCP offers out-of-the-box

Ordered messaging

  • Ensures order of message servicing equals order of calls. Useful in one-way calls across Internet

Inactivity timeout

  • Sliding value specifying time reliable transport session will remain open after not receiving messages
  • Supports Infinite
  • Transport session only available under certain conditions
  • TCP, IPC, WS-HTTP (with Security or Reliability turned on)

Receive timeout

  • Sliding value specifying time non-reliable session will remain open after not receiving messages
  • Supports Infinite

Send timeout

  • Specifies time a call will wait for message to be processed

Message size

  • Specifies maximum size of SOAP message

Useful Behavior Configurations

Exception details

  • Provides a little more detail on unhandled SOAP faults

Throttling

  • Allows manipulation of values that prevent server snap and memory overload

Instancing & Concurrency

  • Determines how the service is instantiated and how it will handle thread locking

Proxy

  • WCF provides the ClientBase class
  • T is the service contract
  • Proxy class inherits it
  • Proxy also implements the service contract interface
  • Contracts assembly can be shared by client side
  • May not always be the case
  • Dependent on application architecture
  • Proxy methods use “Channel” property from ClientBase
  • Proxy also requires endpoint information
  • Can be fed through code
  • Usually fed through configuration

Contract Equivalence

  • Service and Data contracts can be housed in a shared DLL
  • Can also reside in different binaries for service or client
  • When using two contracts, both sides need to be equivalent
  • Contract names and member/operation names must be the same
  • If not, can use “Name” property of attributes to change
  • Contracts namespace must match
  • Use “Namespace” property to set
  • Can also use ContractNamespace assembly attribute

Channel Factory

  • Internally, proxies wrap the creating of a “channel”
  • Provide developer-friendly class
  • Remember a proxy is an implementation of the service contract
  • A service contract implementation can be obtained without proxy
  • Kind of a virtual proxy
  • No actual proxy class
  • Channel factory creates a proxy for you
  • Tied to the service contract
  • Can use config or go config-less

Version Tolerance

  • DataContractSerializer provides version tolerance
  • Two equivalent contracts may be “off” in their properties
  • Use IExtensibleDataObject interface
  • ExtensionData property (ExtensionDataObject type)
  • All you need to do is implement it in data contracts
  • Property can be use short-form syntax
  • WCF will use property to store extra property values for potential pass-through scenarios

Web Hosting

Self Hosting

  • The ServiceHost class
  • Used in ALL hosting scenarios
  • IIS instantiates it for you
  • One-to-One with the Service
  • Can optionally be provided with a base address
  • Requires Endpoint information
  • Can either be fed from Configuration or from Code
  • Once “opened”, services are listening and waiting
  • Two options for housekeeping
  • Close: Will wait for any in-progress calls
  • Abort: Will abort any in-progress calls (client gets exception)

Data Contracts

  • Incoming data (request) can be in a data contract or simple arguments
  • Return data (response) must be a data contract
  • A single simple type need not be wrapped in a data contract
  • Data contracts are simple classes with just properties
  • Auto-implemented properties are just fine
  • Use DataContractSerializerinstead of XmlSerializer

Attributes used

  • DataContract
  • DataMember

Service Contracts

  • Service contracts define the operations of a service
  • “Define” means “interface” in C# terms
  • Service contract is simply an interface with method members only

Attributes used

  • ServiceContract
  • OperationContract