Why is my ServiceOperation method missing in my WCF Data Services Client proxy?

I have a simple WCF Data Services service, and I want to expose the 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 is the GetProducts method not showing GetProducts when I add a service link on the client?

I am running Visual Studio 2008 SP1 and the .NET Framework 3.5 SP1. I also downloaded and installed this update:

MS KB: 976127 - An update is available that provides additional features and improvements for ADO.NET Data Services in the .NET Framework 3.5 SP1 on a computer that is running Windows 7 or Windows Server 2008 R2

+7
visual-studio-2008 wcf-data-services astoria
source share
3 answers

Finally decided this. To invoke a maintenance operation on a data service class, you need to use the methods of the CreateQuery or Execute data context object. For example:

 ProductDataService ctx = new ProductDataService( new Uri("http://localhost:1234/ProductDataService.svc/")); // Method 1: DataServiceQuery<Product> q = ctx.CreateQuery<Product>("GetProducts"); List<Product> products = q.Execute().ToList(); // Method 2: Uri uri = new Uri(String.Format("{0}GetProducts", ctx.BaseUri), UriKind.RelativeOrAbsolute); List<Product> products = ctx.Execute<Product>(uri).ToList(); 

If you need parameters, let's say a product category in a service operation that had this signature:

 [WebGet] public IQueryable<Product> GetProducts(string category) 

We would do:

 // Method 1: DataServiceQuery<Product> q = ctx.CreateQuery<Product>("GetProducts") .AddQueryOption("category", "Boats") ; List<Product> products = q.Execute().ToList(); // Method 2: Uri uri = new Uri(String.Format("{0}GetProducts?category={1}", ctx.BaseUri, "Boats"), UriKind.RelativeOrAbsolute); List<Product> products = ctx.Execute<Product>(uri).ToList(); 
+10
source share

(this answer is incorrect (see comments), but intentionally left here to stop other answers, blindly into the same hole)


IIRC, it should also be [OperationContract]

 [OperationContract, WebGet] public IQueryable<Product> GetProducts() { // Simple example for testing return (new ProductRepository()).Product; } 

(and ideally the service itself would be [ServiceContract] )

+1
source share

I had a similar problem with the following example

  [System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = true)] public class TestService : DataService<MyService> { // This method is called only once to initialize service-wide policies. public static void InitializeService(DataServiceConfiguration config) { config.SetEntitySetAccessRule("*", EntitySetRights.AllRead); config.SetServiceOperationAccessRule("*", ServiceOperationRights.AllRead); config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2; } [WebGet] public IQueryable<string> GetStrings(int index) { string[] list = { "One", "two" }; return list.AsQueryable(); } } 

When I looked at the service http://localhost:3059/TestService.svc , the list did not display the method with the WebGet attribute, but I can access it using http://localhost:3059/TestService.svc/GetStrings?index=1

This tells me that the WCF data service definition does not list operations when viewed through a web browser, or there is an undocumented way to get both listed.

0
source share

All Articles