locked
OData with WCF Service RRS feed

  • Question

  • Hi,

    I want to be able to use OData queries on my WCF Service(not WCF Data Service)

    The service is hosted inside IIS and the client is IE(the address bar)

    I guess i need to catch the query\url before WCF?

    How should i do it? some extension? some isapi filter?

    Thanks

    Tuesday, January 4, 2011 10:53 AM

Answers

  •  

    Found a solution:

    [WebGet(UriTemplate = "/*")] 
    public void RunQuery()
    {
     string s = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri.ToString();
    
    }
    
    • Marked as answer by NoundaDee Friday, January 7, 2011 7:29 AM
    Friday, January 7, 2011 7:26 AM

All replies

  • Hi,

    I am trying to understand your scenario and why you think this is a WCF question, and not one for WCF Data Services forum.

    Can you explain a bit more about your scenario? What exactly are you trying to do? Do you want to create a WCF client for the OData service? Or do you want to just use IE as a client? Do you have control over the IIS server? Or are you just consuming the Odata service?

    Tuesday, January 4, 2011 6:18 PM
  • I just want to take advantage on odata and restfull query(url format)

    I cannot use wcf data service since i have legacy data layer and even custom wcf data service provider cannot help me.

    I am writing both the client and the server.

    I do have control over IIS server.

    What i need is something like this

    Assuming the following url is entered inside IE address bar

    http://localhost/WCF/WCF.svc/Categories(1)/Products(1)/Supplier/Address/City

    I would like to get the part of the query(from Catgories to the right)

    As the query parameter to the following RunQuery function

    It is very important the the URL will be a valid OData resource path

    [

     

    ServiceContract()]

    [

     

    AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

    public class DataStoreService{

     

     

    public void RunQuery(string query){

     

    }

    }

     

    Thanks

     

    Tuesday, January 4, 2011 8:20 PM
  • There seem to be two issues here that are making this difficult (let me know if I am not understanding you correctly):

     1) You want to have your existing WCF service handle OData-based requests.

    2) You have an existing data access layer.

    For issue #1, have you considered hosting the data service directly by implementing DataServiceHost? For example, this is a simple console-based service that accesses a NW data model:

    using System;
    using System.Data.Services;
    using System.Linq;
    using CustomHostedDataService;
    
    namespace CustomHostedDataService
    {
      public class ConsoleService : DataService<NorthwindEntities>
      {
       public static void InitializeService(
         IDataServiceConfiguration configuration)
       {
         configuration.SetEntitySetAccessRule(
          "*", EntitySetRights.AllRead);
       }
      }
    
      class Program
      {
       static void Main(string[] args)
       {
         Type serviceType = typeof(ConsoleService);
         Uri baseAddress = new Uri("http://localhost:6000/");
         Uri[] baseAddresses = new Uri[] { baseAddress };
    
         DataServiceHost host = new DataServiceHost(
          serviceType, 
          baseAddresses);
         host.Open();
    
         Console.WriteLine(
          "Navigate to the following URI to see the service.");
         Console.WriteLine(baseAddress);
         Console.WriteLine("Press any key to exit...");
         Console.ReadKey();
    
         host.Close();
       }
      }
    }
    
    

    You should be able to do the same in your WCF service.

    As for #2, I am surprised that you cannot write a custom data provider to access your existing data layer as custom providers functionality was designed to enable you to expose any data via WCF Data Services. Also, have you looked into the reflection provider (IQueryable/IUpdatable)? For more info, see http://msdn.microsoft.com/en-us/library/dd672591.aspx.

    What you seem to be wanting to do is define your own "from scratch" implementation of the OData protocol, which may not be the best way for you to go.

    I also suggest that you repost this question to the WCF Data Services forum, as these folks will know much more about your scenario than folks in this WCF forum.


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Tuesday, January 4, 2011 9:46 PM
  •  

    Thanks, I cannot use WCF Data service in the current point of time;such will be too revolutionary approach for us, I agree that a custom data provider is a correct solution for the long term,and i am going to implement such in the future.

     

    For now all I need is to pass a legal OData query to my WCF service,

    I just need a Restfull WCF service with unrestricted Uri template

     

    Thanks

     

    Wednesday, January 5, 2011 8:30 AM
  • The main problem is that OData is inherently model-based, which is very REST friendly, whereas WCF is always operation-based. Even with WCF REST, you still are having to map each REST-ful URI to a service operations. You will do probably even more work creating all of the REST-operation mappings to mimic an OData service in WCF than you would creating a custom data provider for WCF Data Services to access your data. (There is a good example of how to do this in the OData Provider Toolkit.)

    Also, I'm not sure that I agree with OData being a revolutionary" approach versus WCF REST. WCF Data Services uses WCF for messaging, and WCF REST is really only about as mature as WCF Data Services. Plus, for REST data access, I think that there may be more momentum behind OData than WCF REST, as you can see the current list of folks supporting OData services.


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Wednesday, January 5, 2011 6:44 PM
  •  

    Glenn, I am not arguing about OData, I think it is great, I like it very much, I am saying that for my company and my team, this is too revolutionary at

     

    This point of time, We have too many constraints, too many tasks, and too less knowledge on this subject

     

    Anyway, what I want to be able to do is to map each REST-ful URI to the same operation :

     

    void RunQuery(string query) or void RunQuery()

     

    It must be possible; surely I can write some wcf extension?

     

    Thanks

     

    Thursday, January 6, 2011 8:09 AM
  • I think that I understand your constraints, but it doesn't sound like you are really interested in implementing the OData protocol, just accepting REST-ful OData-style URIs. In this case you will still have to write your own query parser.

    You should really repost this question in the WCF Data Servics forum, because the folks there have a pretty good knowledge of WCF and a very good knowledge of OData.


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Thursday, January 6, 2011 5:46 PM
  • Glenn, this is true i just need to accept REST-ful OData-style URIs

    How do i do it?

    Do i need to derive from UriTemplate and somehow enable wild card?

    Do i need to implement HttpHandler?

    Where in WCF pipe line can i  inject an extension in order to map All URIs

    To same operation?

    Thanks in advance

     

    Friday, January 7, 2011 7:13 AM
  •  

    Found a solution:

    [WebGet(UriTemplate = "/*")] 
    public void RunQuery()
    {
     string s = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri.ToString();
    
    }
    
    • Marked as answer by NoundaDee Friday, January 7, 2011 7:29 AM
    Friday, January 7, 2011 7:26 AM