Search code examples
wcfriawcf-ria-servicesdomainservices

WCF Ria DomainService - Initialize WebService on StartUp


Currently, my DomainService does perform an Initialization of a resource everytime a client is connecting to him. Every client should access the same instance of this resource. I would like to initialize this resource on the StartUp of the WebService. Is there any chance to do that with WCF Ria Services?

EDIT: Okay, don't mention it. I wanted to use this for an global DbContext object. This isn't a good idea anyway, because there will be multiple threads managed by the HttpApplication which would access the DbContext simultaneously. I will change my implementation to an "per Thread", respectively "per HttpContext", approach. Thanks anyhow.


Solution

  • You can define a class that contains a static property for that resource. In the DomainService you can then access that property. It would then be initialized only when it is accessed the first time.

    Example:

    public class ResManager {
        public static MyObject {...}
    }
    

    In the DomainService:

    public IQueryable<SomeClass> GetSomeObjects()
    {
        // you can access it here and it will not be initialized 
        // every time the DomainService is called
        MyObject obj = ResManager.MyObject;
        return new List<SomeClass>().AsQueryable();
    }
    

    If you want to initialize it when the Service is started, then you should be able to do that in the Global class.