Search code examples
c#stringwcfgetassert

WCF self hosted service, parameter (string) assertion fails


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.


Solution

  • 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