The different using "using" and not in service host

Last post 07-02-2009 7:34 PM by randallt. 3 replies.

Sort Posts:

  • The different using "using" and not in service host

    06-25-2009, 9:59 PM
    • Member
      76 point Member
    • ronald_yoh
    • Member since 10-27-2008, 2:37 AM
    • Posts 266

    does anyone know the diff between the two code:


    using (ServiceHost sh = new ServiceHost(typeof(ServiceLib.WCFServiceMath.MathService)))
    {
    	sh.Open();
    }
    

    ServiceHost sh;
    sh = new ServiceHost(typeof(ServiceLib.WCFServiceMath.MathService));
    sh.Open();
    
    


    The first one is not working correctly (meaning it does not hosting the endpoint - cannot find the service while trying to add the service reference)

    the second one is working fine.


    any advice?


  • Re: The different using "using" and not in service host

    06-26-2009, 2:09 AM
    • Contributor
      5,540 point Contributor
    • integrasol
    • Member since 06-05-2009, 3:18 PM
    • Posts 1,041

    I don't see any difference between the two pieces of code. However, I do need to check where you try to add the service reference, and how and where you dispose of the ServiceHost for the second piece of code.

    Thanks and HTH

    Carsten

    Please mark this post as Answer if it is of help to you. :-)
  • Re: The different using "using" and not in service host

    06-28-2009, 7:01 PM
    • Member
      76 point Member
    • ronald_yoh
    • Member since 10-27-2008, 2:37 AM
    • Posts 266

    I think i figured out what the problem was...

    at a glance, the two codes are looking the same but they're not... as you might have been aware in c# that the using keyword will call the disposal method (to execute the garbage collector) at the end of the statement which will return back the memory allocation (reference type) back to heap.

    In this case, the using keyword will call sh.close() once it reaches the closing block.

    let me know if you have any further comments.

  • Re: The different using "using" and not in service host

    07-02-2009, 7:34 PM
    • Member
      90 point Member
    • randallt
    • Member since 02-11-2009, 11:53 AM
    • Posts 30

     Yes Ronald, you are right.  The first code snippet opens the service host and then closes it right after as it exits the using block.

Page 1 of 1 (4 items)