クライアントコードを実行しようとしても何も返されないため、GET メソッドにバグがあると思います。
私の GET 操作コントラクトは次のようになります。
[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;
}
これをテストするためのデータがないため、クライアント側からグループとタグを手動で追加する必要があります。次に、グループにタグを追加しようとすると、次のようになります。
[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 });
}
}
そして、クライアントからこれは次のように行われます:
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();
}
返されるメッセージは問題なく、すべて問題ないようです。
今、私が問題を抱えているクライアントコードの一部はこれです:
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;
}
これは、最初に述べた GET 操作に関連しています。それで、クライアントコードが何か間違っているのか、それとも私の実際のGetGroupsCollection
方法なのかを調べる方法がわかりませんか?
したがって、私の問題はグループへのタグの追加に関連しています:
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 });
}
}
または、クライアント側のコードに関連していますGetGroupsCollection
か?
以前にどのサーフェンが解決したか (404 エラー) の小さなエラーを反映するように質問を更新しましたが、何も返されないという問題は解決していませんか?