如何使用WCF数据服务操作在客户端.NET应用程序? [英] How to consume WCF Data Service Operations in client .NET Application?

查看:177
本文介绍了如何使用WCF数据服务操作在客户端.NET应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的人,我是一个新人,数据服务和LINQ的一些指导的迫切需要。在短短的几天,我遇到过无数意想不到的障碍,我被困在一个现在。我希望这些都是学习新工具只是典型的挫折。

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.

我有一个WCF数据服务,用于从GPS坐标SQL​​ Server数据库表中提供的数据。特别是我有一个服务的操作方法,使您可以指定一个小数点precision和经/纬度范围内产生的数据更通用的重新presentation。

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.

在Web浏览器中它似乎工作一样预期。然而,当我尝试从我的客户端应用程序调用操作时,列表返回给客户端不同于由服务生成的列表

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.

我会用我的code片来解释的细节:

I will use pieces of my code to explain the details:

数据服务操作:

    // 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不同的全球定位系统的对象,其值四舍五入至小数点我指定的列表。

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.

然而,一旦结果在我的客户端应用程序返回到调用方法,我有全球定位系统200的对象的列表,但它们都是相同的值。具体而言,重复的值是在我的预期的结果集的最后一个值。予通过调用该操作在网络浏览器和查看结果证实了这一点。

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.

客户端方式:

// 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;
    }
}

我试图消除了ToList()中的执行()行的一部分,但是当我尝试查看结果在调试器设置,充分显示了读取一个例外,只有一个枚举支持这IEnumerable的。

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."

据我所知,我的客户code是第一嫌疑人。毕竟,每个其它试验表明,我的数据服务操作时产生期望的效果。

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?

据我所知,是一个的createQuery()的选项,但我读过,执行()是此方案更合适的路线。

I understand there is a CreateQuery() option, but I've read that Execute() is the more appropriate route for this scenario.

推荐答案

这可能是因为这个原因:

It's probably because of this:

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

我认为GPS的实体的ID属性是主键。在您的例子中,你正在设置每个标识返回的GPS为零。在WCF数据服务客户端库,具有相同主键的实体将被视为既为更改跟踪的原因相同的实例,使对象图的行为,你会在面向对象的,参考跟踪环境预期,如.NET。

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.

如果由于某种原因,你不能给GPS的实体唯一的ID,可以考虑使用GUID的主键。

If for some reason you can't give the Gps entities a unique Id, consider using a Guid for the primary key.

这篇关于如何使用WCF数据服务操作在客户端.NET应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆