简体   繁体   中英

Equivalent Odata WCF Service Functionality with OData Controller

In one project I was really close to implementing OData with a WCF Service and Entity Framework. With the WCF Service I was able to extend DataService and it was able translate my context to entities queryable by OData. I want to do the same thing except using an ODataController. Ideally, I'd like to have all of my Entity Sets accessible from one controller. Is this possible?

This is the approach that I've tried so far. How do I bind multiple entity sets to one odata controller?

This the approach I used in my wcf service

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, IncludeExceptionDetailInFaults = true)]
    public class MyService : MSDataService.System.Data.Services.DataService<MyDataContext>
    {
        private SPWeb _web;
        private string connectionString;

        #region Constructor
        /// <summary>
        /// Default constructor
        /// </summary>
        public MyService() : base()
        {

        }

        #endregion
        #region Methods

        protected override MyDataContext CreateDataSource()
        {
            return newMyDataContext();
        }



        public static void InitializeService(MSDataService::System.Data.Services.DataServiceConfiguration config)
        {
            config.SetEntitySetAccessRule("MyEntity1", MSDataService::System.Data.Services.EntitySetRights.AllRead);
            config.SetEntitySetAccessRule("MyEntity2", MSDataService::System.Data.Services.EntitySetRights.AllRead);
            config.DataServiceBehavior.MaxProtocolVersion = System.Data.Services.Common.DataServiceProtocolVersion.V2;
            config.DataServiceBehavior.AcceptProjectionRequests = true;
            config.UseVerboseErrors = true;
            config.EnableTypeAccess("*");
            config.SetEntitySetPageSize("*", 1000);
        }
        #endregion
    }

MyDataContext had DbSets for MyEntity1 and MyEntity2.

For the OData Controller, I would currently need two different controllers for each entity, but I would like to have one generic controller.

Sounds like you need RESTier:

Restier is the spiritual successor to WCF Data Services. Instead of generating endless boilerplate code with the current Web API + OData toolchain, RESTier helps you boostrap a standardized, queryable HTTP-based REST interface in literally minutes. And that's just the beginning.

Like WCF Data Services before it, Restier provides simple and straightforward ways to shape queries and intercept submissions before and after they hit the database. And like Web API + OData, you still have the flexibility to add your own custom queries and actions with techniques you're already familiar with.

https://github.com/OData/RESTier

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM