• We just launched and are currently in beta. Join us as we build and grow the community.

How To Create WCF Service

ksa1

Mobile SEO Guru
K Rep
0
0
0
Rep
0
K Vouches
0
0
0
Vouches
0
Posts
158
Likes
187
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
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



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

custservice.png


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. namespace

    EchoService
  7. {
  8. [

    DataContract]
  9. public

    class

    Customer
  10. {

  11. [

    DataMember]
  12. public

    string

    customerName {

    get

    ;

    set

    ;

    }

  13. [

    DataMember]
  14. public

    string

    customerAddress {

    get

    ;

    set

    ;

    }

  15. [

    DataMember]
  16. public

    int

    customerAge {

    get

    ;

    set

    ;

    }

  17. [

    DataMember]
  18. public

    string

    customerCity {

    get

    ;

    set

    ;

    }

  19. }
  20. }


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. namespace

    EchoService
  8. {
  9. // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "ICustomerService" in both code and config file together.
  10. [

    ServiceContract]
  11. public

    interface

    ICustomerService
  12. {
  13. [

    OperationContract]
  14. string

    GetCustomerName(

    string

    name)

    ;

  15. [

    OperationContract]
  16. string

    GetCustomerAddress(

    string

    address)

    ;

  17. [

    OperationContract]
  18. Customer GetCustomerDetails(

    )

    ;

  19. [

    OperationContract]
  20. List<

    Customer>

    GetCustomerS(

    )

    ;


  21. }
  22. }

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. namespace

    EchoService
  8. {
  9. // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "CustomerService" in code, svc and config file together.
  10. public

    class

    CustomerService :

    ICustomerService
  11. {

  12. public

    string

    GetCustomerName(

    string

    name)
  13. {

  14. return

    string

    .

    Format

    (

    "Customer Name Is : {0}"

    , name)

    ;

  15. }

  16. public

    string

    GetCustomerAddress(

    string

    address)
  17. {

  18. return

    string

    .

    Format

    (

    "Customer Address Is : {0}"

    , address)

    ;

  19. }


  20. public

    Customer GetCustomerDetails(

    )
  21. {

  22. Customer newCustomer =

    new

    Customer(

    )

    ;

  23. newCustomer.

    customerName

    =

    "Smith"

    ;
  24. newCustomer.

    customerAddress

    =

    "River Avanue NW11E London"

    ;
  25. newCustomer.

    customerAge

    =

    25

    ;
  26. newCustomer.

    customerCity

    =

    "London"

    ;

  27. return

    newCustomer;

  28. }


  29. public

    List<

    Customer>

    GetCustomerS(

    )
  30. {
  31. List<

    Customer>

    Clist =

    new

    List<

    Customer>

    (

    )

    ;

  32. Customer newCustomer1 =

    new

    Customer(

    )

    ;

  33. newCustomer1.

    customerName

    =

    "Smith"

    ;
  34. newCustomer1.

    customerAddress

    =

    "River Avanue NW11E London"

    ;
  35. newCustomer1.

    customerAge

    =

    25

    ;
  36. newCustomer1.

    customerCity

    =

    "London"

    ;

  37. Clist.

    Add

    (

    newCustomer1)

    ;

  38. Customer newCustomer2 =

    new

    Customer(

    )

    ;

  39. newCustomer2.

    customerName

    =

    "Jhone"

    ;
  40. newCustomer2.

    customerAddress

    =

    "No 125/2 Texas US"

    ;
  41. newCustomer2.

    customerAge

    =

    40

    ;
  42. newCustomer2.

    customerCity

    =

    "Texas"

    ;

  43. Clist.

    Add

    (

    newCustomer2)

    ;

  44. return

    Clist;
  45. }

  46. }
  47. }

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

testclient.png


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.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top