0

I have a self hosted wcf service, it has three OperationContract's, two of those should accept parameters

 [OperationContract]
 [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Json,
            UriTemplate = "json/{id}")]
    string JSONData(string id);

Based on this parameter i execute some logic and return data :

public string JSONData(string id)
    {
        if (id == "1")
        {
            string json = "{ \"months\":[{ \"name\":\"January\"},  { \"name\":\"Febuary\"},  { \"name\":\"March\"}  ]}";
            return json;
        }
        else
        {
            return "Id not found, invalid request";
        }
    }

I do this request using postman :

enter image description here

However, it doesn't matter what i fill in as id, i always get :

"Id not found, invalid request"

To add to my confusion the following does work :

 public string JSONData(string id)
    {
        return Data(id);
    }

    private string Data(string id)
    {
        // logic
        return "Data: " + id;
    }

Which leads me to believe that its not so much the input failing as much as the asserting in the if statement.

Having changed the logic to :

  public string JSONData(string id)
    {
        if (id.Equals("name"))
        {
            return "equal";
        }
        else { 
        return Data(id);
        }
    }

    private string Data(string id)
    {
        // logic
        return "Data: " + id;
    }

The assertion in the if statement is still not being done, however i do not see what i am doing wrong in asserting the equality of a string in this fashion.

Help would be appreciated.

Thanks.

Delano zuurman
  • 234
  • 2
  • 11
  • are you sure by doing this { \"months\":[{ \"name\":\"January\"}, ... etc is a correct way to create a json? from what I have been doing is to parse by using serializer or something, what are you doing is better to use a MVC. but in your case this is selft hosted right so it's okay. Just create classes and call them as a List, then you use JsonConvert – AbbathCL Oct 02 '20 at 18:23
  • Thanks for your suggestion, this was actually just a test json that i was trying to return to see if everything was working as i expected. For some reason I couldn’t get the assertion to correctly work. But after several other issues i decided to delete everything and start over. – Delano zuurman Oct 04 '20 at 13:17

1 Answers1

1

I don't know what you want to accomplish but using wcftestclient I can return what you asked.

WcfTest

This is how you call it in code This is how you call it in code

Check this page Answer https://stackoverflow.com/a/18589783/13760086

AbbathCL
  • 79
  • 6
  • The issue for me was that i couldn't get it to work using postman, after many tries and testing and of course a lot of googling i found out that most of my assumptions where faulty and that while is was trying to create a restful service i was actually working with a soap example muddling it with restful examples. After a lot of trial and error i have found what i needed. I wil however flag your answer as correct. I went back to see if it worked and it did. – Delano zuurman Oct 04 '20 at 13:19
  • Yeah I guess what you was trying to do is a Rest API, and that is a different solution project. I would recommend you to use .Net Core. Don't go by asmx service that's legacy product, you could try MVC or Rest, grPC or something else. – AbbathCL Oct 05 '20 at 13:43