WCF-返回多条记录
上周我创建了一个 asmx Web 服务,它使用soap 返回多行。
我现在转向 WCF,并且也想做同样的事情。
在我的 ASMX Web 服务中,我正在执行以下操作。
public class sample
{
public string Id { get; set; }
public string Name { get; set; }
public string NameOfFile { get; set; }
//public int Distance { get; set; }
}
[WebMethod]
public sample[] Test(int count, float lat, float lng)
{
DataTable dt = new Gallery().DisplayNearestByLatLong(count, lat, lng);
var samples = new List<sample>();
foreach (DataRow item in dt.Rows)
{
var s = new sample();
s.Id = item[0].ToString();
s.Name = item[1].ToString();
s.NameOfFile = item[2].ToString();
//s.Distance = (int)item[3];
samples.Add(s);
}
return samples.ToArray();
}
这段代码工作得很好,但现在我想做同样的事情,但使用 WCF。
我当前的 WCF 文件如下所示(我复制了教程,但设置了数据协定(我认为这是需要的?))
GalleryWebService.cs
public class GalleryWebService : IGalleryWebService
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
return composite;
}
public CompositeType GetTestData()
{
return new CompositeType();
}
}
IGalleryWebService.cs
[ServiceContract]
public interface IGalleryWebService
{
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "test/random")]
CompositeType GetTestData();
}
[DataContract]
public class CompositeType
{
[DataMember]
string _id;
public string Id
{
get { return _id; }
set { _id = value; }
}
[DataMember]
string _name = "Hello";
public string Name
{
get { return _name; }
set { _name = value; }
}
[DataMember]
string _nameoffile = "Hello";
public string NameOfFile
{
get { return _nameoffile; }
set { _nameoffile = value; }
}
}
执行此操作的最佳方法是什么以及如何操作?非常感谢您的帮助!
提前致谢。
Last week I created an asmx web service which returns multiple rows using soap.
I now moving onto WCF and am wanting to do the same.
In my ASMX web service I am doing the following..
public class sample
{
public string Id { get; set; }
public string Name { get; set; }
public string NameOfFile { get; set; }
//public int Distance { get; set; }
}
[WebMethod]
public sample[] Test(int count, float lat, float lng)
{
DataTable dt = new Gallery().DisplayNearestByLatLong(count, lat, lng);
var samples = new List<sample>();
foreach (DataRow item in dt.Rows)
{
var s = new sample();
s.Id = item[0].ToString();
s.Name = item[1].ToString();
s.NameOfFile = item[2].ToString();
//s.Distance = (int)item[3];
samples.Add(s);
}
return samples.ToArray();
}
This code works great but now I want to do the same but using WCF.
My current WCF files look like this (I copied a tutorial but have setup the data contract (which i think is needed?))
GalleryWebService.cs
public class GalleryWebService : IGalleryWebService
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
return composite;
}
public CompositeType GetTestData()
{
return new CompositeType();
}
}
IGalleryWebService.cs
[ServiceContract]
public interface IGalleryWebService
{
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "test/random")]
CompositeType GetTestData();
}
[DataContract]
public class CompositeType
{
[DataMember]
string _id;
public string Id
{
get { return _id; }
set { _id = value; }
}
[DataMember]
string _name = "Hello";
public string Name
{
get { return _name; }
set { _name = value; }
}
[DataMember]
string _nameoffile = "Hello";
public string NameOfFile
{
get { return _nameoffile; }
set { _nameoffile = value; }
}
}
What is the best way to go about doing it and how? Your help is greatly appreciated!
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
WCF 没有太多差异:
默认情况下不设置 DataMember 属性,因此它们在某种程度上是强制性的。
此外,如果您使用
basicHttpBinding
并使用“添加 Web 引用”(而不是服务引用)添加链接 - 您将收到与使用 asmx 服务时相同的结果。WCF has not so many differences:
DataMember
attributes are not set by default, so they are obligatory somehow.Moreover, if you use
basicHttpBinding
and add link using "Add Web reference" (instead of service reference) - you will receive the same result as you would have if you used asmx service.