WCF

SOA tenants (CAPE)

·         Contracts not code

·         Autonomous services

o   Operate in isolation & be fault tolerant

o   Service boundary is also a unit of versioning

o   Independently deployable

·         Policy based compatibility

·         Explicit service boundaries

Hosting

·         Self
Console, winforms, NT service, etc.

·         IIS
IIS requires a .svc file to back the endpoint (e.g. http://somehost/MyService.svc) in order to route the request to the listener. The .svc file may be as simple as <% @ServiceHost Service='MyNamespace.MyImplementingType’ %>, or it may have a code behind file.

·         WAS (Windows Activation Service)

Both IIS and WAS support Message-Based activation (this is where if the service host isn’t already running, it’s started automatically when the first message comes in)

Channels

Service host creates a channel listener (for each endpoint) which in turn generates a communication channel which in turn comprises of transport channel, message encoding channel, (optionally) security channel and so on. i.e. we’re talking a channel stack or pipeline here.

Behaviors

Modifies how messages are processed as they flow through the channel stack. Behaviors are ‘local’ in that only the client or server implementing them knows about them. Behaviors are not exposed to the wider world through the contract or other metadata.

DLLs

WCF design / best practice allows you to put the service interfaces and implementations in one set of assemblies, and the hosting code in another. This allows services to be ‘ported’ to different environments (Web, NT Service, Console, etc) easily.

Tools

·         Servic Configuration Editor

·         WCF Service Host (WcfSvcHost.exe /service:serviceAssemblyPath /config:serviceConfig)

·         WCF Test Client (WcfTestClient.exe mexAddress)

·         ServiceModel Metadata Utility (svcutil)

Mex

·         Metadata Exchange

·         Like any other endpoint, needs ABC

o   A is the base address (can’t move it)

o   B is mexHttpBinding, mexHttpsBinding, mexTcpBinding or mexNamedPipeBinding

o   C is [System.ServiceModel.Description.]IMetadataExchange)

Coding

ServiceContractAttribute

When applied to a class, indicates the class is a service type. When applied to an interface, indicates all classes that implement that interface are a service type. If the Namespace parameter is not set, it defaults to tempuri.

OperationContractAttribute

When applied to a method, indicates the method is a service operation (and is part of the contract).

ABC (Address, Binding, Contract)

·         Address
Scheme (net.tcp, net.pipe, net.msmq, http) / endpoint URI

·         Bindings
Protocols (transport, security, etc) & message encoding (binary, XML, etc)

·         Contract

Namespace & DLL for WCF code

System.ServiceModel

Console hosting code

// A part of ABC (but not necessarily all of A) & specifying the contract implementer
ServiceHost sh = new ServiceHost(typeof(MyService), new Uri(“http://localhost:1234/Optional1”));
// B & C part of ABC (and optionally more A)
sh.AddServiceEndpoint(typeof(IMyService), new BasicHttpBinding(), “Optional2”);
// Now listening on http://localhost:1234/Optional1/Optional2
sh.Open();

Console calling code

Proxies are generated from the ChannelFactory:

EndpointAddress ep = new EndpointAddress(http://localhost:1234/Optional1/Optional2”);
IMyService proxy = ChannelFactory<IMyService>.CreateChannel(new BasicHttpBinding(), ep);

If we generated the proxy from the wsdl:

MyServiceClient client = new MyServiceClient

 

Note that channel clean up if different depending on whether the channel is in a faulted state or not:
if (client.State == CommunicationState.Faulted) client.Abort(); else client.Close();

Behaviors

·         Service Behaviors
Provided by types that implement the [System.ServiceModel.Description.]IServiceBehavior  model.
Config set up is two phase:

1.       Declare a set of behaviors in <system.serviceModel><behaviors><serviceBehaviors><behavior name='mySet'> …

2.  Attach the set of behaviors to one or more services
<system.serviceModel>…<services><service behaviorConfiguration='mySet'…>

Programmatic set up is via ServiceHost.Description.Behaviors:

ServiceHost sh = new ServiceHost(typeof(myClassThatImplementsTheContract));
ServiceMetadataBehavior b = host.Description.Behaviors.Add(…);

·         Endpoint Behaviors
Provided by types that implement [System.ServiceModel.Descritpion.]IEndpointBehavior
These affect the server’s interaction with client proxies. For example, debugging, timeouts, security, serialization, …

·         OOB Behaviors

o   serviceMetadata

§  Set httpGetEnabled to turn WSDL on & off

o   serviceDebug

§  Set httpHelpPageEnabled to off help page on & off

§  Set includeExceptionDetailInFaults to true to include exception details in faults (duh)