6

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?

Community
  • 1
  • 1
capdragon
  • 14,565
  • 24
  • 107
  • 153

1 Answers1

6

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

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)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • thanks the post. It is great and simple. I just don't understand what i'm doing wrong because i'm using the ServiceRoute as he describes with no success. – capdragon Sep 09 '11 at 13:16
  • 2
    @capdragon: I see one discrepancy in your code: in your `` section in your config, you're adding the `ServiceHostFactory` for the `.svc` extension - that would indicate a SOAP service (e.g. `basicHttpBinding` or `wsHttpBinding` or something like that). In your `global.asa.cs` however, you're registering the `WebServiceHostFactory` which is the factory used for REST-based service using the `webHttpBinding` ... unfortunately, you're not showing the relevant config sections to show us **what** binding your service is supposed to use .... – marc_s Sep 09 '11 at 13:55
  • 2
    Thanks Mark... that was it, after changing the factory in the global file to match the one in the web.config "ServiceHostFactory", it all works fine now. Thanks again! – capdragon Sep 09 '11 at 14:10