1

I am working on WCF application which is running fine in production. It has lots of service methods. Now there is requirement that if one of the field in config file is set to true then it must show some custom message to consumer.

I created constructor to make sure it will be called for each request and checked for that config key there and thrown FaultException as below

public RESTService()
{
    if (ConfigurationManager.AppSettings("BlockLogin") == "1")
        throw new FaultException("Application under maintainance");
}

but it is not giving response in JSON. Instead it returns it in html format as below

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Request Error</title>
        <style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
    </head>
    <body>
        <div id="content">
            <p class="heading1">Request Error</p>
            <p xmlns="">The server encountered an error processing the request. Please see the 
                <a rel="help-page" href="http://172.16.3.156:81/_RestAPI/RESTService.svc/help">service help page</a> for constructing valid requests to the service.
            </p>
        </div>
    </body>
</html>
Imad
  • 7,126
  • 12
  • 55
  • 112
  • I don't think, that using `Exception` in current situation is a good idea. There is nothing exceptional happening, you're just want to notify user that application under maintenance. Just create an error response. – SᴇM Oct 30 '18 at 05:51
  • @SeM How can I do that? Is contructor idea correct? – Imad Oct 30 '18 at 05:53
  • @Imad please refer this – Ashish Kamble Oct 30 '18 at 05:54
  • You said in your question that this project is `WPF`, but tagget `WCF`, so is it one or both? – SᴇM Oct 30 '18 at 05:57
  • @SeM sorry it's WCF only. I will update – Imad Oct 30 '18 at 05:58
  • @AshishKamble I thought of that first but I will end up modifying all operations and it will be too time consuming. That would be my last solution – Imad Oct 30 '18 at 05:59
  • @Imad Yes true but we like to build enough flexible software for future modification, Right – Ashish Kamble Oct 30 '18 at 06:05
  • I've not done wcf projects for a while now, but take a look at [this post](https://stackoverflow.com/questions/3230160/turn-off-wcf-soap-service-for-maintenance-and-provide-friendly-message). If you use throwing exception behavior, your clients should be aware of that and be ready to handle. – SᴇM Oct 30 '18 at 06:06

1 Answers1

0

You don't need to use exception - as you saw WCF will convert it to a 500 server error, and not give you control.

You could create a class (plain class) called (for example) BusinessFault:

public class BusinessFault
{
 public string ErrorCode { get; set; }
 public string ErrorMessage { get; set; }
}

Then, when you would like to return a fault:

if (ConfigurationManager.AppSettings("BlockLogin") == "1")
{
  return new BusinessFault() {
    ErrorCode = "0001",
    ErrorMessage = "Application under maintainance"
  }
}
Russell
  • 17,481
  • 23
  • 81
  • 125
  • Then you return it as you normally would return an object to the consumer. Note this still returns an HTTP error code of 200. – Russell Oct 30 '18 at 06:26
  • Thanks for the answer. Can I implement it in a way that it should get automatically called when service is requested. I want to keep all operations as it is, if possible. – Imad Oct 30 '18 at 06:26