我正在使用域名服务从Silverlight客户端从数据库中获取数据。
在DomainService1.cs中,我添加了以下内容:
[EnableClientAccess()]
public class Product
{
public int productID;
public string productName;
public List<Part> Parts = new List<Part>(); //Part is already present in Model designer
}
在DomainService1类中,我添加了一个新方法来检索自定义类对象的集合:
[EnableClientAccess()]
public class DomainService1 : LinqToEntitiesDomainService<HELPERDBNEWEntities1>
{
...
public List<Product> GetProductsList(...)
{
List<Product> resultProducts = new List<Product>();
...
return resultProducts;
}
}
我试图从Silverlight客户端访问该方法:
DomainService1 ds1 = new DomainService1();
var allproductList = ds1.GetProductsList(...);
ds1.Load<SLProduct>(allproductList).Completed += new EventHandler(Load_Completed); //Not correct usage
然而,这不是调用新方法的正确方法。我在DomainServices.cs中添加新类Product的原因是要进行有效的分组。我无法使用实体框架自动生成的模型类来实现相同的目标。
如何调用我从客户端调用新方法?
答案 0 :(得分:1)
答案 1 :(得分:1)
虽然我不知道你的意思是“这不是调用新方法的正确方法”,或者如果你收到任何错误,我想也许发布一些有用的代码可能有所帮助。
我的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<GraphPointWithMeta> CallingActivityByCommercial()
{
List<GraphPointWithMeta> gps = new List<GraphPointWithMeta>();
// ...
return gps;
}
从Silverlight客户端调用,如
ctx1.Load(ctx1.CallingActivityByCommercialQuery(), CallingActivityCompleted, null);
客户回电方法
private void CallingActivityCompleted(LoadOperation<GraphPointWithMeta> lo)
{
// lo.Entities is an IEnumerable<GraphPointWithMeta>
}
答案 2 :(得分:0)
我不确定您的Product类是否是实际实体。从定义的方式来看,它似乎不是一个实体。我的回答是假设它不是一个实体。您需要为产品属性应用DataMemberAttribute,并且您不会加载产品列表 - 加载是针对实体查询(服务端的IQueryable)。您可以像这样(客户端)调用它:
void GetProductList( Action<InvokeOperation<List<Product>>> callback)
{
DomainService ds1 = new DomainService();
ds1.GetProductsList(callback, null);//invoke operation call
}
域服务(服务器端)方法需要InvokeAttribute,如下所示:
[EnableClientAccess]
public class MyDomainService
{
[Invoke]
public List<Product> GetProductList()
{
var list = new List<Product>();
...
return list;
}
}
以下是您的Product类的定义方式(如果它不是实体):
public class Product
{
[DataMember]
public int productID;
[DataMember]
public string productName;
[DataMember]
public List<Part> Parts = new List<Part>(); // 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.
}
就像我说的那样,我不确定Product继承的是什么......希望这会有所帮助。