如何在客户端.NET应用程序中使用WCF数据服务操作?

4

大家好,我是一个新手,需要一些关于数据服务和LINQ的指导。在短短几天内,我遇到了许多意外的障碍,现在卡在了一个问题上。我希望这些只是学习新工具时典型的挫折。

我有一个WCF数据服务,用于提供来自Sql Server数据库表的GPS坐标数据。特别地,我有一个服务操作方法,允许您指定十进制精度和纬度/经度范围,以生成更通用的数据表示。

在Web浏览器中,它看起来正如预期的那样工作。但是当我尝试从客户端应用程序调用操作时,返回给客户端的列表与服务生成的列表不同。

我将使用我的代码片段来解释细节:

数据服务操作:

    // 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对象组成的列表,但它们都是相同的值。具体来说,重复的值是我期望结果集中的最后一个值。通过在Web浏览器中调用此操作并查看结果,我确认了这一点。 客户端方法:
// 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()是更合适的路线。

1个回答

1

这可能是因为这个原因:

select new
{
    Id = 0,
    Latitude = Math.Round(g.Latitude, decimalPlaces),
    Longitude = Math.Round(g.Longitude, decimalPlaces)
}

我假设 Gps 实体的 Id 属性是您的主键。在您的示例中,您将每个返回的 Gps 的 Id 设置为零。在 WCF 数据服务客户端库中,具有相同主键的实体被视为相同的实例,既为了更改跟踪原因,也为了使对象图在诸如 .NET 等面向对象、引用跟踪的环境中表现出您所期望的行为。

如果由于某种原因您无法为 Gps 实体提供唯一的 Id,请考虑使用 Guid 作为主键。


谢谢您的建议。这个我其实也考虑过,但是我想如果那样的话,在代码返回之前这一部分我的列表看起来就不正确了。但是我会尝试一下并让大家知道结果如何。 - Tim Coolman
你今天是我的英雄!与其更改你指出的代码部分,我更改了将其从匿名类型转换为Gps对象的部分。在此之前,我声明了“long id = 0;”,然后在转换例程中设置了“Id = id ++”,为每个项目赋予了唯一的Id值。再次感谢! - Tim Coolman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接