G Gr
G Gr

Reputation: 6090

client side GET request not returning anything?

I think something buggy is up with my GET method as I am getting nothing returned when I try to run a piece of client code.

My GET operation contract looks like this:

    [OperationContract] 
    [WebInvoke(Method = "GET", 
    BodyStyle = WebMessageBodyStyle.Bare, 
    RequestFormat = WebMessageFormat.Xml, 
    ResponseFormat = WebMessageFormat.Xml, 
    UriTemplate = "/Group/{TagName}")]
    List<Group> GetGroupsCollection(string TagName);

    public List<Group> GetGroupsCollection(string TagNames)
    {
        List<Group> groups = (from g in Groups 
                where
                    (from t in g.Tags where t.TagName == TagNames select t).Count() > 0
                select g).ToList();
    return groups;
    }

Now I dont have any data to test this with so I have to manually add groups and tags from my client side, I then attempt to add a tag to a group and I do this like so:

    [OperationContract]
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/AddTagtoGroup/{group}/{tag}")]
    void AddTagtoGroup(string group, string tag);

    public void AddTagtoGroup(string group, string tag)
    {
        var result = Groups.Where(n => String.Equals(n.GroupName, tag)).FirstOrDefault();
        if (result != null)
        {
            result.Tags.Add(new Tag() { TagName = tag });
        }
    }  

And from client this is done like this:

    private void AddTagetoGroup_Click(object sender, EventArgs e)
    {
        string uriAddTagtoGroup = string.Format("http://localhost:8000/Service/AddTagtoGroup/{0}/{1}", textBox6.Text, textBox7.Text);
        byte[] arr = Encoding.UTF8.GetBytes(uriAddTagtoGroup);
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uriAddTagtoGroup);
        req.Method = "POST";
        req.ContentType = "application/xml";
        req.ContentLength = arr.Length;
        Stream reqStrm = req.GetRequestStream();
        reqStrm.Write(arr, 0, arr.Length);
        reqStrm.Close();
        HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
        MessageBox.Show(resp.StatusDescription);
        reqStrm.Close();
        resp.Close();
    }

The message I get back is OK and all seems fine.

Now the piece of client code I get a problem with is this:

    string uriGetGroupsCollection = "http://localhost:8000/Service/GetGroupsCollection/{TagName}";
    private void button8_Click(object sender, EventArgs e)
    {
        string tagUri = uriGetGroupsCollection.Replace("{TagName}", textBox8.Text);

        XDocument xDoc = XDocument.Load(tagUri); //this line gives 404 error not found.
        var Tag = xDoc.Descendants("Group")
            .Select(n => new
            {
                Tag = n.Element("GroupName").Value,
            })
            .ToList();
        dataGridView3.DataSource = Tag;
    }

Which is related to the GET operation I first mentioned. So I am unsure how to find out if its the client code doing something wrong or my actual GetGroupsCollection method?

So either my problem is related to adding a tag to a group:

    public void AddTagtoGroup(string group, string tag)
    {
        var result = Groups.Where(n => String.Equals(n.GroupName, tag)).FirstOrDefault();
        if (result != null)
        {
            result.Tags.Add(new Tag() { TagName = tag });
        }
    }  

Or its related to the client side code for GetGroupsCollection?

I updated my question to reflect the small error I was previously getting which surfen solved (404 error) but this hasnt solved my problem of not getting anything back?

Upvotes: 2

Views: 179

Answers (1)

surfen
surfen

Reputation: 4692

I think that you've made a mistake in your URL:

string uriGetGroupsCollection = "http://localhost:8000/Service/GetGroupsCollection/{TagName}";

since you defined your URITemplate like this: "/Group/{TagName}"

[OperationContract] 
[WebInvoke(Method = "GET", 
BodyStyle = WebMessageBodyStyle.Bare, 
RequestFormat = WebMessageFormat.Xml, 
ResponseFormat = WebMessageFormat.Xml, 
UriTemplate = "/Group/{TagName}")]
List<Group> GetGroupsCollection(string TagName);

So your URL in the client should look like this:

string uriGetGroupsCollection = "http://localhost:8000/Service/Group/{TagName}";

OR change your URITemplate to:

UriTemplate = "/GetGroupsCollection/{TagName}")]

UPDATE

Your AddTagtoGroup has another typo.

    var result = Groups.Where(n => String.Equals(n.GroupName, tag)).FirstOrDefault();

should be:

    var result = Groups.Where(n => String.Equals(n.GroupName, group)).FirstOrDefault();

Upvotes: 2

Related Questions