Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding WCF Restful service to an existing ASP.Net Website

Tags:

c#

.net

asp.net

wcf

I am trying to add a wcf restful service to my existing asp.net 4.0 website (not mvc). I am finding hard time trying to find a good tutorial that walks through in adding one to an existing app/website. Everywhere I see there are notes on creating a new project (with wcf restful template). What are the basic steps? can I do this without an .svc file since I am on .net 4.0? Any tutorial you can point me to?

So far i've tried adding a IService.cs and Service.cs classes that define service contracts and uses webinvoke/webget attributes, added routes in global.asax and service definitions in web.cong as shown in this tutorial http://www.codeproject.com/Articles/255684/Create-and-Consume-RESTFul-Service-in-NET-Framewor but when I point to localhost/restservice, I get 404 not found.

update: This is what I have in my config file. Note that is an existing wcf soap based service.

<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="migrationServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <basicHttpBinding>
        <binding name="migrationHttpBinding" maxBufferSize ="104857600" maxReceivedMessageSize="104857600"/>
      </basicHttpBinding>
    </bindings>
    <services>
      <service name ="Site.Web.WebService.MigrationService" behaviorConfiguration="migrationServiceBehavior">
        <endpoint binding="basicHttpBinding"
                  bindingConfiguration="migrationHttpBinding"
                  contract="Site.Web.WebService.IMigrationService"/>
      </service>
    </services>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
  </system.serviceModel>
like image 554
xoail Avatar asked Apr 10 '12 14:04

xoail


2 Answers

You don't need any *.svc file for a WCF REST service - you can activate it using a ServiceRoute - see RESTful WCF Services with No svc file and No config for a great explanation.

Basically what it boils down to is adding a ServiceRoute to your route table in global.asax:

protected void Application_Start(object sender, EventArgs e)
{
    RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(), 
                                           typeof(YourService)));
}
like image 127
marc_s Avatar answered Sep 20 '22 18:09

marc_s


Not sure whether you can do without svc file.Adding svc file is not abig deal.Just click add new item and YourServiceName.svc .In the sc file enter the following code:-

<%@ServiceHost Service="YourNamsespace.YourWCFservice"%>

.net 4 may come with easy ways to add the svc file directly

public class Global : System.Web.HttpApplication
 {
     protected void Application_Start(object sender, EventArgs e)
  {
         RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(), typeof(YourService)));
   }
 }
like image 28
Krishnanunni Jeevan Avatar answered Sep 21 '22 18:09

Krishnanunni Jeevan