从客户端访问DomainService中的自定义对象
我正在使用域服务从Silverlight客户端从数据库中获取数据。
在DomainService1.cs中,我添加了以下内容:
[EnableClientAccess()] public class Product { public int productID; public string productName; public List Parts = new List(); //Part is already present in Model designer }
在DomainService1类中,我添加了一个新方法来检索自定义类对象的集合:
[EnableClientAccess()] public class DomainService1 : LinqToEntitiesDomainService { ... public List GetProductsList(...) { List resultProducts = new List(); ... return resultProducts; } }
从Silverlight客户端我试图访问该方法:
DomainService1 ds1 = new DomainService1(); var allproductList = ds1.GetProductsList(...); ds1.Load(allproductList).Completed += new EventHandler(Load_Completed); //Not correct usage
但是,这不是调用新方法的正确方法。 我在DomainServices.cs中添加新类Product的原因是要进行有效的分组。 我无法使用entity framework自动生成的模型类来实现相同的function。
如何调用我从客户端调用新方法?
我相信这里有一个类似的问题答案:
DomainService可以返回单个自定义类型吗?
另外,这里有一些关于在域服务中添加自定义方法的整体问题的讨论:
虽然我不知道你的意思是“它不是调用新方法的正确方法”,或者如果你遇到任何错误,我想也许发布一些工作代码可能有所帮助。
我的POCO
public class GraphPointWithMeta { [Key] public Guid PK { get; set; } public string SeriesName { get; set; } public string EntityName { get; set; } public double Amount { get; set; } public GraphPointWithMeta(string seriesName, string entityName, double amount) { PK = Guid.NewGuid(); SeriesName = seriesName; EntityName = entityName; Amount = amount; } // Default ctor required. public GraphPointWithMeta() { PK = Guid.NewGuid(); } }
域服务中的方法(EnableClientAccess修饰类)
public IEnumerable CallingActivityByCommercial() { List gps = new List (); // ... return gps; }
从Silverlight客户端调用就像
ctx1.Load(ctx1.CallingActivityByCommercialQuery(), CallingActivityCompleted, null);
客户回叫方法
private void CallingActivityCompleted(LoadOperation lo) { // lo.Entities is an IEnumerable }
我不确定您的Product类是否是实际实体。 从定义的方式来看,它似乎不是一个实体。 我的回答是假设它不是一个实体。 您需要为产品属性应用DataMemberAttribute,并且不会加载产品列表 – 加载是针对实体查询(服务端的IQueryable)。 您可以像这样(客户端)调用它:
void GetProductList( Action>> callback) { DomainService ds1 = new DomainService(); ds1.GetProductsList(callback, null);//invoke operation call }
域服务(服务器端)方法需要InvokeAttribute,如下所示:
[EnableClientAccess] public class MyDomainService { [Invoke] public List GetProductList() { var list = new List (); ... return list; } }
以下是您的Product类的定义方式(如果它不是实体):
public class Product { [DataMember] public int productID; [DataMember] public string productName; [DataMember] public List Parts = new List (); // you might have some trouble here. //not sure if any other attributes are needed for Parts, //since you said this is an entity; also not sure if you //can even have a list of entities or it needs to be an //entity collection or what it needs to be. You might //have to make two separate calls - one to get the products //and then one to get the parts. }
就像我说的,我不确定Productinheritance的是什么……希望这会有所帮助。