为什么我的 WCF 数据服务客户端代理代码中缺少 ServiceOperation 方法?
我有一个简单的 WCF 数据服务服务,我想公开一个服务操作,如下所示:
[System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class ProductDataService : DataService<ProductRepository>
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(IDataServiceConfiguration config)
{
config.SetEntitySetAccessRule("*",
EntitySetRights.ReadMultiple | EntitySetRights.ReadSingle);
config.SetServiceOperationAccessRule("*",
ServiceOperationRights.All);
config.UseVerboseErrors = true;
}
// This operation isn't getting generated client side
[WebGet]
public IQueryable<Product> GetProducts()
{
// Simple example for testing
return (new ProductRepository()).Product;
}
为什么在客户端上添加服务引用时 GetProducts
方法不可见?
我正在运行 Visual Studio 2008 SP1 和 .NET Framework 3.5 SP1。我还下载并安装了此更新:
I have a simple WCF Data Services service and I want to expose a Service Operation as follows:
[System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class ProductDataService : DataService<ProductRepository>
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(IDataServiceConfiguration config)
{
config.SetEntitySetAccessRule("*",
EntitySetRights.ReadMultiple | EntitySetRights.ReadSingle);
config.SetServiceOperationAccessRule("*",
ServiceOperationRights.All);
config.UseVerboseErrors = true;
}
// This operation isn't getting generated client side
[WebGet]
public IQueryable<Product> GetProducts()
{
// Simple example for testing
return (new ProductRepository()).Product;
}
Why isn't the GetProducts
method visible when I add the service reference on the client?
I'm running Visual Studio 2008 SP1 and .NET Framework 3.5 SP1. I also downloaded and installed this update:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
终于解决了这个问题。要调用数据服务类上的服务操作,您需要使用数据服务上下文对象的
CreateQuery
或Execute
方法。例如:如果需要参数,假设服务操作上的产品类别具有以下签名:
我们会这样做:
Finally solved this. To call a service operation on a data service class you need to use data service context object's
CreateQuery
orExecute
methods. For example:If parameters were required, say a product category on a service operation that had this signature:
We would do:
(这个答案是不正确(见评论),但故意留在这里是为了阻止其他答案盲目地陷入同一个洞)
IIRC,它也需要是一个
[OperationContract]
(理想情况下,服务本身就是一个
[ServiceContract]
)(this answer is incorrect (see comments), but is deliberately left here to stop other answers stumbling blindly into the same hole)
IIRC, it also needs to be an
[OperationContract]
(and ideally the service itself would be a
[ServiceContract]
)我在以下示例中遇到了类似的问题
当我浏览服务
http://localhost:3059/TestService.svc
时,列表没有列出具有 WebGet 属性的方法,但我能够使用访问它http://localhost:3059/TestService.svc/GetStrings?index=1
这表明 WCF 数据服务定义在通过 Web 浏览器浏览或存在时不会列出操作一些未记录的方法可以将两者都添加到列表中。
I had a similar issue with following sample
When I browsed the service
http://localhost:3059/TestService.svc
, the list didn't list the method with the WebGet attribute, but I am able to access it usinghttp://localhost:3059/TestService.svc/GetStrings?index=1
This suggests to me that the WCF data service definition doesn’t list the operations when it's browsed through a web browser or there is some undocumented way to get both in the list.