Wcf 通过服务公开数据对象?
我创建了一个SOAP服务,该服务应该通过亚音速查询返回一个Category对象和一个CategoryCollection。问题是我可以很好地返回数据表,但是从服务返回的对象在内部是活动记录类型的,而不是我的DAL实体 例如,当我使用该服务时,我看到的是SOAPService.Category,而不是SOAPService.CategoryCollection(我应该能够看到SOAPService.[所有其他数据实体],而SOAPService.Category是活动记录类型,不包含实际的类别属性 这两个类都是通过我的亚音速DAL生成定义的Wcf 通过服务公开数据对象?,wcf,c#-4.0,webservice-client,Wcf,C# 4.0,Webservice Client,我创建了一个SOAP服务,该服务应该通过亚音速查询返回一个Category对象和一个CategoryCollection。问题是我可以很好地返回数据表,但是从服务返回的对象在内部是活动记录类型的,而不是我的DAL实体 例如,当我使用该服务时,我看到的是SOAPService.Category,而不是SOAPService.CategoryCollection(我应该能够看到SOAPService.[所有其他数据实体],而SOAPService.Category是活动记录类型,不包含实际的类别属性
namespace TrainingWebService.DAL
{
/// <summary>
// Strongly-typed collection for the Category class.
/// </summary>
[Serializable]
public partial class CategoryCollection : ActiveList<Category, CategoryCollection>
{
.
.
.
}
在我的SOAPService.cs中
public CategoryCollection GetAllCategories() // get a collection of all categories
{
return DataCaller.GetAllCategories();
}
public DataTable GetAllCategoriesAsDataTable()
{
return DataCaller.GetCategoriesAsDataTable();
}
public DataTable GetCategoryAsDataTable(int id)
{
return DataCaller.GetCategoryAsDataTable(id);
}
Here's a snip of the DataCaller code.
/// <summary>
/// Get all categories - returns a collection of categories
/// </summary>
/// <returns></returns>
public static CategoryCollection GetAllCategories()
{
categoryCollection =
DB.Select().From("Categories")
.ExecuteAsCollection<CategoryCollection>();
return categoryCollection;
}
public static DataTable GetCategoryAsDataTable(int id)
{
try
{
dtResults = new Select()
.Where(Category.Columns.CategoryID).IsEqualTo(id)
.From(Category.Schema)
.ExecuteAsCollection<CategoryCollection>().ToDataTable();
}
catch (Exception ex)
{
throw ex;
}
return dtResults;
}
public CategoryCollection GetAllCategories()//获取所有类别的集合
{
返回DataCaller.GetAllCategories();
}
公共数据表GetAllCategoriesAsDataTable()
{
返回DataCaller.GetCategoriesAsDataTable();
}
公共数据表GetCategoryAsDataTable(int id)
{
返回DataCaller.GetCategoryAsDataTable(id);
}
这是一段DataCaller代码。
///
///获取所有类别-返回类别的集合
///
///
公共静态类别集合GetAllCategories()
{
分类收集=
DB.Select().From(“类别”)
.ExecuteAsCollection();
返回类别集合;
}
公共静态数据表GetCategoryAsDataTable(int id)
{
尝试
{
dtResults=newselect()
.Where(Category.Columns.CategoryID).IsEqualTo(id)
.From(Category.Schema)
.ExecuteAsCollection().ToDataTable();
}
捕获(例外情况除外)
{
掷骰子;
}
返回结果;
}
我认为问题可能在于通过我的web服务公开*.DAL实体,以便它们可以访问
我在前一段时间构建的另一个解决方案中可以很好地实现这一点,但由于某些原因,我看不出我在这里缺少了什么。如果适用,请不要忘记使用[DataContract]装饰您的DAL实体
如果适用,不要忘记用[DataContract]装饰您的DAL实体
也许DataContract适用?啊,是的!我使用的DAL生成器的旧版本缺少CollectionDataContract和DataContract添加项。是的,我真傻。看起来就是这样。我还注意到我在Codeplex()上有一个旧版本这不支持CollectionDataContract和DataContracts-现在已更新。但无法将您的评论标记为答案。添加了我的评论作为答案,尽管它很短…也许DataContract适用?啊,是的!我使用的DAL生成器的旧版本缺少CollectionDataContract和DataContracts addi是的,我真傻。看起来就是这样。我还注意到我在Codeplex上有一个旧版本()这不支持CollectionDataContract和DataContracts-现在已更新。但无法将您的评论标记为答案。添加了我的评论作为答案,尽管它很短…还添加了CollectionDataContract和可序列化属性。还添加了CollectionDataContract和可序列化属性。
[OperationContract]
string GetDBConnectionStringDetails();
[OperationContract]
string ReturnSameString(string someString);
//
// Database-related
//
[OperationContract] // categories
CategoryCollection GetAllCategories(); // SubSonic object
[OperationContract]
DataTable GetAllCategoriesAsDataTable();
[OperationContract]
DataTable GetCategoryAsDataTable(int id);
[OperationContract]
Category GetCategoryByID(int id); // SubSonic object
[OperationContract]
// products
ProductCollection GetAllProducts();
[OperationContract]
Product GetProductByID(int id);
[OperationContract] // suppliers
SupplierCollection GetAllSuppliers();
[OperationContract]
Supplier GetSupplierByID(int id);
}
public CategoryCollection GetAllCategories() // get a collection of all categories
{
return DataCaller.GetAllCategories();
}
public DataTable GetAllCategoriesAsDataTable()
{
return DataCaller.GetCategoriesAsDataTable();
}
public DataTable GetCategoryAsDataTable(int id)
{
return DataCaller.GetCategoryAsDataTable(id);
}
Here's a snip of the DataCaller code.
/// <summary>
/// Get all categories - returns a collection of categories
/// </summary>
/// <returns></returns>
public static CategoryCollection GetAllCategories()
{
categoryCollection =
DB.Select().From("Categories")
.ExecuteAsCollection<CategoryCollection>();
return categoryCollection;
}
public static DataTable GetCategoryAsDataTable(int id)
{
try
{
dtResults = new Select()
.Where(Category.Columns.CategoryID).IsEqualTo(id)
.From(Category.Schema)
.ExecuteAsCollection<CategoryCollection>().ToDataTable();
}
catch (Exception ex)
{
throw ex;
}
return dtResults;
}