如何在客户端.NET应用程序中使用WCF数据服务操作?
好吧,伙计们,我是数据服务和 LINQ 的新手,迫切需要一些指导。短短几天内,我遇到了无数意想不到的障碍,现在我被困在其中一个了。我希望这些只是学习新工具时遇到的典型挫折。
我有一个 WCF 数据服务,用于从 Gps 坐标的 Sql Server 数据库表提供数据。特别是,我有一个服务操作方法,允许您指定小数精度和纬度/经度范围以生成更通用的数据表示。
在网络浏览器中,它似乎按预期工作。但是,当我尝试从客户端应用程序调用该操作时,返回给客户端的列表与服务生成的列表不同。
我将使用我的代码片段来解释详细信息:
数据服务操作:
// This is my service operation that I need to call from my client app (see below).
// It should return an IEnumerable<Gps> (Gps is one of my Entity Model
// types) list of distinct GPS rounded to the number of decimal positions
// specified and within the range specified.
[WebGet]
public IEnumerable<Gps> GetGpsView(int decimalPlaces, decimal minLatitude, decimal minLongitude, decimal maxLatitude, decimal maxLongitude)
{
// I must first return a list of anonymous-type objects
// because LINQ does not seem to allow me to construct my
// Gps object within the query (one of those other issues
// I had to tip-toe around).
var list = (from g in this.CurrentDataSource.Gps
where g.Latitude >= minLatitude &&
g.Latitude <= maxLatitude &&
g.Longitude >= minLongitude &&
g.Longitude <= maxLongitude
select new
{
Id = 0,
Latitude = Math.Round(g.Latitude, decimalPlaces),
Longitude = Math.Round(g.Longitude, decimalPlaces)
}).Distinct().ToList();
// Now that I have my results, I need to convert the items in the
// list to my Gps entity object.
IEnumerable<Gps> gpsList = list.ConvertAll<Gps>(item => new Gps
{
Id = item.Id,
Latitude = item.Latitude,
Longitude = item.Longitude
});
return gpsList;
}
如果我在从客户端应用程序调用时调试上述方法(在 Visual Studio 的虚拟服务器上运行它),gpsList 似乎包含在返回给客户端之前正确的数据。使用我的测试参数,我获得了 200 个不同 Gps 对象的列表,其值四舍五入到我指定的小数位。
然而,一旦结果返回到我的客户端应用程序中的调用方法,我就有了 200 个 Gps 对象的列表,但它们都是相同的值。具体来说,重复的值是我预期结果集中的最后一个值。我通过在网络浏览器中调用此操作并查看结果来确认这一点。
客户端方法:
// Partial class extension of code auto-generated by service reference.
public partial class HsiSideBySideEntities
{
public List<Gps> GetGpsView(int decimalPlaces, decimal minLatitude, decimal minLongitude, decimal maxLatitude, decimal maxLongitude)
{
this.IgnoreMissingProperties = true;
// Format my relative URI string.
string uri = string.Format("/GetGpsView?decimalPlaces={0}&minLatitude={1}M&minLongitude={2}M&maxLatitude={3}M&maxLongitude={4}M", decimalPlaces, minLatitude, minLongitude, maxLatitude, maxLongitude);
// If I debug both client and service at the same time, when I step over this
// line, it does reach my data service - and as I mentioned above, on the
// service end it appears to generate the correct results.
List<Gps> gpsList = this.Execute<Gps>(new Uri(uri, UriKind.Relative)).ToList();
// However, the results are returned to the client code, my list contains
// duplicates of the last expected record.
return gpsList;
}
}
我尝试删除“Execute()”行的“ToList()”部分,但是当我尝试在调试器中查看结果集时,它显示一个异常,内容为“此 IEnumerable 仅支持单个枚举。”
据我所知,我的客户端代码首先受到怀疑。毕竟,所有其他测试都表明我的数据服务操作正在产生预期的结果。
我是否需要执行其他操作才能从数据服务获取 IEnumerable 对象列表?
我知道有一个 CreateQuery() 选项,但我读到 Execute() 是这种情况下更合适的途径。
Alright folks, I am a newcomer to Data Services and LINQ in desperate need of some guidance. In just a few days I have encountered numerous unexpected obstacles and I'm stuck on one now. I'm hoping these are just typical frustrations of learning a new tool.
I have a WCF Data Service for serving up data from a Sql Server database table of Gps coordinates. In particular I have a service operation method that allows you to specify a decimal precision and latitude/longitude range to produce a more general representation of the data.
In a web browser it appears to work just as expected. However when I try to call the operation from my client application, the list returned to the client differs from the list generated by the service.
I will use pieces of my code to explain the details:
Data Service Operation:
// This is my service operation that I need to call from my client app (see below).
// It should return an IEnumerable<Gps> (Gps is one of my Entity Model
// types) list of distinct GPS rounded to the number of decimal positions
// specified and within the range specified.
[WebGet]
public IEnumerable<Gps> GetGpsView(int decimalPlaces, decimal minLatitude, decimal minLongitude, decimal maxLatitude, decimal maxLongitude)
{
// I must first return a list of anonymous-type objects
// because LINQ does not seem to allow me to construct my
// Gps object within the query (one of those other issues
// I had to tip-toe around).
var list = (from g in this.CurrentDataSource.Gps
where g.Latitude >= minLatitude &&
g.Latitude <= maxLatitude &&
g.Longitude >= minLongitude &&
g.Longitude <= maxLongitude
select new
{
Id = 0,
Latitude = Math.Round(g.Latitude, decimalPlaces),
Longitude = Math.Round(g.Longitude, decimalPlaces)
}).Distinct().ToList();
// Now that I have my results, I need to convert the items in the
// list to my Gps entity object.
IEnumerable<Gps> gpsList = list.ConvertAll<Gps>(item => new Gps
{
Id = item.Id,
Latitude = item.Latitude,
Longitude = item.Longitude
});
return gpsList;
}
If I debug the above method (running it on Visual Studio's virtual server) when called from my client app, gpsList appears to contain the proper data just before returning to the client. Using my test parameters, I get a list of 200 distinct Gps objects whose values are rounded to the decimal places I specify.
However, once the results are returned to the calling method in my client app, I have a list of 200 Gps objects, but they are all the SAME value. To be specific, the duplicated value is the LAST value in my expected result set. I confirmed this by calling this operation in a web browser and viewing the results.
Client Method:
// Partial class extension of code auto-generated by service reference.
public partial class HsiSideBySideEntities
{
public List<Gps> GetGpsView(int decimalPlaces, decimal minLatitude, decimal minLongitude, decimal maxLatitude, decimal maxLongitude)
{
this.IgnoreMissingProperties = true;
// Format my relative URI string.
string uri = string.Format("/GetGpsView?decimalPlaces={0}&minLatitude={1}M&minLongitude={2}M&maxLatitude={3}M&maxLongitude={4}M", decimalPlaces, minLatitude, minLongitude, maxLatitude, maxLongitude);
// If I debug both client and service at the same time, when I step over this
// line, it does reach my data service - and as I mentioned above, on the
// service end it appears to generate the correct results.
List<Gps> gpsList = this.Execute<Gps>(new Uri(uri, UriKind.Relative)).ToList();
// However, the results are returned to the client code, my list contains
// duplicates of the last expected record.
return gpsList;
}
}
I tried removing the "ToList()" portion of the "Execute()" line, but when I try to view the result set in the debugger, it shows an exception that reads, "Only a single enumeration is supported by this IEnumerable."
As far as I can tell, my client code is first suspect. After all, every other test shows that my data service operation is producing the desired results.
Do I need to do something different to obtain the IEnumerable list of objects from the data service?
I understand there is a CreateQuery() option, but I've read that Execute() is the more appropriate route for this scenario.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能是因为这样:
我假设 Gps 实体的 Id 属性是您的主键。在您的示例中,您将每个返回的 Gps 的 ID 设置为零。在 WCF 数据服务客户端库中,具有相同主键的实体被视为同一实例,这既是出于更改跟踪的原因,也是为了使对象图的行为符合您在面向对象、引用跟踪的环境(例如 .NET)中的预期。
如果由于某种原因您无法为 Gps 实体提供唯一的 ID,请考虑使用 Guid 作为主键。
It's probably because of this:
I assume that the Id property of the Gps entity is your primary key. In your example you are setting the Id of each returned Gps to zero. In the WCF Data Services client library, entities with the same primary key are treated as the same instance both for change tracking reasons and so that object graphs behave as you would expect in an object-oriented, reference-tracked environment such as .NET.
If for some reason you can't give the Gps entities a unique Id, consider using a Guid for the primary key.