How To Create WCF Service

Submitted by: 
Language: 
Visitors have accessed this post 2156 times.

Introduction

In this tutorial you are going to learn WCF one of the key features of Visual Studio. WCF stand for windows communication foundation. WCF is one of powerful introduction of .net framework and provide grate solutions for space left by the web services. Using WCF we can build secure, reliable, transacted solutions that integrate across platforms.

Building a distributed system is a very common task for many programmers. However, doing so involves confronting numerous design challenges. WCF has overcome most of the issues with most of distributed system previously had.

Features of WCF

  1. 1
  2. Service Orientation
  1. Interoperability
  1. Multiple Message Patterns
  1. Service Metadata
  1. Data Contracts
  1. Security
  1. Multiple Transports and Encodings
  1. Reliable and Queued Messages
  1. Durable Messages
  1. Transactions
  1. AJAX and REST Support
  1. Extensibility

Before going to code WCF application we should learn some of key concepts of WCF API.

What is end point?
WCF Services exposes a collection of Endpoints, each Endpoint is a portal for communicating with the world. Endpoint is used to identify the service; it is more are like an address for your home. Each endpoint is used to identify the specific service. One service can have multiple endpoints. Client application will use this endpoint for communication with service. All the WCF communications are take place through end point. End point consists of three components. Address, Binding and Contract (ABC )

What is address

Address is the place which has ability to send WCF messages. In another word it has describe WHERE service has hosted


What is binding ?

Binding represent how the message are sent. In other word it specify the type of communication
WCF provides the following transport schemas like: HTTP, TCP, Peer Network, IPC (Inter Process Communicatiion), MSMQ. Basically this describe How end points are communicating

What is Contract ?

It defines “WHAT”. Contract identifies what is exposed by the service.

example of service end point

http://localhost:5784/CustomerServoce.svc


How to create WCF Application

Open VS 2010
File -> New -> Project
Select Visual C# -> WCF (also select .net framework 4.0) -> select WCF Service Application Template.
Give Name -> EchoService
Click OK.

Adding a Service

Right Click the project folder in Solution Explorer of VS 2010, select -> Add new Item, select -> Web, select -> WCF Service.
Name it -> CustomerService.svc

create service
Adding a DataContract

Right Click the project, select -> Add new Item, select -> web, select -> Class.
Name it -> Customer.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Runtime.Serialization;
  6.  
  7. namespace EchoService
  8. {
  9. [DataContract]
  10. public class Customer
  11. {
  12.  
  13. [DataMember]
  14. public string customerName { get; set; }
  15.  
  16. [DataMember]
  17. public string customerAddress { get; set; }
  18.  
  19. [DataMember]
  20. public int customerAge { get; set; }
  21.  
  22. [DataMember]
  23. public string customerCity { get; set; }
  24.  
  25. }
  26. }


Service Interface

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.Serialization;
  5. using System.ServiceModel;
  6. using System.Text;
  7.  
  8. namespace EchoService
  9. {
  10. // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "ICustomerService" in both code and config file together.
  11. [ServiceContract]
  12. public interface ICustomerService
  13. {
  14. [OperationContract]
  15. string GetCustomerName(string name);
  16.  
  17. [OperationContract]
  18. string GetCustomerAddress(string address);
  19.  
  20. [OperationContract]
  21. Customer GetCustomerDetails();
  22.  
  23. [OperationContract]
  24. List<Customer> GetCustomerS();
  25.  
  26.  
  27. }
  28. }

Implementing the service.

Usually WCF project can have lots of services but to implement successfully you should have at least one service.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.Serialization;
  5. using System.ServiceModel;
  6. using System.Text;
  7.  
  8. namespace EchoService
  9. {
  10. // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "CustomerService" in code, svc and config file together.
  11. public class CustomerService : ICustomerService
  12. {
  13.  
  14. public string GetCustomerName(string name)
  15. {
  16.  
  17. return string.Format("Customer Name Is : {0}", name);
  18.  
  19. }
  20.  
  21. public string GetCustomerAddress(string address)
  22. {
  23.  
  24. return string.Format("Customer Address Is : {0}", address);
  25.  
  26. }
  27.  
  28.  
  29. public Customer GetCustomerDetails()
  30. {
  31.  
  32. Customer newCustomer = new Customer();
  33.  
  34. newCustomer.customerName = "Smith";
  35. newCustomer.customerAddress = "River Avanue NW11E London";
  36. newCustomer.customerAge = 25;
  37. newCustomer.customerCity = "London";
  38.  
  39. return newCustomer;
  40.  
  41. }
  42.  
  43.  
  44. public List<Customer> GetCustomerS()
  45. {
  46. List<Customer> Clist = new List<Customer>();
  47.  
  48. Customer newCustomer1 = new Customer();
  49.  
  50. newCustomer1.customerName = "Smith";
  51. newCustomer1.customerAddress = "River Avanue NW11E London";
  52. newCustomer1.customerAge = 25;
  53. newCustomer1.customerCity = "London";
  54.  
  55. Clist.Add(newCustomer1);
  56.  
  57. Customer newCustomer2 = new Customer();
  58.  
  59. newCustomer2.customerName = "Jhone";
  60. newCustomer2.customerAddress = "No 125/2 Texas US";
  61. newCustomer2.customerAge = 40;
  62. newCustomer2.customerCity = "Texas";
  63.  
  64. Clist.Add(newCustomer2);
  65.  
  66. return Clist;
  67. }
  68.  
  69. }
  70. }

Testing service.

It is very simple. Just press Ctrl + F5 then WCF test client will open. Now you can test you service without implement on server

test client


Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.