Access one service from another RRS feed

  • Question


    In my project I have two services 'a.svc' and 'b.svc' each of which has a standard 'public class Service: DataService<T>', where T are two different types - A and B respectively.


    (This is because the A is an Entity Framework-generated service, but B is completely my own confection).


    Now to my question: in the service code for b.svc (that is in a 'public IQueryable<C>' method within the type B), is it possible to get access to entities from A.


    That is I want service b.svc to be able to get data that would have been served by service a.svc, but shape it into types not understood by the enrtity framework.


    It this doable?






    (The reason is: let's say that in the Entity Framework-generated service there's an entity E. I wish to create a new entity Ex, which derives from E, but which overrides propertied. I've been unable to do this within a.svc and the entity model A, but perhaps that's because I just haven't tried the right thing).

    Tuesday, June 24, 2008 2:36 PM

All replies

  • In service B (the non-EFx service), could you directly use LINQ queries against the EFx model that backs service A and return them from your custom data source class as your own IQueryable? E.g. say you have an EFx model that has a "Customers" entity-set, your custom data source class could be something like:


    Code Snippet

    class CustomerEx


      public int ID { get; set; }

      public string Name { get; set; }



    class MyDataSource : IDisposable


      private EfxContext _context;


      MyDataSource() {

        _context = new EfxContext(); // your EFx context class



      public void Dispose() { _context.Dispose(); }


      public IQueryable<CustomerEx> CustomerExSet {

        get {

          return from c in _context.Customers

                   select new CustomerEx { ID = c.CustomerID, Name = c.CompanyName };





    (just a quick sample, I wrote it here directly so probably doesn't even compile as-is Smile


    The only issue with this I can think off the top of my head is that interceptors from service A wouldn't run, so if you're relying on those you may need something a bit fancier...(maybe factor out the interceptor into functions that can be used in both contexts).


    Pablo Castro
    Software Architect
    Microsoft Corporation


    Monday, June 30, 2008 2:59 AM
  • Thanks, I can see what you are getting at.


    But I don'd understand the context of your code snippet above. This is not the first forum post in which I've seen people talking about creating the context, but I've no idea of how to do it, and searching the web and MSDN for hours has proven fruitless.


    Secondly, what is MyDataSource above? Why only deriving from IDisposable and not DataService<T> ?


    I'm lost





    Monday, July 14, 2008 11:11 AM
  • The context in the sameple refers to the ObjectContext, assuming that the underlying provider is EntityFramework. If you are using Linq to Sql, it is called DataContext. Basically, its the class which exposes the top level IQueryable properties.


    Your service will look something like MyDataService<MyDataSource>, and hence MyDataSource doesn't need to derive form DataService<T>. In other words, MyDataSource becomes your DataContext like the ObjectContext in EF case or DataContext class in Linq to Sql.


    Hope this helps.





    Monday, July 14, 2008 5:03 PM