locked
Can't expose service operation RRS feed

  • Question

  • Hello I'm testing the WCF Data Services and wrote the following code:

    public class MusicStore : DataService<MusicStoreEntities>
        {
            private const string ADMIN_ROLE = "Administrator";
    
            // This method is called only once to initialize service-wide policies.
            public static void InitializeService(DataServiceConfiguration config)
            {
                config.SetEntitySetAccessRule("Albums", EntitySetRights.All);
                config.SetEntitySetAccessRule("Artists", EntitySetRights.All);
                config.SetEntitySetAccessRule("Genres", EntitySetRights.All);
                config.SetServiceOperationAccessRule("GetAlbumsByArtist", ServiceOperationRights.All);
                config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
            }
    
            [WebGet]
            public IQueryable<Album> GetAlbumsByArtist(string name)
            {
                return this.CurrentDataSource.Albums.Where(a => a.Artist.Name.Contains(name));
            }
    
            [ChangeInterceptor("Albums")]
            public void OnChangeAlbum(Album album, UpdateOperations operations)
            {
                if (!HttpContext.Current.Request.IsAuthenticated || !HttpContext.Current.User.IsInRole(ADMIN_ROLE))
                    throw new DataServiceException(400, "Albums can only be modified by an administrator.");
            }
        }


    All works except I can't see the "GetAlbumsByArtist" service operation. There is following response from the server:

    <?xml version="1.0" encoding="UTF-8"?>
    -<edmx:Edmx xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx" Version="1.0">-<edmx:DataServices xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:MaxDataServiceVersion="3.0" m:DataServiceVersion="1.0">-<Schema xmlns="http://schemas.microsoft.com/ado/2009/11/edm" Namespace="KendoWcfSample.Models">+<EntityType Name="Album">-+<EntityType Name="Genre">-+<EntityType Name="Artist">-+<Association Name="Genre_Albums">----+<Association Name="Album_Artist">----+<EntityContainer Name="MusicStoreEntities" m:IsDefaultEntityContainer="true">---</Schema></edmx:DataServices></edmx:Edmx>

    If I comment "config.SetEntitySetAccessRule("Albums", EntitySetRights.All);" there is the following error appears: "The server encountered an error processing the request. See server logs for more details."

    I'm using WCF Data Services 5.1.0, .Net Framework 4.5; web server - iis express; MusicStoreEntities type is DBContext. Maybe WCF Data Services correctly works only with ObjectContext? All queries to entities are working correctly. 

    Monday, December 3, 2012 7:12 PM

Answers

  • There should be a FunctionImport element under the EntityContainer element. You can see one for example in this response: http://services.odata.org/OData/OData.svc/$metadata - near the end there's the FunctionImport for GetProductsByRating.

    Thanks,


    Vitek Karas [MSFT]

    • Marked as answer by valx12 Tuesday, December 4, 2012 6:54 AM
    Monday, December 3, 2012 9:52 PM
    Moderator