Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF removing .svc extension

I'm able to add a ServiceRoute to my WCF .net 4.0 service with a .svc extension by following these posts:

  1. Post 1
  2. Post 2

However how can i remove the ".svc" extension?

My actual service file is called Catalog.svc

And after following those posts i'm able to access the same service using csw.svc

my Global.asa.cs:

protected void Application_Start(object sender, EventArgs e)
{
    RegisterRoutes(RouteTable.Routes);
}

private void RegisterRoutes(RouteCollection routes)
{
    routes.Add(new ServiceRoute("csw.svc", new WebServiceHostFactory(), typeof(CSW_ebRIM_WebService.Catalog)));
}

my Web.config:

...
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" >
   <serviceActivations>
      <add factory="System.ServiceModel.Activation.ServiceHostFactory"
           relativeAddress="~/csw.svc"
           service="CSW_ebRIM_WebService.Catalog"/>
   </serviceActivations>          
</serviceHostingEnvironment>
<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"/>
   </handlers>
</system.webServer>

However if I remove the .svc extension from both the global and the web.config I get and error saying:

The registered relativeAddress '~/csw' under section 'system.serviceModel/serviceHostingEnvironment/serviceActivations' in configuration file does not have an extension.

If i put back the .svc extension on the web.config and only remove it from the global I get:

Value cannot be null. Parameter name: contract

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentNullException: Value cannot be null. Parameter name: contract

What gives? How can I remove the .svc extension?

like image 377
capdragon Avatar asked Sep 08 '11 21:09

capdragon


1 Answers

Ron Jacobs has a great post on the topic - you need to use a ServiceRoute to achieve what you're looking for:

  • Using System.Web.Routing with Data Services (OData)

As he shows in his follow-up post, WCF Data Services and ServiceRoute, the resulting service document does indeed also reflect the change and shows the service URL without the .svc extension.

This stuff requires the ASP.NET routing support, which is fully present in .NET 4 (and can be bolted on in .NET 3.5 SP1, if needed)

like image 92
marc_s Avatar answered Sep 28 '22 06:09

marc_s