Search code examples
.netwcfrestwshttpbinding

How can I host a WCF REST service at the service root address (without trailing slash)?


I have a service hosted using wsHttpBinding on an address (the host base address is http://localhost/MyService).

In my contract (IMyService), I have:

    [OperationContract]
    [WebInvoke(
        Method = "GET",
        UriTemplate = "/")]
    Stream GetRootPage();

If I run my service and browse to http://localhost/MyService/, it works fine. If I browse to http://localhost/MyService, I am greeted with a default WCF page indicating that the metadata publishing service is not enabled for this service.

I have tried various combinations of UriTemplate:

  • UriTemplate = ""
  • UriTemplate = "/"
  • UriTemplate = "*"

In combination with the service base address:

All of them yield the same behavior. At some point previously, I could have sworn this was working, though I think I may have been using a subpath in the UriTemplate. I know that if I have a UriTemplate of "/abc/123/", I can browse to /abc/123 (without the trailing slash) because WCF actually issues an HTTP 307 redirect automatically.

I am using .NET 4.0.


Solution

  • I figured out how to do this:

    Ensure that the default WSDL generation is disabled (or at least not using the default URL):

    <serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" />
    

    Ensure that the help URL is disabled:

    <serviceDebug httpHelpPageEnabled="false" httpsHelpPageEnabled="false" />