none
How to close WebServiceHost which is in Opening State? RRS feed

  • Question

  • Hi,

    i am using WebServiceHost  for connection to a uri.

    the created WebServiceHost is in OPENING state for long time.

    How can we Close the Webservicehost when it is in opening state ?

    Thursday, November 2, 2017 9:06 PM

All replies

  • Hi,

    i am using WebServiceHost  for connection to a uri.

    the created WebServiceHost is in OPENING state for long time.

    How can we Close the Webservicehost when it is in opening state ?

    Recycle the App Pool?

    Thursday, November 2, 2017 9:31 PM
  • Hi PM16,

    Could you share us your code?

    Based on Windows Forms App WebServiceHost Open() event dows not retrun, it seems your issue happens when the URI does not exist. But, I fail to reproduce your issue with below code.

            private void SelfHost_Click(object sender, EventArgs e)
            {
                Uri baseAddress = new Uri("http://invalidhost:6666");
                WebServiceHost host = new WebServiceHost(typeof(SelfHostService), baseAddress);
                host.Open();
                MessageBox.Show("Service is Open");
                host.Close();
            }
    

    No matter what value I set for baseAddress, it will open correctly.

    I am afraid we could not Close the WebServiceHost when it is in Opening state. If this issue only happens under the none-exist URI, I would suggest you check whether the URI is exist before WebServiceHost open.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 3, 2017 2:07 AM
  • Hi,

    In my app,  i have just used the same code as yours.

    If webservicehost  is not successfully opened because of any exception  I Close / Abort based on the state of webservicehost . Then every second i am checking  whether the host is successfully opened.

    If not opened, then again try to open the host.

     

    Friday, November 3, 2017 12:49 PM
  • Hi,

    In my app,  i have just used the same code as yours.

    If webservicehost  is not successfully opened because of any exception  I Close / Abort based on the state of webservicehost . Then every second i am checking  whether the host is successfully opened.

    If not opened, then again try to open the host.

     


    It doesn't do any good if the service is hosed and not responding. 
    Friday, November 3, 2017 1:24 PM
  • Hi PM16,

    >> If webservicehost  is not successfully opened because of any exception  I Close / Abort based on the state of webservicehost .

    Do you get any exception while trying to open WebServiceHost?

    Based on your original post, it seems your issue is related with WebServiceHost open for a long time. Based on your follow up, it seems your issue is related with exception while opening WebServiceHost.

    Do you get any error with my above code?

    Could you share us a simple demo which could reproduce your issue?

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 6, 2017 1:54 AM
  • Hi,

    >> Do you get any exception while trying to open WebServiceHost?

    No, I do not get any exception :(

    Thanks.

    Monday, November 6, 2017 3:00 AM
  • Hi PM16,

    If no exception, what is your issue? Do you mean it wastes a long time with my above code?

    How long did you wait? And did you wait such long time every time?

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 6, 2017 3:09 AM
  •    

    Hi,

    1. I try to open a uri using WebServiceHost as below code.

    public partial class TestHostApp : Form
    {
        WebServiceHost host = null;
        public System.Windows.Forms.Timer checkHostTimer;
        bool startHostFlag = false;
        bool stopIHostFlag = false;
        bool hostOpenedFlag =  false;

        public TestHostApp()
        {
            InitializeComponent();

            StartHost();

            checkHostTimer = new System.Windows.Forms.Timer();
            checkHostTimer.Interval = 1000;
            checkHostTimer.Enabled = true;
            checkHostTimer.Start();
            checkHostTimer.Tick += new System.EventHandler(this.checkHost_timer_tick);
        }

        private void checkHost_timer_tick(object sender, EventArgs e)
        {
            if(host == null || hostOpenedFlag == false)
            {
                StopHost();
                StartHost();
            }
        }
        void StartHost()
        {
            if(startHostFlag == true)
            {
                return;
            }
            try
            {
                startHostFlag = true;
                Uri baseAddress = new Uri("http://invalidhost:6666");
                host = new WebServiceHost(typeof(SelfHostService), baseAddress);
                host.Open();
                hostOpenedFlag = true; //this flag never set to true, as opening invalid uri always fails
                MessageBox.Show("Service is Open");            
            }
            catch(Exception e)
            {
                //Write the exception log to a text box.

                if(host != null)
                {
                    if(host.State != System.ServiceModel.CommunicationState.Faulted)
                    {
                        host.Close();
                    }
                    else
                    {
                        host.Abort();
                    }
                }
    host = null; // i just set the host as null here.

            }

            startHostFlag = false;
        }

        void StopHost()
        {
            if(stopIHostFlag == true)
            {
                return;
            }
            try
            {
                stopIHostFlag = true;
                if(host != null)
                {
                    host.Close();
                }

                host = null;
                hostOpenedFlag = false;

            }
            catch(Exception e)
            {
                //Write the exception log to a text box.
            }

            stopIHostFlag = false;
        }
    }

                                                                            


    2. host.Open() works for sometime when i try the StartHost()  from timer. that is host.Open() responds after 2 to  3secs. from my test. I am just trying the StartHost() from timer continuously, after 20 to 30 mins, the host.Open() just hangs. I waited for more than 30 mins and it never responds.  I do not even get any exception.

    I do not know what is wrong there.







    • Edited by PM16 Tuesday, November 7, 2017 5:08 AM
    Monday, November 6, 2017 5:19 AM
  • Hi PM16,

    What do you mean by “host.Open() just hangs”?

    After “StartHost()” first run, it will not hit “host.Open();” again since host is not null and hostOpenedFlag is not false.

    I made a test with your code, it works over one hour, and when I add breakpoint on “if(startHostFlag == true)”, it is true, and return directly as expected result.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 7, 2017 4:47 AM
  • Hi Edward Z,
    1. As opening a invalid uri always fails i.e. host.Open() never succeeded, "hostOpenedFlag" never set to true

    2.  startHostFlag is set false at the end of StartHost(). As host.Open() fails, the startHostFlag always false at the end of the function.

    So, the "if" condition in timer always success and try to hit StartHost().

    Tuesday, November 7, 2017 5:15 AM
  • Hi,

    As my test, host.Open() succeeded and hostOpenedFlag is true with an invalid uri.

    Result as below

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 7, 2017 5:21 AM