0

I have a Wcf Rest service

    [OperationContract]
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]       
    void Import(stringrequest);

My try code:

 public void Import(string request)
    {
        if (request != null)
        {
             //....
        }
        else
        {
            throw new ApplicationException("Empty DATA");
        }
    }

In fact, I want to display a specific error message on the else treatment,when I test my wcf srevice in POSTMAN , if I enter a empty string ---> My objectif to display "Empty DATA",

How can do that? Thanks,

IngTun2018
  • 329
  • 1
  • 10

1 Answers1

1

If you want to handle exception you should use try-catch.

When you want to handle all possible behavior, like empty strings or string full of numbers etc.. You can use if-else statement as you are doing.

You need to change your return type from void to string :

public string Import(string request) {

    if (String.IsNullOrEmpty(request)) {
        // ...
        return "{ \"Status\" : \"Ok\" }"; // or null if you don't want to return anything
    }
    else 
    {
        return "{ \"Status\" : \"Error : Empty DATA\" }";    
    }
}

You can also check this link to learn more about how to use Json in C#. How to use http return code and how to return Json, that could help you understand a bit more how WCF REST works.

Hope this helps.

Philippe B.
  • 485
  • 4
  • 18