WCF, or Windows Communication Framework, is the next generation development platform from Microsoft, encompassing and standardising the features provided by earlier technologies such as COM+, DCOM, web services and remoting.
The topic is much too vast to try and explain in a single post, or even two or three, so I’m going to start off with an example that can be downloaded and build on it as time permits.
The example has 4 projects that contain:
1) The service contract(s)
2) The service
3) A console application to host a service
4) A client application the will consume the service.
Terminology
Some terms that need explaining up front are:
> Contract
This is essentially an interface. By using contracts, classes can be instantiated through a service that implement the same interfaces. This means that, if you provide functionality to third parties, you need only ship a library of contracts(interfaces), rather than the implementations themselves.
> End-Point
An endpoint is where the service will be exposed by the host. The mnemonic ABC can be used to describe an end-point. A – Address, B – Binding, C – Contract.
> Binding
This is a set of communication aspects and patterns which will be used by a service. This binding information is published by a service in its metadata, allowing clients/consumers to correctly choose their communication properties. WCF provides 9 standard bindings that can be used, and tweaked, if necessary.
A look at some of the code
In IServiceContract.cs (from the WCFService Contracts project), you will see some interesting attribute classes.
[ServiceContract()]
publicinterfaceIServiceContract
{
[OperationContract(Name = "FooOne")]
string Foo(string objectName);
[OperationContract(Name = "FooTwo")]
string Foo(string greetingWord, string objectName);
}
The ServiceContract() attribute tells WCF that this interface can be exposed as a service contract. Inside this class, methods that are individually exposed have the OperationContract() attribute. Take note that the “Name” parameter is set, as contracts may not expose methods that have the same name, regardless of whether they have the same method signature or not.
Moving along, you can have a look at the code in the client application.
ChannelFactory<IServiceContract> factory = newChannelFactory<IServiceContract>("");
IServiceContract wcfService = factory.CreateChannel();
string s = wcfService.Foo("Damn Right", "Doggy!");
MessageBox.Show(s, "WCFService Client");
There are many ways to create or open a channel to a service. I’ve found that using the ChannelFactory class works easiest, and the settings are read from the configuration file.
Configuration
Although I’ve done the configuring of my services, both hosting and client, in a configuration file, these could also be done in code. The code below duplicates the first line in the previous code example, except the configuration is done in code as apposed to being read from a file.
NetTcpBinding binding = newNetTcpBinding();
EndpointAddress endpoint = newEndpointAddress("http://localhost:9000/WCFService/");
ChannelFactory<IServiceContract> factory = newChannelFactory<IServiceContract>(binding, endpoint);
You may also notice that, in the host project, I’ve exposed an http binding as well. To see how transparent WCF is in implementing different formats, browse to
http://localhost:8000/?wsdl .
Next
I’ll be putting together a more real-world example in the near future that will showcase some of the real power of WCF.