Silverlight 中的域服务问题
我在 Silverlight RIA WCF 服务应用程序中有以下代码:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
EmployeesService2 context = new EmployeesService2();
EntityQuery<Employee> query = context.GetEmployeeQuery();
context.Load(query);
int count = context.Employees.Count();
EmployeeGrid.ItemsSource = context.Employees;
}
它用项目填充网格,但 context.Employees.Count() = 0
。为什么会这样呢?
另一个问题是我对不同域服务的另一个页面有类似的代码 基于另一个实体模型和数据库。但在这种情况下,服务不会返回任何实体。可能的原因是什么?数据库不为空。
I have the following code in the Silverlight RIA WCF services application:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
EmployeesService2 context = new EmployeesService2();
EntityQuery<Employee> query = context.GetEmployeeQuery();
context.Load(query);
int count = context.Employees.Count();
EmployeeGrid.ItemsSource = context.Employees;
}
It populates Grid with items, but context.Employees.Count() = 0
. Why is this so?
Another problem is that I have a similar code for another page against different domain service which
based on another entity model and database. But in that case the service didn't return any entities. What can be possible reason for that? The database is not empty.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如其他人提到的,这是因为 Load 是异步的,并且立即返回,没有任何数据。您需要响应加载操作已完成事件。
您可以按照@Levisaxos所说的那样执行此操作并处理Completed事件,或者您可以将回调作为参数传递给Load。当加载操作完成时,将执行此回调。
正如 @Levisaxos 所说,连接到该事件也应该有效,但您应该记住再次取消订阅该事件,否则您将泄漏内存。
http://forums.silverlight.net/forums/p/129624/296266.aspx
Ass others have mentioned, it's because Load is asynchronus and returns immediately without any data. You need to respond to the Load operations completed event.
You can do this as @Levisaxos said and handle the Completed event or you can pass in a callback as a parameter to Load. This callback will then be executed when the Load operation has completed.
Hooking up to the event as @Levisaxos said should work too but you should remember to unsubscribe from the event again or you'll leak memory.
http://forums.silverlight.net/forums/p/129624/296266.aspx
一种可能是您没有将服务设置为在返回的数据中包含 Employee 表。为此,只需将 [Ininclude] 属性添加到员工的 Members 属性上的元数据文件中(如果发生这种情况)。
One possibility is that you do not have your service set to include the Employee table in the returned data. To do this simple add the [Include] attribute to your metadata file on the Employees property of employee (if this is what is happening).
这可能是因为在您询问其项目数时 context.load 仍在加载。
试试这个
This is probably because the context.load is still loading at the moment you are asking for the count of it's items.
try this
context.Load(query) 是一个异步操作。在底层的网络请求检索数据之前,它立即返回。您需要监听 Levisaxos 答案中所示的操作完成事件。
如果您挂接了 operation.Completed 事件,但仍然在上下文中看不到任何数据,那么是时候将您的注意力转向服务器端的域服务了。在与 GetEmployeeQuery(可能是 GetEmployee)对应的域服务方法中设置一个断点,并确保它被调用。然后在模型的实体集合属性中设置一个断点(可能在 mymodel.Designer.cs 的 ObjectSetEmployees 属性中),以查看实际检索的数据。
context.Load(query) is an async operation. It returns immediately, before the network request under the hood has retrieved the data. You need to listen for the operation completed event as indicated in Levisaxos' answer.
If you hook up the operation.Completed event and still don't see any data in the context, then it's time to turn your attention to the domain service on the server side. Set a breakpoint in the domain service method corresponding to GetEmployeeQuery (probably GetEmployee) and make sure it's getting called. Then set a breakpoint in the entity collection property in your model - probably in mymodel.Designer.cs, property ObjectSet Employees, to see what data is actually being retrieved.