无法在(WCF数据服务)中创建简单的服务操作

时间:2014-04-18 22:35:49

标签: c# linq wcf-data-services

我有WCF数据服务:

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");
    }
}

当我启动该服务时,我的浏览器会在

处打开
http://localhost:10144/Services/WcfTcSubDataService.svc/
在浏览器中我可以成功查看数据库中的所有entite。

但是当我试着去

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

我得到一个例外说:

 <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>

1 个答案:

答案 0 :(得分:0)

不得不补充:

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

    etc...