Tuesday, June 1, 2010

WCF:

Is the Microsoft programming model which unifies the existing distributed technologies like(web services,com+,etc….) in a one programming model.

 

When you develop a WCF application it will be a set of services.

Each service has components for clients to communicate and work with These components are called End Points

 

Each end point has 3 attributes (ABC) Address,Binding,Contract

 

  • Binding:HOW to communicate with the service
  • Contract:WHAT the service will offer to clients (will be an interface)

 

To Make WCF service

 

Create an interface which surrounded by [ServiceContract()] attribute Include the methods that will be the behavior of your service, the method that will be presented to service clients must be surrounded with [OperationContract] attribute.Then write a class that implement the interface.

 

ex:
  1. [ServiceContract()]
  2. public interface IWCFHarf
  3. {
  4.                 [OperationContract]
  5.                 Public string SayHello(HarfDataContract data);
  6. }
  7.  
  8. public class WCFHarfService:IWCFHARF
  9. {
  10. String _name=””;
  11.  
  12.     public WFCHarf(HarfDatContract data)
  13.                 {
  14.                                         _name=data.Name;
  15.         }
  16.    
  17.     public string SayHello()
  18.         {
  19.                                 return “Hello” + “  “+_name ;
  20. }
  21. }
  22.  
  23. public class HarfDataContract
  24. {
  25.                 [DataMember]
  26.                 public string Name
  27. {
  28.                                 get{ Return “Ahmed”; }
  29.  
  30.                 }
  31.     }

Making a simple Example

If u have visual studio 2008 installed u will make the project automatically. but if u have vs2005 u must install .Net Framework 0.3 & WCF Extension for VS2005.

Steps:

1.Create a new solution then add a new project of type WCF Service

                This will add these files(Service.svc,web.config,Service.cs)

                Service.cs:Copy and paste previous code in this file

                Service.svc:

 

this file like page header of an aspx file. it specify lang. used, code file path, and service name

 

ex:
  1. <% @ServiceHost Language=C# Debug="true" Service="WCFHarfService" CodeBehind="~/App_Code/Service.cs" %>
  2.             Web.config file:
  3.             <?xml version="1.0"?>
  4.  
  5. <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
  6.   <system.serviceModel>
  7.     <services>
  8.       <service name="MyService" behaviorConfiguration="returnFaults">
  9.         <endpoint contract="IMyService" binding="wsHttpBinding"/>
  10.       </service>
  11.     </services>
  12.     <behaviors>
  13.       <serviceBehaviors>
  14.         <behavior name="returnFaults" >
  15.           <serviceDebug includeExceptionDetailInFaults="true" />
  16. <serviceMetadata httpGetEnabled="true" />
  17.         </behavior>
  18.       </serviceBehaviors>
  19.     </behaviors>
  20.   </system.serviceModel>
  21.   <system.web>
  22.     <compilation debug="true"/>
  23.   </system.web>
  24. </configuration>

 

Explanation of web.config class

Within the <system.serviceModel> we will host Serivces tag. which will has all services we will have in our project or what we want to expose

Within   <Services>

We will host service by service

Each service tag has a name atribute “the name of the service class” and “behaviorConfiguration” that specify name of behaviour tag within serviceBehaviors tag

Each serrvice has endpoint(s) tag(s) as wanted

Each endpoint( “ABC” as before) :has three  attributes Address,Binding, and Contract

  • Address(“Where”): will be “” empty if the service on the localhost.
  • Binding(“How”): binding="wsHttpBinding" we will use http or https for transportation and communication
  • Contract(What”):the Name of the interface

Within <ServiceBehaviours> there are behaviour(s) tag(s)

<serviceDebug includeExceptionDetailInFaults="true" />

 

Each behaviour has name attribute so service can find it as mentioned before

serviceDebug includeExceptionDetailInFaults="true" specify that in occurance of exceptions include details of the exception in the message.

<serviceMetadata httpGetEnabled="true" />

To enable service client to get metadata of the service project assebmly to view and call service contained types

Go to IIS and enable Windows Integration Security on the virtual directory of the WCF project

Press F5 a page with this URL (http://localhost/WCFHarf/WCFHarfService.svc?wsdl ) will be shown to u.

Pressing it will generate this class “WCFHarfServiceClient”

Add a new project of any type say console:

Add web reference type (http://localhost/WCFHarf/WCFHarfService.svc) then click add reference

In the main method type the following

WCFHarfServiceClient client=new WCFHarfServiceClient();

Console.WriteLine(client.SayHello);

 

End of the story;

C# fast-food by:
Eng.Waleed abou-zaid
Senior Software Developer
Harf Information Technology
MCSD.Net,MCAD.Net,MCP,A+,N+ certified
walid.abouzaid@gmail.com



1 comments:

IP Access Control Miami said...

Loved this post. Thanks for sharing your day!