Bryan Lewis
Bryan Lewis

Reputation: 5978

Adding Routing to eliminate .SVC in REST service isn't working

I am building a dummy WCF REST service just for purposes of learning how it works (preparing for real service build). I have the REST service working and responding with both JSON and POX formatting. However, I cannot get the routing solution to work in order to eliminate the ".svc" file. I am using VS 2010, WCF 4.0 and IIS 7.5 on Win Server 2008 R2.

Right now the URL works as: "/api/rest/rest.svc/json/myMethod" but I want to just have "/api/rest/json/myMethod". I have found numerous articles here on SO and elsewhere which claims to remove the ".svc" file. I believe I have it setup as instructed, but the project will not build because of an error in the Global.asax file.

It says to add the following to the Application_Start function:

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

I also added the following to the web.config:

 <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>

I also added the aspNetCompatibility lines to the web.config and above the class in the svc.cs file.

Th issue is that I can't even get the project to build. When I add the RouteTable.Routes.Add line to the global.asax and build it, I get the following errors:

The type 'System.ServiceModel.Activation.ServiceHostFactory' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.ServiceModel.Activation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

The type or namespace name 'ServiceRoute' could not be found (are you missing a using directive or an assembly reference?)

Any ideas why this is failing?

Upvotes: 2

Views: 8131

Answers (1)

larsw
larsw

Reputation: 3830

make sure that "System.ServiceModel.Activation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" is referenced in the project, if it is make sure to add this to the web.config file as well. – Joakim Mar 25 at 19:03

Upvotes: 5

Related Questions