扩展WCF OData服务中的方法

时间:2014-07-16 11:58:55

标签: entity-framework-5 wcf-data-services

我创建了WCF OData服务,引用了基于EF6的项目DAL。 在这里,我指的是我的DataContext的扩展类,名为MyDbContext。

namespace PScope.DAL
{

public class MyDbContext : DbContext
{
    public DbSet<AspNetUser> AspNetUsers { get; set; }

    PScopeDB db = new PScopeDB();

    public MyDbContext()
        : base("name=PScopeDB")   
    {
    }

     public IQueryable<Task> TaskList
    {
        get
        {
            return db.Tasks;
        }
    }
}
}

在这个类中,我正在尝试创建一个新方法,该方法几乎没有连接来返回一个新实体。我使用WebGet方法在我的DataService类中定义了实体。

namespace PrimariusScope.DataService
{

[JSONPSupportBehavior]
public class DataService : EntityFrameworkDataService<DAL.MyDbContext>, IServiceProvider
{
    // This method is called only once to initialize service-wide policies.
    public static void InitializeService(DataServiceConfiguration config)
    {
        // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
        // Examples:
        // config.SetEntitySetAccessRule("MyEntityset", EntitySetRights.AllRead);
        // config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);
        config.SetEntitySetAccessRule("*", EntitySetRights.All);
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;

    }
    public object GetService(Type serviceType)
    {
        if (serviceType == typeof(IDataServiceStreamProvider))
        {
            // Return the stream provider to the data service.
            return new ImageStreamProvider();
        }
        return null;
    }

    // Define a query interceptor for the Resources entity set.
    [QueryInterceptor("Resources")]
    public Expression<Func<DAL.Resource, bool>> OnQueryResources()
    {
        return o => o.Active == true;
    }

    [WebGet]
    public IQueryable<DAL.Task> TaskList()
    {
        DAL.MyDbContext entities = new DAL.MyDbContext();
        return entities.TaskList;
    }
}
}

但是当我运行我的服务时,不会公开TaskList方法。 http://localhost:12034/DataService.svc/TaskList返回Not Found。

我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

我遗失了以下声明:

public static void InitializeService(DataServiceConfiguration config) {
    ...
    config.SetServiceOperationAccessRule("TaskList", ServiceOperationRights.All);
    ...
}