views:

63

answers:

3

I have a WCF REST service that exposes a method in class GreetService:

[ServiceContract]
public class GreetService
{
    [WebGet(UriTemplate = "greet/{name}")]
    public String GreetName(string name)
    {
        return "Hello " + name;
    }
}

Also, I registered a route in Global.asax:

RouteTable.Routes.Add(new ServiceRoute("GreetService", new WebServiceHostFactory(), typeof(GreetService)));

Now when i run this directly from visual studio, I am able to leverage the UriTemplate and invoke this method using a GET call to http://localhost:5432/GreetService/greet/JohnDoe

However, after deploying this to IIS7 by creating a Greet.svc file for it, I am observing the following behavior:

Any ideas why the WebGetAttribute is not working in IIS? Or is there something else I am doing wrong?

EDIT: This is the ServiceModel part of my web.config file which resides in the directory that IIS uses:

<system.serviceModel>
    <!-- <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> -->
    <standardEndpoints>
      <webHttpEndpoint>
        <!-- 
        Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
        via the attributes on the <standardEndpoint> element below
        -->
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" />
      </webHttpEndpoint>
    </standardEndpoints>
</system.serviceModel>

EDIT 2: For completeness' sake here is my full web.config file:

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="UrlRoutingModule"
           type="System.Web.Routing.UrlRoutingModule,
           System.Web, Version=4.0.0.0,
           Culture=neutral,
           PublicKeyToken=b03f5f7f11d50a3a" />
    </modules>
    <handlers>
      <add name="UrlRoutingHandler"
         preCondition="integratedMode"
         verb="*" path="UrlRouting.axd"
         type="System.Web.HttpForbiddenHandler, 
         System.Web, Version=4.0.0.0, Culture=neutral, 
         PublicKeyToken=b03f5f7f11d50a3a" />
    </handlers>
  </system.webServer>

  <system.serviceModel>
     <!--<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>--> 
    <standardEndpoints>
      <webHttpEndpoint>
        <!-- 
            Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
            via the attributes on the <standardEndpoint> element below
        -->
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" />
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel>

</configuration>
+1  A: 

If you've defined your route to be:

new ServiceRoute("GreetService", .....

then you should be able to call your service at

http://localhost:5432/YourVirtualDirectory/GreetService/greet/JohnDoe

and if your web app is deployed to your IIS root (not in a virtual directory), that would be:

http://localhost:5432/GreetService/greet/JohnDoe

When defining a ServiceRoute, that's done to get rid of having to specify the Greet.svc file, really - the ServiceRoute entry already contains all the information IIS needs to instantiate your service and call it - no need for having the *.svc file involved in your URL (the svc file basically contains the same info your ServiceRoute entry has).

marc_s
I deployed it into it's own website within IIS, not into a virtual directory. I also tried to omit the .svc file and build the URL you mentioned last, but it didn't work. But thanks for confirming this is the way to go, there must be some silly mistake I made.
Masterfu
+1  A: 

Change the line in your global.asax.cs to read:

RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(), typeof(GreetService)));

and put the following in your web.config right under the root <configuration> node:

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="UrlRoutingModule"
           type="System.Web.Routing.UrlRoutingModule, 
          System.Web.Routing, Version=4.0.0.0, 
          Culture=neutral, 
          PublicKeyToken=31BF3856AD364E35" />

    </modules>
    <handlers>
      <add name="UrlRoutingHandler"
         preCondition="integratedMode"
         verb="*" path="UrlRouting.axd"
         type="System.Web.HttpForbiddenHandler, 
         System.Web, Version=4.0.0.0, Culture=neutral, 
         PublicKeyToken=b03f5f7f11d50a3a" />
    </handlers>
  </system.webServer>

(do make sure you're using the right .NET version) and see what that does for you.

Jesse C. Slicer
Nothing, unfortunately - I just can't get it working. I can access a static file that is located in my application directory, though - but it seems as all the routes are missing.The preferred way would be to deploy it as application directly under Default Web Site for example, right?
Masterfu
See if my edit helps. That was a problem with mine.
Jesse C. Slicer
No that didn't help either, but I think this information is important since I found it somewhere else, too, together with the hint to put <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> into the <system.serviceModel> section within the web.config file
Masterfu
+1  A: 

NOTE: please post the web.config, at least the system.servicemodel part.

You normally use either the Route-based configuration or a .svc file, not both, but that's orthogonal to your problem. FWIW, you should be able to kill the .svc file once you get the service working and just use the route.

Since you're able to generate WSDL and call it, that sounds like you might not have webhttp as an endpoint behavior?

Make sure you have an endpoint behavior defined like this (can be a diff name of course)

    <endpointBehaviors> 
        <behavior name="webHttpBehavior"> 
            <webHttp /> 
        </behavior> 
    </endpointBehaviors> 

and then make sure your service endpoint includes behaviorConfiguration="webHttpBehavior"

James Manning
I believe that webHttpEndpoint already includes the webHttp behavior: http://msdn.microsoft.com/en-us/library/ee816921.aspx
Masterfu
I also added my full web.config file now
Masterfu