b

Friday, 18 May 2012

How to host rest wcf service iis 7.0

Build Basic WCF Service.

Step 1)  Create a Class Library (name it as wcftest)
                    Add reference System.ServiceModel  


            add 4 files   1)  Interface file 
                                2) Implementation File
                                3) SVC file
                                4) Web config file
Interface file


File name :  interface1.cs


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

namespace wcftest
{
    [ServiceContract]
    interface InterfaceOne
    {
        [OperationContract]
        [WebGet]
        int add(int x, int y);

        [OperationContract]
         [WebGet]
        string getUserName(string user);
    }
}


Implementation file

file name is intImpl.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Runtime.Serialization;
using System.ServiceModel.Activation;
namespace wcftest
{
    [AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)]
    public class intImpl : InterfaceOne
    {
        public int add(int x, int y)
        {
            return x + y;
        }

        public string getUserName(string user)
        {
            return string.Format("U entered {0} is:",user);
        }
    }
}



Add  SVC file
file name is wcftest.svc

<%@ ServiceHost Service="wcftest.intImpl"  Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>


Add   Web/application config file
file name:  web.config



<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MyserBehaviour">
          <serviceDebug includeExceptionDetailInFaults="true" />
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="WcfService2.Service1" behaviorConfiguration="MyserBehaviour">
        <endpoint contract="WcfService2.IService1" binding="webHttpBinding" bindingConfiguration="myweb"></endpoint>

      </service>
    </services>
    <bindings>
      <webHttpBinding>
        <binding name="myweb">
          <security mode="None">
          </security>
        </binding>
      </webHttpBinding>
    </bindings>
  </system.serviceModel>
</configuration>


Build Class Library-> dll will be produced.



Now hosting WCF Service in IIS 7.0


Step 1)  Runt  Inetmgr.msc from run command


Step 2)   Add new Site as shown below


Step 3)   add site name as mywcfone, create folder with same or different in c:\inetpub\wwwroot\wcfone
Step 4)  Click on Connect as...   button and provide ur system's administrator name and password
               to test connection Click on Test Settings Button...(testing purpose only)
Step 5)  Add port as 8080   or some unused port from 1025-65535. Click OK


Step 6)  Previously Created WCF files copy to c:\inetput\wwwroot\wcfone  folder

Step 7)   create a bin folder in wcfone
                    copy   wcftest.dll to bin folder only (not to debug folder)
                  copy  svc file and   web.config file to    c:\inetput\wwwroot\wcfone  folder.

folders & files
 c:\inetput\wwwroot\wcfone\bin  has wcftest.dll
c:\inetput\wwwroot\wcfone   has  wcftest.svc and web.config files

open browser type http://localhost:8088/WcfTest.svc/add?x=10&y=20
following output will come.


            

No comments:

Post a Comment