Pergunta

I have the WCF data service:

public class WcfTcSubDataService : DataService<TcSubEntities>
{
    /// <summary>
    /// Get the user that is loged in in the session
    /// </summary>
    Common.Models.Client client
    {
        get
        {
            return HttpContext.Current.Session["l"] as Common.Models.Client;
        }
    }

    // This method is called only once to initialize service-wide policies.
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("*", EntitySetRights.All);  
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;          
    }

    // this is the method I am trying to invoke

    [WebGet]
    public IQueryable<Data.Sub.User> Foo()
    {
        return this.CurrentDataSource.Users;  
    }


    protected override void HandleException(HandleExceptionArgs args)
    {
        base.HandleException(args);
    }


    protected override Data.Sub.TcSubEntities CreateDataSource()
    {
        // for now return regardles if user is logged in or not
        return base.CreateDataSource();

        if(client!=null && client.IsLoggedIn)
           return base.CreateDataSource();
        else
            throw new Exception("unauthorized user");
    }
}

When I start that service my browser opens at

http://localhost:10144/Services/WcfTcSubDataService.svc/

in the browser I can successfully see all the entites on my database.

But when I try to go to

http://localhost:10144/Services/WcfTcSubDataService.svc/Foo

I get an exception saying:

 <m:error
   xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
   <m:code/> <m:message xml:lang="en-US">Resource not found for the
   segment 'Foo'.</m:message> 
 </m:error>
Foi útil?

Solução

Had to add:

public static void InitializeService(DataServiceConfiguration config)
{
    config.SetServiceOperationAccessRule("*", ServiceOperationRights.All); // <--- this line!!!

    etc...
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top