2

I have an IIS 8.5 running a RESTful ASP.NET 4.5 WCF service as application on Windows Server 2012 R2.

During maintenance of the service, I want all requests to the WCF Service (https://server/path/service.svc/whatever/path) to result in a custom error code (e.g. 555) and a static JSON response (static content and correct Content-Type header). As there should be some automatically triggered maintenance, it would be great if this IIS behaviour could be triggered programatically.

Do you know if there is some way to temporarily deliver static JSON content for a specific URL (and all REST-ful subpaths, see example URL above), and additionally use a custom 5xx error code (instead of 200) as HTTP response code?

muffel
  • 342
  • 7
  • 21

1 Answers1

2

You are asking two questions, how to do this and how to script what you want to do.

I don't know whether its possible out of the box, but one solution is to use the IIS UrlRewrite module, this is very handy for all kinds of things, so I think its a good idea to have it anyways.

Create a new rule that matches all the requests that you want to handle, in your case path/service.svc/whatever/path, you can use regular expressions to match your requests. Use a custom response and send your own status code. In the web.config you should have a section like this:

<rewrite>
  <rules>
    <rule name="Json Redirect" stopProcessing="true">
      <match url="path/service.svc/whatever/path" />
      <action type="CustomResponse" statusCode="555" subStatusCode="0" statusReason="Nothing to see here" statusDescription="Temporary static json..." />
    </rule>
  </rules>
</rewrite>

If you just do that, you get the error code, but not your static json. Add a new entry under system.webServer-httpErrors in web.config:

<error statusCode="555" subStatusCode="0" path="temp.json" responseMode="File" />

The responseMode = file means that the static file location is relative to your web root, in the case above right in the root of your web site.

Now you have to fix the content type, add an entry under system.webServer-staticContent in your web.config:

<mimeMap fileExtension=".json" mimeType="text/json" />

When testing this locally, you may still get an IIS error page, but calling it remotely, it should work.

You can do all these changes in the IIS manager UI, but if you want to script them, look into the PowerShell IIS cmdlets.

Peter Hahndorf
  • 14,058
  • 3
  • 41
  • 58
  • great, thank you for this detailed answer! Keeping the settings in the web.config is enough *scriptability* for me and works just great for what I want to achieve – muffel Jan 19 '15 at 09:37