WCF - Overview and Sample Code Snippets in C#


WCF - Overview and Sample Code Snippets in C#

A Fast-Track Overview to WCF



Overview of WCF

- Windows Communication Foundation.
- It combines functionalities of Asp.Net Web services, remoting, MSMQ and Enterprise Services.
- It’s more reliable and secured then Web services.
- All the major changes can be done in the configuration file instead of code file.

Differences with Webservices

Hosting
WCF can be hosted on IIS, WAS, of self-hosting.
WS can be hosted on IIS.

Attributes
WCF has [ServiceContract] attribute attached with the class.
WS has [WebService] attribute attached with the class.
WCF has [OperationContract] attribute attached with methods.
WS has [WebMethod] attribute attached with methods.

XML
WCF uses System.Runtime.Serialization namespace for serialization.
WS uses System.Xml.Serialization namespace for serialization.

Encoding
WCF supports XML, MTOM (Messaging Transmission Optimization Mechanizm),Binary.
WS supports XML, MTOM, DIME (Direct Internet Message Encapsulation).

Transports
WCF supports Http, Tcp, Msmq, P2P transport protocols.
WS supports Http protocol.

Security
WCF provides reliable security for messaging and transactions over net.
WS doesn’t provide method level security & message delivery is’t assured & can be lost without acknowledgement.

3 major Points in WCF

Address
WCF service location that client can use to expose its features

Binding
Configuration for Transport protocol, security and encoding mechanism.

Contract
It defines type of data and type of oprtaions, a client application can use from WCF service.

Service Contract in WCF

It has 2 attributes: ServiceContract & OperationContract


[ServiceContract]
Interface ITestContract
{
[OperationContract]
string MyMethod();
}

Class myService : ITestContract
{
public string myMethod()
{
return "Hello World!!";
}
}



Data Contract in WCF

It has 2 attributes: DataContract & DataMember


[DataContract]
class TestDataContract
{
[DataMember]
public string EmployeeName;

[DataMember]
public DateTime JoiningDate;
}



WCF configurations in config file

It declares information about endpoint name, address, binding and contract.


<system.serviceModel>
<client>
<endpoint name = "MyEndpoint"
address = "http://localhost:8000/MyService/"
binding = "wsHttpBinding"
contract = "IMyContract"
/>
</client>
</system.serviceModel>



WCF proxy

It provides the same operations as that of a Service Contract. There are 2 ways to generate WCF proxies.

In Visual Studio 2008
Right click reference > Add service reference

In Visual Studio - Command Prompt
Using SvcUtil.exe tool


ex: ScvUtil http://localhost/MyWCFService/MyService.svc /out:MyProxy.cs


Fault Contract

It provides the implementation to handle the error raised by WCF
It also propagates the errors to its client applications.

Collection Types Supported by WCF

System.Array
System.Collections.ArrayList
System.Collections.Generics.LinkedList
System.Collections.Generics.List
System.Collections.ObjectModel.Collection
System.ComponentModel.BindingList

Dictionary Collection Types Supported by WCF

System.Collections.Generics.Dictionary
System.Collections.Generics.SortedList
System.Collections.Generics.SortedDictionary
System.Collections.HashTable
System.Collections.ObjectModel.KeyedCollection
System.Collections.SortedList
System.Collections.Specialized.HybridDictionary
System.Collections.Specialized.ListDictionary
System.Collections.Specialized.OrderedDictionary

I hope, I am able to cover main topics about WCF in this article.

Refer the sample code in the attachment.

Regards!



Attachment:


Attachments

  • WCF Service and Test Application (Document File) (36860-71226-WCF-Service-and-Test-Application.doc)
  • Comments

    Author: tanisha07 Feb 2011 Member Level: Bronze   Points : 0

    Nice Article...

    Author: Pal (Parthiv) Patel07 Feb 2011 Member Level: Gold   Points : 0

    hey nice article Mr. sarad kapil.
    articles give clear basic idea about WCF

    Author: navneet chandra22 Mar 2011 Member Level: Bronze   Points : 1

    Hi Mr. Sharad Kapil,

    I am new on WCF, and your article give me clear basic knowledge about WCF.

    Thanks for your nice Article.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: