【问题标题】:How to return List<int> from domain service如何从域服务返回 List<int>
【发布时间】:2011-11-29 19:39:49
【问题描述】:

大家好,我正在使用 WCF RIA 服务,我有域服务,我在其中编写了这个方法

public List<int> GetActionIDs() 
    {
        return (from d in ObjectContext.actions select d.id).ToList();
    }

如何在客户端获取此列表? 这不起作用:

List<int> = db.GetActionIDs();

有什么建议吗?

【问题讨论】:

  • 您有任何错误吗?您的 Web 服务的其他方法是否有效?顺便说一句,List&lt;int&gt; = db.GetActionIDs(); 无效 - 您需要一个变量名。
  • 是的,但它给出了这个错误:>> 只有赋值、调用、递增、递减和新对象表达式可以用作语句
  • 你在哪一行得到这个错误?
  • 列表 = db.GetActionIDs();
  • 试试:List&lt;int&gt; ids = db.GetActionIDs();

标签: silverlight wcf wcf-ria-services domainservices


【解决方案1】:

首先,您应该阅读RIA Services manual,因为您没有意识到 Silverlight 中的服务调用是异步的。

在你的情况下,你应该

在服务中添加InvokeAttribute到你的操作中:

[Invoke]
public List<int> GetActionIDs() 
{
    return (from d in ObjectContext.actions select d.id).ToList();
}

然后,对DomainContext 的所有调用都是异步的,因此您可以在回调中获得结果:

db.GetActionIDs(operation =>
                {
                  //TODO: check the operation object for errors or cancellation

                  var ids = operation.Value; // here's your value

                  //TODO: add the code that performs further actions
                }
                , null);

【讨论】:

    【解决方案2】:

    在域服务中

    [Query]    
    public List<Action> GetActionIDs()     
     {         
       List<Action> result =  (  
                               from a in ObjectContext.actions                                     
                                select new action                                   
                                 {                        
                                    ID = a.ID
                                 }
                            ).ToList(); 
       return result ;
     }
    

    银光

    DomainService1 DS = new  DomainService1();
    LoadOperation<Action> LoadOp = Ds.Load(Ds.GetActionIDsQuery());
    
    LoadOperation.Completed += new EventHandler((s,e)=>{
       foreach (Action item in LoadOp.Entities)
       {
       }
    });
    

    【讨论】:

      猜你喜欢
      • 2014-10-13
      • 2016-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-18
      • 2016-08-19
      • 1970-01-01
      相关资源
      最近更新 更多