locked
Instance management of WCF Data Service RRS feed

  • Question

  • I read this article http://msdn.microsoft.com/en-us/magazine/cc163590.aspx regarding WCF instance management.

     When I tried the techniques in WCF Data Service I found no interface to applied ServiceContractAttribute like

    [ServiceContract(Session = true)]
    interface IMyContract {...}

    How we configure WCF Data Service for percall, persession, singleton, share service , etc..?

     

     

     

     

    Wednesday, June 29, 2011 4:36 PM

Answers

  • Hi WCF Data Services don't support the WCF instance management.

    The instance management they use is like this:

    - Each request creates a new instance of DataService<T>

    - Each request then calls CreateDataSource on that instance. The default implementation creates a new instance of T, so by default each request gets its own new context (T) as well.

    - You can override CreateDataSource and implement whichever instance management you want for the context (Which is what holds usually holds all the interesting information, like the connection to DB and so on).

    - You could also in theory override the creation of the DataService<T>, if you don't use the default factory. Not sure how helpfull that would be though as DataService<T> is pretty lightweight.

    Thanks,


    Vitek Karas [MSFT]
    • Marked as answer by B L 07058 Thursday, June 30, 2011 12:23 PM
    Wednesday, June 29, 2011 5:49 PM
    Moderator
  • It depends on the provider.

    If you're using built-in providers (EF, LINQ to Objects, LINQ to SQL), then the T is going to be disposed after each request. You could obviously override the Dispose and handle it differently.

    If you're using custom provider, then it's up to the provider, since the WCF DS doesn't call Dispose on the data source, but it calls it on the provider instance (which might or might not dispose the data context).

    Thanks,


    Vitek Karas [MSFT]
    • Marked as answer by B L 07058 Thursday, June 30, 2011 12:23 PM
    Thursday, June 30, 2011 12:11 PM
    Moderator

All replies

  • Hi WCF Data Services don't support the WCF instance management.

    The instance management they use is like this:

    - Each request creates a new instance of DataService<T>

    - Each request then calls CreateDataSource on that instance. The default implementation creates a new instance of T, so by default each request gets its own new context (T) as well.

    - You can override CreateDataSource and implement whichever instance management you want for the context (Which is what holds usually holds all the interesting information, like the connection to DB and so on).

    - You could also in theory override the creation of the DataService<T>, if you don't use the default factory. Not sure how helpfull that would be though as DataService<T> is pretty lightweight.

    Thanks,


    Vitek Karas [MSFT]
    • Marked as answer by B L 07058 Thursday, June 30, 2011 12:23 PM
    Wednesday, June 29, 2011 5:49 PM
    Moderator
  • Thanks for the quick reply.

    Further question after reading your reply.

    When an instance of DataService<T> got destroyed, does it dispose the instance T from an overrided CreateDataSource?

     




    Wednesday, June 29, 2011 7:32 PM
  • It depends on the provider.

    If you're using built-in providers (EF, LINQ to Objects, LINQ to SQL), then the T is going to be disposed after each request. You could obviously override the Dispose and handle it differently.

    If you're using custom provider, then it's up to the provider, since the WCF DS doesn't call Dispose on the data source, but it calls it on the provider instance (which might or might not dispose the data context).

    Thanks,


    Vitek Karas [MSFT]
    • Marked as answer by B L 07058 Thursday, June 30, 2011 12:23 PM
    Thursday, June 30, 2011 12:11 PM
    Moderator
  • Vitek, can you please publish a *working* real-world sample:

    - a singleton object is the root of an object graph in memory

    - the singleton is accessed using classic WCF for complicated method calls AND by OData services for access by mobile devices. => how do WCF Data Service and classic WCF cooperate on the same object graph?

    - because I am a database agnostic programmer, I use EF 4.2 code first on top. So how do I set up EF Code first context class for supporting singleton OData and singleton classic WCF?

    - the resulting code should be encapsulated in libraries so it can be hosted in a windows service or IIS.

     

     

    thanks

    Tuesday, January 3, 2012 9:49 PM