Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. WCF ServiceHost shuts down when not in console application

WCF ServiceHost shuts down when not in console application

Scheduled Pinned Locked Moved WPF
csharpdatabasewcfquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    Yoav Ben Zvi
    wrote on last edited by
    #1

    Hi, I am just starting to learn WCF. In all the examples I've seen so far, the SerciveHost was initialized inside a console application and remained listening until the console was closed. I am trying to have it initiated from inside a winform app. I have a button which triggers this method: private void StartServer() { using (host = new ServiceHost(typeof(DataBase.DataBaseService ), new Uri("http://localhost:8000/DataBaseService"))) { host.AddServiceEndpoint(typeof( DataBase.IDataBaseService), new BasicHttpBinding(), "DataBaseService"); host.Open(); } } As soon as the method reaches the end, host.Status is set to Closed. Is there any way to stop this?

    P 1 Reply Last reply
    0
    • Y Yoav Ben Zvi

      Hi, I am just starting to learn WCF. In all the examples I've seen so far, the SerciveHost was initialized inside a console application and remained listening until the console was closed. I am trying to have it initiated from inside a winform app. I have a button which triggers this method: private void StartServer() { using (host = new ServiceHost(typeof(DataBase.DataBaseService ), new Uri("http://localhost:8000/DataBaseService"))) { host.AddServiceEndpoint(typeof( DataBase.IDataBaseService), new BasicHttpBinding(), "DataBaseService"); host.Open(); } } As soon as the method reaches the end, host.Status is set to Closed. Is there any way to stop this?

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      The problem you're facing here is that you are disposing of the host immediately after opening it. What you might want to do here is to make host a member instead, then initialise it in StartServer. In your Form_Closing method, you would want to close host.

      private _host = null;

      private void StartServer()
      {
      _host = new ServiceHost(typeof(DataBase.DataBaseService ), new Uri("http://localhost:8000/DataBaseService"));
      _host.AddServiceEndpoint(typeof(DataBase.IDataBaseService),
      new BasicHttpBinding(), "DataBaseService");
      _host.Open();
      }

      private void StopServer()
      {
      if (_host != null)
      {
      _host.Close();
      _host.Dispose();
      }
      }

      Call StopServer in the Form_Closing event.

      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

      My blog | My articles | MoXAML PowerToys

      Y 1 Reply Last reply
      0
      • P Pete OHanlon

        The problem you're facing here is that you are disposing of the host immediately after opening it. What you might want to do here is to make host a member instead, then initialise it in StartServer. In your Form_Closing method, you would want to close host.

        private _host = null;

        private void StartServer()
        {
        _host = new ServiceHost(typeof(DataBase.DataBaseService ), new Uri("http://localhost:8000/DataBaseService"));
        _host.AddServiceEndpoint(typeof(DataBase.IDataBaseService),
        new BasicHttpBinding(), "DataBaseService");
        _host.Open();
        }

        private void StopServer()
        {
        if (_host != null)
        {
        _host.Close();
        _host.Dispose();
        }
        }

        Call StopServer in the Form_Closing event.

        "WPF has many lovers. It's a veritable porn star!" - Josh Smith

        My blog | My articles | MoXAML PowerToys

        Y Offline
        Y Offline
        Yoav Ben Zvi
        wrote on last edited by
        #3

        I did have host as a member. I think the problem was using the 'using' statement. After looking at your code I dropped it and it works so thanks a lot.

        P 1 Reply Last reply
        0
        • Y Yoav Ben Zvi

          I did have host as a member. I think the problem was using the 'using' statement. After looking at your code I dropped it and it works so thanks a lot.

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          Not a problem. For your information - the using statement here triggers a Dispose when the code reaches the end of the code block (which was the point I was trying to make).

          "WPF has many lovers. It's a veritable porn star!" - Josh Smith

          My blog | My articles | MoXAML PowerToys

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups