
[英]Adding Generated Code (wsdl generated) as DataContracts and DataMembers for another WCF service
[英]WCF/REST return group associated with tags, fix datacontracts & datamembers
所以我在下面的每个部分都坚持了一些事情,它们对你们中的大多数人来说都是微不足道的,所以我想知道我是否能得到帮助解决我在下面的代码段中遇到的三个问题,我遇到的问题是:
我的操作合同如下所示:
[OperationContract]
[WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "")]
List<Tag> GetTagCollection();
#endregion
我的数据合同如下所示:
[DataContract(Name="Student")]
public class Student
{
[DataMember(Name = "StudentID")]
public string StudentID { get; set; }
[DataMember(Name = "FirstName")]
public string FirstName { get; set; }
[DataMember(Name = "LastName")]
public string LastName { get; set; }
}
我的服务工作是这样的:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class RawDataService : IReceiveData
{
public List<Group> GetGroupsCollection(string TagName)
{
List<Group> groups = (from g in _program.Groups // _program does not exist notsure what goes here
where
(from t in g.Tags where t.Name == TagName select t).Count() > 0
select g).ToList();
return groups;
}
1.因为GetGroupsCollection
需要一个TagName
参数,所以您需要将其包含在您的 uri 模板中:
/whatever/{TagName}
为了在 WCF REST 调用时将其传递给方法。Uri模板 (msdn)
2.你的合同看起来不错,但是当名字与类/成员名字相同时你不需要名字。 .Net 会为您解决这个问题。 但是不疼...
3.您的服务必须从某个地方提取数据,对吗? 现在,您可以简单地新建一组组以用于测试并将其返回。 数据库连接数,ORM等大概超出了本题的scope。
这应该产生足够的东西来玩:
var tempGroups = new[]{
new Group {
Name = "Hello",
Tags = new[] {
new Tag { Name = "Tag1"},
new Tag { Name = "Tag2"}
}
},
new Group {
Name = "World",
Tags = new[] {
new Tag { Name = "Tag1"},
new Tag { Name = "Tag2"}
new Tag { Name = "Tag3"}
}
}
};
只需将_program.Groups
替换为tempGroups
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.