Question

I've got a simple web application which has a single WCF service in the root folder:

+AppRoot
---MyService.svc

This is being modified to include a lot of new functionality, and I've split these into two separate services in different folders to represent their version:

+AppRoot
---v1
-----MyService.svc
---v2
-----MyService.svc

However, the existing consumers of this service still using v1 need to be able to access the v1 service the old URL - eg. http://services.example.com/MyService.svc.

I'd like to keep these services physically separated in their respective v1 and v2 folders, while transparently routing clients trying to access the old URL to the v1 service (instead of having v1 in the root and v2 in it's own folder).

I've tried setting a route in Global.asax:

RouteTable.Routes.MapPageRoute("MyServiceV1", "MyService.svc", "~/v1/MyService.svc");

Also tried setting the service endpoint:

<endpoint address="/MyService.svc"
          binding="webHttpBinding"
          contract="MyApp.V1.IMyService"
          behaviorConfiguration="web" />

Neither of these seemed to work.

Is there any other way to do this?

Was it helpful?

Solution

Was trying to avoid URL rewriting as I was hoping there was a simpler way to achieve this using built in functionality, but this turned out to be the easiest way to resolve the issue.

First I had to install the IIS add-in for URL rewriting. This can be done by downloading URL Rewrite from the Microsoft website, or installing it via Web Platform Installer.

Once that was done, I added the following to web.config in my WCF service web application.

<system.webServer>
    <!-- Other stuff here -->
    <rewrite>
        <rules>
            <!-- Rewrite requests to /MyService.svc to /v1/MyService.svc -->
            <rule name="MyService v1" stopProcessing="true">
                <match url="MyService.svc(.*)" />
                <conditions logicalGrouping="MatchAll">
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
                </conditions>
                <action type="Rewrite" url="/v1/MyService.svc{R:1}" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

This allows the v1 service to be accessed via both /MyService.svc and /v1/MyService.svc, and v2 can be accessed via /v2/MyService.svc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top