有关如何在XAML Metro应用程序中使用WCF RIA服务的任何示例? [英] Any sample on how to use WCF RIA Services in XAML metro app?

查看:76
本文介绍了有关如何在XAML Metro应用程序中使用WCF RIA服务的任何示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人尝试在基于XAML的Metro应用程序中使用WCF RIA服务.如果您有任何博客或示例,请分享.

I wonder if someone have tried using WCF RIA Services in XAML based metro application. If you have any blog or sample please share.

推荐答案

事实上,我确实做到了,这就是窍门:)

As a matter of fact I did and here's the trick :)

我在WCF服务中添加了"服务参考",从而公开了 ADO.NET实体框架模型.问题是在XAML/C#Metro应用中,执行以下代码失败:

I added a "Service Reference" to my WCF service exposing an ADO.NET Entity Framework model. The problem is that in a XAML/C# Metro app, executing the following code fails:

SampleEntities ctx = ((App)Application.Current).Context;
var query = from p in ctx.Products
              where p.Name == name
              select p;

foreach (Product p in query) /** this line fails **/
{
    // do stuff
}

这是您在运行时遇到的异常:

Here's the exception you'll get at runtime:

"Silverlight无法使您直接枚举数据 服务查询.这是因为枚举会自动发送一个 向数据服务的同步请求.因为只有Silverlight 支持异步操作,您必须改为调用 BeginExecute和EndExecute方法以获得一个查询结果,该结果 支持枚举.} [System.NotSupportedException]:{"Silverlight无法使您直接枚举数据服务查询.这是因为 枚举自动向数据发送同步请求 服务.由于Silverlight仅支持异步操作, 您必须改为调用BeginExecute和EndExecute方法以 获取支持枚举的查询结果."

"Silverlight does not enable you to directly enumerate over a data service query. This is because enumeration automatically sends a synchronous request to the data service. Because Silverlight only supports asynchronous operations, you must instead call the BeginExecute and EndExecute methods to obtain a query result that supports enumeration."} [System.NotSupportedException]: {"Silverlight does not enable you to directly enumerate over a data service query. This is because enumeration automatically sends a synchronous request to the data service. Because Silverlight only supports asynchronous operations, you must instead call the BeginExecute and EndExecute methods to obtain a query result that supports enumeration."

啊,这真是太好了,难以置信!

Ahhh, this would have been too good to be true!

如异常中所述,您需要服务器调用是异步的,就像在Silverlight中一样.

As stated in the exception, you need your server calls to be asynchronous, just as in Silverlight.

以下是如何在C#Metro应用程序中以传统方式使用WCF RIA服务的示例:

Here's a sample of how you can consume a WCF RIA Service in a C# Metro App doing it the old school way:

(...)
var query = from p in ctx.Products
            where p.Name == Name
            select p;

((DataServiceQuery<Product>)query).BeginExecute(OnLoadItemsCompleted, query);
(...)

private void OnLoadItemsCompleted(IAsyncResult result)
{
    var query = result.AsyncState as DataServiceQuery<Product>;
    IEnumerable<Product> response = query.EndExecute(result);

    foreach (Product o in response)
    {
         // Do stuff
    }
}

现在使用.NET 4.5及其新的 await & async 关键字,您可以获得相同的结果,同时又避免了所有这些小的回调方法将代码分块.

Now using .NET 4.5 and its new await & async keywords, you can get the same result whilst avoiding your code to be chunked with all those little callback methods.

示例:

async void GetProducts()
{
    SampleEntities ctx = ((App)Application.Current).Context;
    var query = from p in ctx.Products
                  where p.Name == name
                  select p;
    DataServiceQuery<Product> dqs = (DataServiceQuery<Product>)(query);
    TaskFactory<IEnumerable<Product>> tf = new TaskFactory<IEnumerable<Product>>();
    myListView.ItemsSource = await tf.FromAsync(dqs.BeginExecute(null, null), 
                               iar => dqs.EndExecute(iar));
}

使用后两种方法对我来说都很好:)

Using either of the last two methods worked fine for me :)

这篇关于有关如何在XAML Metro应用程序中使用WCF RIA服务的任何示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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