70-562 - WCF

Filed under: , , , , by:

When it comes to WCF, from my experience I know that for the great majority of .NET developers WCF is just an acronym and definitely not a framework of their choice when building distributed application. But ASP.NET 3.5 specialist must know how to use WCF services. On my road to MCPD Enterprise Developer I had to take 70-503 exam, which covers WCF 3.5, therefore for me WCF part on ASP.NET 3.5 exam shouldn't be a problem, but I know that for some of developers that consider taking 70-562 exam that might be another obstacle. That's why I thought of creating this post that will give a short overview of WCF. I hope this will help anyone studying to the exam and anyone who wants to thrive as a .NET developer.
WCF stands for Windows Communication Framework and was created to unify all Microsoft Distributed Computing Technologies (Web Services, .NET Remoting, Enterprise Services, WSE, Microsoft Queueing). Before one had to learn 5 different technologies, whereas with WCF one learns one concept that is the same for all those technologies. This basic concept of WCF is a three-step process:
- You define a contract and implement it on a service
- You choose or define a service binding that selects a transport along with quality of service, security and other options
- You deploy an endpoint for the contract by binding it (using the binding definition, hence the name) to a network address
That's ABC (Address, Binding, Contract) of WCF. Address is a physical address where the service can be reached along with specifying transport protocol, Binding describes transport and message protocol, Contract describes class being exposed as a service. These are common for all remoting technologies. Based on specified binding and address a technology to use is determined.
Examples:
- Addresses: net.tcp://localhost:9000, http://localhost:8000, net.msmq://localhost
- Bindings: NetTcpBinding (connections across process and machine over TCP), BasicHttpBinding (support for ASMX web services), NetMsmqBinding (reliable messaging over MSMQ)
So if you use NetMsmqBinding with address net.msmq://localhost, you utilize Message Queue as before using System.Messaging.MessageQueue.
Contracts are the classes or interfaces that expose distributed methods. They require ServiceContract attribute. Methods require OperationContract attribute. Transfer objects (like custom classes returned by methods) require Serializable or in WCF DataConctract attribute, and within those objects all properties and fields require DataMemeber attribute.
Hosting a service:
- If you create ie. a WCF service library and run it, a WCF Service Host (WcfSvcHost.exe) will be launched and it will host the service
- Self-hosting the service in console application, Windows Forms or Windows Service by using System.ServiceModel.ServiceHost class
- In IIS or WAS Windows Activation Service) when you create WCF Service Application, which creates Service.svc. When you click on it and click "View Markup", you will see that it has something like that: <%@ ServiceHost Language="C#" Service="WCFService.Service" %>, which instructs to luanch ServiceHost and host specified service
Consuming a service:
- By WCF Test Client (WcfTestClient.exe) - a GUI tool that enables to test a service, invoke exposed methods and view responses sent by the service
- By proxy class generated after adding a service through Add Service Reference.
- By proxy class generated after running SvcUtil.exe utility. This tool has many options that allow to customize the way a proxy is generated ie. without generating config file or to generate also asynchronous methods
- By creating a class that inherits from System.ServiceModel.ClientBase
- By using ChannelFactory<TChannel> class, which has a method CreateChannel that returns TChannel. Usually, TChannel is an interface or a class that is defined by ServiceContract attribute.
- By adding  a service reference with path to the service to ScriptManager object in order to utilize it from Ajax
So there's a lot of ways in which a service can be hosted and also consumed but in order to do that, it has to be properly configured. Most of the times it will be a proper configuration in web.config or app.config file. If you self-host a service or consume a service via ChannelFactory, you can instantiate and configure an EndpointAddress object along with approperiate binding ie. BasicHttpBinding. You can also create your own ServiceHostFactory and hard-code configuration in order not to read from a configuration file, but the good practise is to have a configuration of your service in a configuration file within <system.serviceModel> configuration section. This process can be done by:
- altering an existing configuartion ie. created when creating WCF service from template
- writing from scratch
- using Configuration Editor Tool (SvcConfigEditor.exe) that is available when you select config file and choose from context menu "Edit WCF Configuration".
Here are a few screenshots that demonstrate different ways of hosting a service, consuming it and configuring it. The screenshots will come from examples that I will use to explain syndication feed available in WCF and how to consume WCF services in Ajax/javascript using JSON. I hope you've found this post useful and helpful when it comes to understanding different ways of hosting, consuming (calling) and configuring WCF services. I can't emphasize how much it is important sometimes to know more than 1 way of getting to the solution so you can choose the best one, and sometimes only other way will be the only one you'll be able to use. I hope this post has taught you what are different ways of hosting, consuming and configuring a WCF service. We will need those skills to create and call 2 WCF services that I'll create in the next two posts.






0 comments: