【问题标题】:Consuming Odata Service on windows phone 8 on the Localhost在 Localhost 上的 windows phone 8 上使用 Odata 服务
【发布时间】:2015-03-07 23:45:24
【问题描述】:

我能够从本地主机在我的 Windows Phone 应用程序上添加我的 Odata 服务服务引用,但是当我知道当我使用 Northwind 服务引用时我可以获取数据时,我无法获取数据集合需要的数据,我什至检查了服务版本,我认为这是兼容性的问题?但我仍然不确定这里发生了什么,因为我没有任何错误。我正在尝试使用提琴手获得服务响应,但我不知道我的方向是否正确,请帮忙???我在 Visual Studio 2010 上使用 Odata 服务 V3,我正在尝试从 IIS(本地主机)上的 VS express 2012 中使用它。!!! :(((同一个项目在服务器和本地主机上的 LinqPad4 上运行良好)

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using TestConsumeWebService.Resources;
using TestConsumeWebService.Northwind;
using System.Data.Services.Client;
using TestConsumeWebService.DataService;

namespace TestConsumeWebService
{
    public partial class MainPage : PhoneApplicationPage
    {
        private static readonly Uri _rootUri =
            new Uri("http://localhost:88/DataService.svc/");

        //// Define the typed DataServiceContext.
        //private DataEntities _context;

        // Define the binding collection for Customers.
        private DataServiceCollection<Table> _customers;

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            Loaded += new RoutedEventHandler(MainPage_Loaded);

        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
           var _context = new DataEntities(_rootUri);
           Customers = new DataServiceCollection<Table>(_context);

            // Specify an OData query that returns all customers.
            var query = from cust in _context.Table
                        select cust;

            // Load the customer data.
            Customers.LoadAsync(query);
        }
        public DataServiceCollection<Table> Customers
        {
            get { return _customers; }

            private set
            {
                // Set the Titles collection.
                _customers = value;

                // Register a handler for the LoadCompleted callback.
                _customers.LoadCompleted += OnCustomersLoaded;

                // Raise the PropertyChanged events.
            }
        }


        // Handles the DataServiceCollection<T>.LoadCompleted event.
        private void OnCustomersLoaded(object sender, LoadCompletedEventArgs e)
        {
            // Make sure that we load all pages of the Customers feed.
            if (Customers.Continuation != null)
            {
                Customers.LoadNextPartialSetAsync();
            }

        }

    }
}

这是我的 Odata 服务代码:

public class DataService : DataService<DataEntities>
    {
        // This method is called only once to initialize service-wide policies.
        public static void InitializeService(DataServiceConfiguration config)
        {
            // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
            // Examples:
            config.UseVerboseErrors=true;
             config.SetEntitySetAccessRule("*", EntitySetRights.All);
             config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
        }
}

【问题讨论】:

  • 请向我们展示您的代码以获得帮助。
  • 好的,我已经添加了我的代码!请帮忙!!谢谢!

标签: c# visual-studio-2010 windows-phone-8 wcf-data-services


【解决方案1】:

根据此 API 的 cmets。此 API 仅适用于 silverlight。

    // Summary:
    //     Asynchronously loads the collection by executing a System.Data.Services.Client.DataServiceQuery<TElement>.Supported
    //     only by the WCF Data Services 5.0 client for Silverlight.

为了在 Windows phone 中查询客户。你可以试试下面的代码。

       var _customers = new DataServiceCollection<Table>(_context);

        // Specify an OData query that returns all customers.
        var query = (from cust in _context.Customer
                    select cust)
                    as DataServiceQuery<Table>;

        var ar = query.BeginExecute(null, null);
        ar.AsyncWaitHandle.WaitOne();
        response = query.EndExecute(ar) as QueryOperationResponse<Table>;
        _customers.Load(response);
        var continuation = response.GetContinuation();            

        while (continuation != null)
        {
            var ar2 = _context.BeginExecute<Table>(continuation, null, null);
            ar2.AsyncWaitHandle.WaitOne();
            var response2 = _context.EndExecute<Table>(ar2) as QueryOperationResponse<Table>;
            _customers.Load(response2);
            continuation = response2.GetContinuation();
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-14
    • 1970-01-01
    • 2013-11-13
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多