Results 1 to 13 of 13

Thread: Starting and Stopping a WCF Service

  1. #1

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Starting and Stopping a WCF Service

    Hi,

    I have a WCF service, which I can open and close by doing:
    Code:
    Dim myService As MyServicehost = New MyServiceHost()
    myService.Open()
    
    myService.Close()
    the issue is that while the MyServicehost is operational, ie up and running, I need to destroy it, and create a new service host:
    Code:
    Dim myService As MyServiceHost = New MyServiceHost()
    myService.Open()
    
    Dim newService As MyServiceHost = New MyServiceHost()
    myService.Close()
    newService.Open()
    The problem with this is I have "downtime" of the service in between myService.Close() and newService.Open(). if anyone tries to connect while this is happening it fails.

    how can I make the "downtime" as small as possibel?

    Make sense?

    Woka

  2. #2
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611

    Re: Starting and Stopping a WCF Service

    You could try this:
    Code:
    Dim myService As MyServiceHost = New MyServiceHost()
    myService.Open()
    
    Dim newService As MyServiceHost = New MyServiceHost()
    newService.Open()
    myService.Close()
    But I haven't validated to check if this works, it could fail because the 2 servicehosts are both active. Why do you need the recreate the service anyway?
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

    Free online tools

  3. #3

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Starting and Stopping a WCF Service

    You cannot open 2 services at the same time.
    You get an error saying that 2 listeners cannot listen to the same port.
    So I must stop one service 1st...or delete it's endpoint listener.

    I need to do this because I need to refresh cache in the service.

    Woka

  4. #4
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611

    Re: Starting and Stopping a WCF Service

    In that case the simplest thing to do, I think, would be to write a method to clear cache. This can, of course, only work if you handle the caching yourself.
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

    Free online tools

  5. #5

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Starting and Stopping a WCF Service

    lol.
    I knew you were gonna say that
    It's not possible do clear the "cache" as the cache is assemblies loaded into a new AppDomain, which needs to be destroyed and thus I must create a whole new servicehost in a new AppDomain.

    Woka

  6. #6
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611

    Re: Starting and Stopping a WCF Service

    IF it is possible you could host the service in the IIS so you can recycle the "worker processes". The services is always available, but will get restarted.

    But only if you can host in the IIS
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

    Free online tools

  7. #7

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Starting and Stopping a WCF Service

    Hmmm not sure I want to host in IIS tbh, windows service all the way.
    I just need to make sure the "downtime" is as small as possible between .Close() and the new .Open().

    Woka

  8. #8
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611

    Re: Starting and Stopping a WCF Service

    Maybe the best to do is to create the "second" servicehost, but without endpoint configuration --> stop the first servicehost--> add the endpoint configuration, that should not take more than a few ms...
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

    Free online tools

  9. #9

  10. #10
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611

    Re: Starting and Stopping a WCF Service

    I don't know the exact code you use to create the service host. If you do this (C# but you should understand):
    Code:
     
                Uri baseAddress = new Uri("https://localhost");
    ServiceHost  myService = new ServiceHost(typeof(myserviceTypel), baseAddress);
    you create the service object but it doesn't listen to request until you do:
    Code:
                binding.Name = "myBinding";
                binding.HostNameComparisonMode = HostNameComparisonMode.WeakWildcard;
                binding.Security.Mode = BasicHttpSecurityMode.TransportWithMessageCredential;
    
                Uri address = new Uri("myServiceAddres" , UriKind.RelativeOrAbsolute);
     myService .AddServiceEndpoint(typeof(myServicePort), binding, address);
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

    Free online tools

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: Starting and Stopping a WCF Service

    By the way, depending on the type of connection, it is only by default that you can't have multiple listeners on a single port. I ran into the same issue when I wanted multiple processes to receive incoming UDP messages. The workaround was easy:

    http://www.vbforums.com/showthread.p...&highlight=UDP

    However, this may not be relevant to you. I have worked with WCF only a little, but you can get access to underlying objects, and maybe you can effect the same solution that I did (thanks to Atheist, I believe).
    My usual boring signature: Nothing

  12. #12

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Starting and Stopping a WCF Service

    hmmm...I tried similar coding to that the other day, but couldn't get it to work
    A few ms downtime is absolutely perfect!
    I will play around with this approach more this evening.

    Thanks

  13. #13
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Starting and Stopping a WCF Service

    There is a WCF specific forum by the way: http://www.vbforums.com/forumdisplay.php?f=86

    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width