What is WCF?

Windows Communication Foundation (WCF) in short enables you to build plain .Net classes and expose them as a technological agnostic service to the outside world. This is a good idea for several reasons. It tends to reduce coupling. Also writing those services yourself adds little business value and lots of things can get wrong if you wold do this yourself.

Why would you even need services that are decoupled? The same reason why the whole industry is progressing to ever more abstraction. It may be slowing down execution time, yet development speed is almost always more important than execution time.

So in a sense it is an easy way to support SOA structures or Microservice architectures, that build on the concept of domain driven design (DDD). DDD is a whole topic on itself. In brief DDD seperates your business in several contexts that have certain boundaries. Those boundaries can only be overcome by messaging. This is what WCF enables you to do. It is also certainly useful for other cases.

When you read something about DDD today, most people will advocate to use Restful services or Messaging approaches. WCF at its core uses SOAP, but can certainly expose a Rest service, even though ASP .Net Web API does this way better. Also you can create publish/subscribe services with WCF they are windows specific. So you might ask the question:

Why is it still useful to learn WCF?

There are several reasons in my opinion to learn and apply WCF.

Firstly there are lots of legacy applications out there that might be using WCF. So it can do no harm to know your way around. WCF is also quite easy to set up and manage, and lets you do a lot of stuff that would be hard and time consuming to implement yourself.

Secondly I personally use it a lot for inproc communication on the same machine. For example for tracing to a hooked up console application or similar things. This works very good, because WCF is highly optimized for .Net IPC. The same is true for TCP. So if you have a windows environment, WCF can still make a lot of sense, because it might increase your deveolpment speed drastically.

Thirdly everything is a tool, and therefore has its own applicable situations. A full blown REST API might be useful for IPC, because I could enlarge it later to communicate with other services. But is it really the right thing to do for my above example of a little tracing or maybe a duplex channel to steer application behavior (say of a WPF app) from an external process? I would argue that it is not. So like every tool WCF has its advantages and its disadvantages

Last but not least understanding WCF helps you to understand serviceorientation, details of communication, its interception model and lots of other ideas. In pair with its highly thougt out architecture, it helps you become a better programmer and engineer, which is also a reason you might read this blog.

So how does WCF work on a general level?

To use WCF you need to create a service, a client, tell them where they can find each other and on what they can interact with each other. That is all, the rest is handled by WCF.

Because WCF uses Simple Object Access Protocl (SOAP) under the hood, you need to describe your serviceinterface in a way that allows WCF to generate the needed WSDL that can be consumed by the clients.

To create an exposable service you need to take the following steps:

  1. Expose an CLR interface (or a class, but preferably an interface) that is decorated with the ServiceContractAttribute.
  2. Decorate the methods you want to expose with the OperationContractAttribute.
  3. All your CustomTypes you want to expose, need to be decorated with the DataContract attribute.

Remember that because WCF uses SOAP, CLR concepts cannot be used. So things like visibility, indexers, properties and events cannot be exposed on an interface, or for visibility have no bearing.

Endpoints – the ABC of WCF services

Now that you have your service class, you will need to set it up to be consumed. Therefore you need to host the service. But even before that you need to determine where you can find this service.

This is done with the ABC of WCF. ABC here stands for Address, Binding and Contract. These three make up what is called an Endpoint in WCF terms. An Endpoint describes your service Where to find it (Address), How (Binding) to interact with it and What (the contract) the service can do.

Where to find it – the Address

The address determines the unique name of the service. It also tells you which protocol/transport scheme is used.
WCF supports the following protocol/transport schemes:

  • Http/s
  • TCP
  • IPC
  • MSMQ
  • WebSocket
  • UDP

To specify an Address you use the following format:

[baseAddress]/[optionalUri]
baseAddress = [transportProtocol]://[machine or domain][:optionalPort]

Examples for this are:

http://localhost:8080
http://localhost:8080/MyService
net.tcp://localhost:8080/MyService
net.pipe://localhost:8080/mypipe

The address defaults to default ports like tcp defaults to 808, but I would always advocate to specify a Port, to make your service definition more explicit.

How to interact with the Service – the Binding

The binding groups together several possible options for message transport. The reason for bindings is, that there are a staggering amount of permutations on possible combinations, where WCF provides you with easy to use prepackaged groups. Those options that are grouped together include the protocol, encoding, security, authentication, authorization, reliability and others.

WCF provides you the following bindings (and several more, which might be used in some more esoteric edge cases)

  • TCP binding
    class: System.ServiceModel.NetTcpBinding
    supports: reliability, transactions, security, and is optimized for WCF-WCF communication by binary encoding
    notes: Requires both client and server to use WCF
  • IPC binding
    class: System.ServiceModel.NetNamedPipeBinding
    supports: reliability, transactions, security, and is optimized for WCF-WCF communication by binary encoding
    notes: Requires both client and server to use WCF, is most performant and secure, only same machine
  • UDP binding
    class: System.ServiceModel.NetNamedPipeBinding
    notes: Requires both client and server to use WCF, no reliability –> duplication, package loss, etc. is not always feasible. Possbile scenarios are event broadcasting or discovery
  • MSMQ binding
    class: System.ServiceModel.NetMsmqBinding
    supports: disconnected services
  • WebService Binding
    class: System.ServiceModel.WSHttpBinding
    supports: reliability, security and transactions
    notes: interoperable with text encoding, uses http or https
  • [Web Binding]
    notes: better use ASP.Net Web API, is used for REST API with WCF
  • [WebSocket Binding]
    notes: not interoperable, callback over internet are better used with Rest

What can the service do – Servicecontract

We already saw that a service needs to expose the ServiceContractAttribute, OperationContractAttribute and apply the DataContractAttribute to its custom Type parameters. Yet there are two other contracts. The first one is the FaultContract, which is used to propagate exceptions to the client. And there is also the MessageContract. Both are used to manipulate the generated SOAP message.

Notes on Endpoint

The Address and the Binding must be consistent, or else the service crashes at loadtime. That means if your address states net.tcp://[…] and your binding needs to be NetTcpBinding.

An endpoint can be configured administratevely (app.config/exe.config or web.config) or programmatically. Both have its advantages and disadvantages which we explore in Configuration.

Also any Service can have several endpoints as long as their URI is different. That means your service can supply an endpoint for http and tcp at the same time, again as long as their URI is not equal.

Summary

WCF is the windows implementation of distributed services for different protocols/transport schemes. It speeds up development time and makes integrating different systems a breaze.

Advantages are the ease of use, its wide adoption, thoughtful programming model and flexibility. Also it supports asynchronous/reactive model of programming, distributed transactions and fault propagation.

Disadvantages are that it is only highly optimized for WCF-WCF communciation whicht binds you to windows environments. Because It uses SOAP as protocol, some of its concepts surface by sort of leaking abstraction (Datacontract and serializer etc.).

Yet all in all it can be still a highly viable option, because each tool has its applications.

To create a WCF service you will need an Endpoint. An Endpoint is composed by the ABC of WCF. Where Address tells you where to find a service, the Binding tells you how to interact with the service, and the Contract tells you what the service can do.

 

Where to go from here?

Hosting of WCF services
Service InstanceManagement
Set up a Client
ServiceContract and OperationContract
DataContractAttribute
Callback

Categories: WCF

0 Comments

Leave a Reply

Avatar placeholder