WCF Introduction - Part1
Introduction
Microsoft Windows Communication Foundation (WCF), alongside Windows Workflow Foundation (WF) and Windows Presentation Foundation (WPF), is intended to become the primary framework for building the next wave of business applications for the Microsoft Windows operating system. WCF enables you to build powerful service-oriented systems, based on connected services and applications. Here is the new version of WCF introduction tutorial using Visual C# 2010
Overview of WCF Features
Interoperability and integration of diverse APIs are only two (very important) aspects of WCF. In addition, WCF provides a rich software fabric that complements the remoting technologies it exposes. Consider the following list of major WCF features:
1. Support for strongly typed as well as untyped messages. This approach allows .NET applications to share custom types efficiently
2. Support for several bindings (raw HTTP, TCP, MSMQ, and named pipes)
3. Support for sessionlike state management techniques, as well as support for one-way stateless messages
Fig1: Typical WCF Application
Purpose of WCF
Web services are just one technology that you can use to create distributed applications for Windows. Others already mentioned include Enterprise Services and .NET Framework Remoting. Another example is Microsoft Message Queue (MSMQ). If you are building a distributed application for Windows, which technology should you use, and how difficult would it be to switch later if you need to? The purpose of WCF is to provide a unified programming model for many of these technologies, enabling you to build applications that are as independent as possible from the underlying mechanism being used to connect services and applications together (note that WCF applies as much to services operating in non-Web environments as it does to the World Wide Web)
The ABCs Of WCF
Hosts and clients communicate with each other by agreeing on the ABCs, a friendly mnemonic for remembering the core building blocks of a WCF application, specifically address, binding, and contract
Address : The location of the service
Binding : WCF ships with a number of different bindings the specify network protocols. Read more about wcf bindings
Contract : A description of each method exposed from the WCF service.Read more about wcf contracts
Contract
The notion of a contract is the key to building a WCF service. While not mandatory, the vast majority of your WCF applications will begin by defining a set of .NET interface types that are used to represent the set of members a given WCF type will support. Specifically, interfaces that represent a WCF contract are termed service contracts. The classes (or structures) that implement them are termed service types.
WCF service contracts are adorned with various attributes, the most common of which are defined in the System.ServiceModel namespace. When the members of a service contract contain only simple data types (such as numerical data, Booleans, and string data) you can build a complete WCF service using nothing more than the [ServiceContract] and [OperationContract] attributes.
However, if your members expose custom types, you will need to make use of types in the System.Runtime.Serialization namespace (see Figure 25-5) of the System.Runtime.Serialization.dll assembly. Here you will find additional attributes (such as [DataMember] and [DataContract]) to fine-tune the process of defining your interface types.
Metadata
Client applications require access to the metadata of a service so that they can determine the operations the service implements
Demo
Please go through the following HelloService WCF application demo.
Step1: Open VS2008, Start WCF service application
Step2: Delete IDE generated default service implementation.
Step3: Add new interface for service contract and operation contract definition
Step4: Define the Service Contract and Operation Contract
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; namespace Hello { [ServiceContract] public interface IHelloService { [OperationContract] string SayHello(); } }
Step5: Implement the service
Add new class to the project.
Implement the service contract interface.
using System; namespace Hello { public class HelloService : IHelloService { public string SayHello() { return "Hello From NeST"; } } }
Step6: Create service definition file (.svc )
In the Add New Item dialog box, select the Text File template, Change the name of the file to HelloService.svc (The service definition file must have the same name as the Service and have the .svc suffix)
Step7 : Configure service definition file
Add following code to the HelloService.svc file displayed in the code view window.
<%@ServiceHost Service="Hello.HelloService" %> <%@Assembly Name="HelloServiceApplication" %>
ServiceHost : Represent the full qualified name for service contract implementation class.
Assembly : Represent the WCF service assembly name.
Step8: Edit the *.config(web.config) file for service hosting
<system.serviceModel> <services> <service name="Hello.HelloService"> <endpoint address="" binding="basicHttpBinding" contract="Hello.IHelloService" /> </service> </services> </system.serviceModel>
The <serviceModel> section of the Web.config file contains the configuration information for a WCF Web service. The <services> section contains the details for each service implemented. The name attribute of the <service> element specifies the namespace and class that implement the service.
The <endpoint> element provides the details of the service that client applications require in order to communicate with the service. An endpoint comprises three pieces of information: an address, a binding, and a contract. The address is the location that the application hosting the service uses to advertise the service. In the case of IIS, the address element actually is ignored as IIS will use a URL containing the name of the virtual directory holding the service and the name of the .svc file as the endpoint (in this case, http://localhost/HelloService/HelloService.svc). The binding element specifies items such as the transport mechanism used to access the Web service, and the protocol to use, amongst other items. One of the WCF 4 feature is default configuration
Step9: Build and Test the Application.
Rebuild the application and view web browser.
Step10: Enable Metadata for client access.
<system.serviceModel> <services> <service name="Hello.HelloService" behaviorConfiguration="HelloServiceMetaData"> <endpoint address="" binding="basicHttpBinding" contract="Hello.IHelloService" /> </service> </services> <behaviors> <serviceBehaviors> <behavior name="HelloServiceMetaData"> <serviceMetadata httpGetEnabled="true" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel>
Rebuild the application and view in Browser.
Step11: Test the service using VS2008 tools ( wcftestclient.exe )
Go to VS2008 command prompt and issue the following command
wcftestclient <service url>
Cooooooooooool :-)
What Next ?
Next Article i will cover detailed information of WCF client application development and WCF server/client easy configuration using Visual studio IDE.
Conclusion
This tutorial was created to help you understand the important of windows communication foundation. I Hope u have found this tutorial helpful and interesting, and that it leads you to implement SOA on your current and upcoming application!. For more WCF tutorials, visit More WCF tutorials
Post Comment
DTFTn3 Im obliged for the blog.Thanks Again. Great.
Hi admin, i love to read your articles, but i noticed that is hard to find your website in google, i found it on 22 spot, you should build more backlinks, i know perfect tool to automate this task, just search in google for: xRumer submitter 4 elites - you will find out how to promote your site, business on thousands of websites a day and increase traffic and sales dramatically!