locked
Preloading/Initializing object on WCF Service Host Startup RRS feed

  • General discussion

  • Hi,

    How can I load something in memory on WCF host startup and have that  object available for every call so I don't have to load it each time I call a method. Is this even possible?
    I created another WCF method to do that, On WCF Host UI on load event, I initialize the service and call the preload method but I think as soon as the load method is done, do will the Service Class. Please help.
    Thursday, April 9, 2009 3:02 PM

All replies

  • Are you self hosting or IIS hosting?
    Richard Blewett, thinktecture - http://www.dotnetconsult.co.uk/weblog2
    Thursday, April 9, 2009 3:10 PM
  • Yes, it is self hosted (Windows APP)
    I have three projects
    WCF Contracts (Interface) Validate()
    WCF Services (Implementation) Validate()
    WCF Host (UI)

    Validate() calls X12Check() method in project in BusinessLayer. So for every call, I instantiate a huge object inside X12Check() everytime and that takes a long time. My logic is that if I can initialize that big object and pass it on to X12Check() somehow, I will be better off. The object will be loaded once and stays that way until the WCF Host is closed. I am new to WCF so I don't know that much but I am sure it's possible.

    Thanks
    Thursday, April 9, 2009 3:21 PM
  • static class HugeObjectHolder
    {
         static HugeObjectHolder()
         {
               theObject = new HugeObject();
               // and other initialization

         }

         static HugeObject theObject;
        
         public static HugeObject TheObject { get{ return theObject; } }
    }

    Now in your service call you can do X12Check( HugeObjectHolder.TheObject );

    Does that work for you?


    Richard Blewett, thinktecture - http://www.dotnetconsult.co.uk/weblog2
    Thursday, April 9, 2009 3:52 PM
  • The second part is no brainer which is how to call X12Check and pass my object but where and how would you instantiate the HughObjectHolder class?
    Thanks
    Thursday, April 9, 2009 4:11 PM
  • Thats the point - you don;t need to  - its a static class
    Richard Blewett, thinktecture - http://www.dotnetconsult.co.uk/weblog2
    Thursday, April 9, 2009 4:17 PM
  • I am sorry, I am not following you. What you're saying is as if the initializating process will be called everytime I call Validate(). is that right? if not, can you show me how to start the initializating process on windows load event or when I start the service?.

    Thursday, April 9, 2009 4:30 PM
  • The initialization will occur when the runtime initializes the type (not create an instance). In other words it will happen once and once only (unless you are running mulitple AppDomains). Thats what the static constructor is for - once per type initialization. After that you will be accessing the same instance of the HugeObject everytime you use the TheObject property.

    With an explicit static constructor it will probably run when the JIT compiler first encounters the type


    Richard Blewett, thinktecture - http://www.dotnetconsult.co.uk/weblog2
    Thursday, April 9, 2009 5:06 PM
  • Thanks for the explanation, last question: will it matter if the HugeObjectHolder class is inside the WCF Services (Implementation) or outside?
    Thursday, April 9, 2009 5:17 PM
  • No that makes no difference


    Richard Blewett, thinktecture - http://www.dotnetconsult.co.uk/weblog2
    Thursday, April 9, 2009 5:19 PM
  • But When I put a breakpoint inside the static class constructor, it hits only when Validate() is called. Can you comfirm that's what is being done?
    Thursday, April 9, 2009 5:24 PM
  • I think I understand now, as long as the service is running, all the subsequent calls will be able to reuse the object. However, the first time the service is up, it will take some time to initialize the object for the first call. I ran a diagnostic test between and the difference is hugh between the first call and all the calls that came after.
    Thank you so much, You've saved the day.


    Thursday, April 9, 2009 5:35 PM
  • Hi,

    I am also facing same problem. I tried with the xmlSerializer, that pre-generates the serialization assembly, but again I am facing startup time issue.

    Monday, June 14, 2010 6:26 AM
  • Can I just clarify - you are finding that the service takes a long time to start up?

    Are you self hosting or hosting in IIS?


    Richard Blewett, thinktecture - http://www.dotnetconsult.co.uk/weblog2
    Twitter: richardblewett
    Monday, June 14, 2010 8:13 AM
  • Thanks for quick reply Richard,

    I am hosting service in IIS 7.

    Tuesday, June 15, 2010 5:27 AM
  • I guess first of all we have to set expectations: if the worker process isn't already running then activation does require a fair amount of work. How long a delay are you experiencing?


    Richard Blewett, thinktecture - http://www.dotnetconsult.co.uk/weblog2
    Twitter: richardblewett
    Tuesday, June 15, 2010 7:00 AM
  • Yes, the activation time is acceptable.

    But if the service is idle for 20 minutes, (that is no one is hitting service for 20 min), response time is high(nearly equal to startup time).

    Can this time be reduced?

    Tuesday, June 15, 2010 7:29 AM
  • It looks as if you are hitting the idle time for the worker process so IIS stops it. The next request incurs the start up time for the worker process again. You can configure the idle time in the advanced properties of the application pool in IIS manager


    Richard Blewett, thinktecture - http://www.dotnetconsult.co.uk/weblog2
    Twitter: richardblewett
    Tuesday, June 15, 2010 9:06 AM
  • Thanks.

    I used "Idle time out" property of the IIS application pool, and it solves my problem.

     

    Monday, June 21, 2010 4:30 AM
  • Hi Richard

    I've seen your nice responses above and thought you would be helpful in our situation.

    We go live next month and we are performance testing our services.

    On analysis, I found that we have a similar problem and here are the characteristics:

    - Our service response times are much high during the first call and after idle times.

    - We are trying to log the time by capturing the time when we start processing the request and after we have done the processing (like calling other services or data sources) and just before sending back the response. This time is much lower than the actual response time seen using other tools at the Client side. And there is a huge difference during the first time or after a gap.

    - For subsequent calls, the response is much faster.

    The first difference in time... suggests may be there is an idle timeout issue. I'll check this on the server.

    But, the second one suggests it could be due to the XMLSerializer that we are using. I've read in other places that XMLSerializer is slow on initialization.

    Do you think these are the reasons? What do you recommend to solve this issue ?

    Thanks in advance,

    rkven.

     

     

     

     

     

     

     

    Friday, July 16, 2010 12:38 AM