Introduction
The Windows Communication Foundation (WCF) is a platform for building distributed, service-oriented applications, clients and services that you want to connect together across a network boundary, that could be a Local Area Network or over the open internet. WCF itself gives you the platform support for defining services, defining hosts for those services and for defining client that are capable of making a connection to those services. Now WCF is built with interoperability in mind, so it does not presume that both sides of the communication are .NET. It's built on a set of open standard protocols that are implemented on other platforms including Enterprise Java.
Another thing to be familiar with WCF is that it's mostly about SOAP messaging. SOAP is a XML-based protocol for how to format information at the wire level. Although WCF provides some support for writing REST-style services.
Features
- Secure: By using WCF you get a system for distributed communications that's secure and has much flexibility on how you secure your calls.
- Scalable: It’s able to be deployed to many servers and have many clients.
- Robust: It allows you to write clients and services that are robust, meaning they can be highly available, reliable and so on.
- Flexible: There are many configuration options for everything and there’s many extensibility points, so it’s very easy.
WCF vs. Web API
- One is not a replacement for the other.
- One is not better than the other.
- Various technologies.
- WCF has many more features.
- The Web API is much more interoperable.
- WCF supports multiple transport protocols (HTTP, TCP, UDP, and custom transports) and allows switching between them.
- The Web API just requires the ability to make an HTTP request. .
- WCF requires tooling.
- WCF’s binding choices make it faster in the firewall.
WCF Building Blocks
A Service Contract is a contract between a client and service for how they will communicate. Part of defining a service contract is to define a data contract. Service contracts are collections of operations, or methods, in .NET terms and methods take parameters and return values. Those input parameters and their return values can be a complex type and those are what are called data contracts. Services is just a .NET class that implements that service contract interface and can contain the implementation details of the operations that are exposed. Next, this code needs a place to live and run on the server-side and that’s what is called a service host. You could host in any .NET process that would include a console application, potentially a client app, like a WPF or a Windows Forms client, most often this means a Windows service if you are going to host. You also have the option of hosting in IIS.
Next, you will to have clients that want to come and connect to your service. Now this client could be a true client application, such as WPF or WinForms, possibly server-side code behind. To call to a WCF service you need what is called a proxy that is a chunk of code that runs and lives in the client application process and exposes an interface that is the same interface as the service and allows the consumer to make calls against the proxy in the process and that proxy will dispatch the calls across the service boundary to the service and then the service call is dispatched down into the service implementation. The configuration exists on the client and server side and you can think of it as defining all the aspects that define the nature of that communication pipe between the client and the server.
WCF Configuration
- Endpoints
Endpoints are composed of what are called the ABCs of WCF. A stands for Address, B for Binding and C for Contract.
- Address: The address is how does a client gets to that service.
- Binding: The binding wraps up all the gory details of how the communication works at a wire level.
- Contract: The contract defines what is exposed at the service for the client to call into.
For a better understanding, we can consider a phone call. To have a successful phone call you need three pieces of information. You need a number to dial that is the equivalent of the address, you need a subject to talk about that is the equivalent of a contract and then you need to be speaking the same language as the person you are calling to successfully communicate and all those aspects of speaking the same language at a wire level are wrapped up in the binding.
- Behaviours
Behaviours are basically aspects of how the service handles a call once it comes into the service.
- Bindings
You can see that the binding is part of the endpoints, but every binding has many optional configurable aspects to it. Things like how big your message can be, or what kind of security are using or whether you are allowing distributed transactions to flow. These are all things that get wrapped up in the binding definition.
Demo
Go and create a new WCF project.
If you look in Solution Explorer, you can see that what is generated here is an IService1, that you can see on the screen, that is an interface marked as ServiceContract. In that same file there is also a DataContract definition and then there is the Service1 implementation class.
The other thing in the project is an App.Config file.
Run the application, It will open a WCF Test client.
Click on the GetData() Method and type anything into the request field on the top.
Then press the Invoke button. You will see the response on the bottom.
Now we can add any client to invoke this service. Select WPF application for demo.
Just drag and drop a TextBox and Button into WPF application.
Go to the solution Explorer, right-click on references in the WPF application and add a Service Reference. Just click on the Discover button. This will automatically select the WCF service because both are in the same solution.
Click on OK, you can see the new service reference in the following figure.
Open the App.Config of WPF application. You can see the configuration of the WCF service.
Create one event for button click; on button click write the code to invoke the WCF service and make the WPF application the startup project. Run the application.
- private void Button_Click(object sender, RoutedEventArgs e)
- {
- ServiceReference1.Service1Client proxy = new ServiceReference1.Service1Client();
- MessageBox.Show(proxy.GetData(Convert.ToInt32(txt1.Text)));
- }
Summary
In this article you learned about WCF and its implementation. I hope you like this article. Thanks for reading.