Monday, February 4, 2013

Simple Steps to create the WCF Service



  Before creating WCF Service, you should know the basic understanding about following things,
   1.  ServiceContract 
   2. OperationContract
   3. DataContract
  Creating WCF Service consists 5 main parts, such as,
  a. Create an interface (ServiceContract) with all abstract class(OperationContract)
   b. Create Data Contract with Data Member
   c.  Implement that interface by concrete class
   d. Hosting part of WCF Services
   e. Test the WCF Service with WCF Test Client tool.
   
   Ok, We will look how to do above steps using Visual Studio 2012.
   
  1. Open Visual Studio 2012 as Administrator. and Create new project
  
  2. Select the WCF Service Library as Project template and name it as "CoreService". change the Location if you want. then click "OK".
      
    
  3.   Rename the IService1.cs to IEmployeeService.cs. delete all the code, and paste below code and add the required dlls.
      using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace CoreService
{
   [ServiceContract]
    public interface IEmployeeService
    {
        [OperationContract]
        Employee GetEmployeeByLoginName(string LoginName);

        [OperationContract]
        List<Employee> GetEmployees();   
    }




       [DataContract]
    public class
Employee     

       {
           private string employeeId;
        [DataMember]
        public string
EmployeeId        

           {
            get { return
employeeId; }
            set {
employeeId= value; }
        }


           private string employeeName;
        [DataMember]
        public string
Employee
Name       
           {
            get { return
employee
Name; }
                        set { employeeName= value; }
                }

           private string department;
        [DataMember]
        public string
Department
           {
            get { return
department
;}
                        set   { department= value; }
                }


    }
 } 

 here Service contract and data contract has been added.  

 4. Implement above interface, Rename "Service1.cs" to EmployeeService.cs.
       delete all code and paste below code,

      using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace CoreService
{

     
//Applied to a service to indicate whether that service can be run in ASP.NET compatibility code.
    
      [AspNetCompatibilityRequirements(RequirementsMode =  AspNetCompatibilityRequirementsMode.Allowed)]
   

    public class EmployeeService : IEmployeeService
    {
        public Employee GetEmployeeByLoginName(string loginName)
        {
            //write your logic here , return the Employee Object
        }

        public List<Employee> GetEmployees()
        {
            //write your logic here , return the Employee Object List      
        }
    }
}

  
 5. Now we have created service and it is the time to host that service in IIS. first create WCF Service Application using Visual studio 2012. (New Project --> WCF --> WCF Service Application)
     
    
    Name it as "EmployeeServiceHost" and change the Location if you really want.
  
  6. Add the service project(CoreService) as reference to Hosting Project (EmployeeServiceHost) 

  6. Delete "IService1.cs"  and "Service1.svc.cs" files , those are unnecessary files for hosting.
     
   7. Open "Service1.svc" and delete Code behind part (CodeBehind="Service1.svc.cs").
       Service part has to be changed correctly, first part is namespace of Concrete class and last part is concrete class name (Service="CoreService.EmployeeService" )

  8. Modify the web.config  file of Hosting Project (EmployeeServiceHost ). before that we have to understand about following things,

      Every services can have more than one end points  so that different connection types will be supported for that service. every service must have a name , which is the concrete class name of WCF Service (CoreService.EmployeeService). if it is required , add behaviorConfiguration property for that service.
      Every end point has some important parameters, like "address" , "binding" and "contract". so change it based on your settings.
  •      address - optional - can keep it as empty
  •       binding - binding method - don't change the default  value  if the WCF is not JSON or JSNOP ("webHttpBinding" for JSON),
  •       Contract - refer the Service Contract ("CoreService.IEmployeeService" )

     other than these 3 main parameters , there are 2 more optional parameter, like behaviorConfiguration and bindingConfiguration. when you want to return as JSON or JSONP you will have to specify those values. (Read here for JSON enabled WCF)
   Sample Web.config (for very simple WCF Service)
 
 <services>
    <service name="CoreService.EmployeeService">
      <endpoint address ="" binding="wsHttpBinding" contract="CoreService.IEmployeeService"/>   
      // other end points      
    </service>
   // other services
  </services>

  before publish the service into IIS , we have to make sure that Our Service works properly. so view the Service1.svc in browser . 



 if the service works properly , brower look like,
  if any errors occurs , solve it before publish into IIS.

  9. Now we are ready to publish the Service into IIS. right click in the Hosting Project and click "publish".

     Select the Profile . (Create new profile , if you don't have any),

    
    click Next...

    
    Select the "File System" as Publish Method. then Select the Target Location. Destination URL is optional. click Next...

  
    Select "Debug" as Configuration mode, then click Next...

  
     Check the parameters again before publish into IIS. once you check it, click "Publish".

  10. Open IIS Manager and Create new site for our WCF Service (Click here if you want to know how to create web site in IIS).

  11. Check whether the WCF Service is working or not by using WCF Test Client tool (How to Use WCF Test Client)
  
   

     

7 comments:

Unknown said...

Very useful..you have mentioned from the scratch..

Unknown said...

Links are broken

Anonymous said...

ninest123 16.03
ray ban sunglasses, cheap oakley sunglasses, replica watches, michael kors outlet, oakley sunglasses, jordan shoes, prada handbags, nike outlet, nike air max, nike free, louis vuitton, tiffany and co, ugg boots, louboutin outlet, ray ban sunglasses, louis vuitton outlet, burberry outlet online, michael kors outlet, chanel handbags, uggs on sale, tiffany jewelry, louis vuitton outlet, michael kors outlet, oakley sunglasses, oakley sunglasses, louboutin shoes, prada outlet, ugg boots, ray ban sunglasses, ugg boots, burberry, replica watches, michael kors outlet, tory burch outlet, longchamp outlet, michael kors outlet, louis vuitton, michael kors, polo ralph lauren outlet, longchamp outlet, louis vuitton, polo ralph lauren outlet, louboutin, oakley sunglasses, gucci outlet, ugg boots, christian louboutin outlet, nike air max, longchamp

Anonymous said...

north face, hogan, coach outlet, air force, hollister pas cher, michael kors, coach outlet, lululemon, burberry, sac guess, nike roshe, nike free, true religion jeans, air jordan pas cher, lacoste pas cher, ralph lauren pas cher, michael kors, kate spade outlet, ralph lauren uk, michael kors, new balance pas cher, true religion jeans, ray ban uk, nike roshe run, converse pas cher, michael kors, hermes, mulberry, vans pas cher, louboutin pas cher, true religion outlet, ray ban pas cher, nike air max, oakley pas cher, air max, timberland, nike free run uk, kate spade handbags, nike air max, hollister, tn pas cher, replica handbags, nike air max, vanessa bruno, abercrombie and fitch, sac longchamp, true religion jeans, nike blazer, coach purses, longchamp pas cher, north face

Unknown said...

I definitely love this site
http://www.prokr.net/2016/09/furniture-moving-company-taif.html
http://www.prokr.net/2016/09/furniture-moving-company-hail.html
http://www.prokr.net/2016/09/furniture-moving-company-buraidah.html

5689 said...

zzzzz2018.8.27
basket nike femme
pandora jewelry
cheap snapbacks
pandora outlet
salomon
basket nike
true religion outlet
canada goose jackets
ugg boots clearance
michael kors outlet online

deraz said...

الروضة
شركات صيانة المباني فى الشارقة
تركيب سيراميك في الشارقة