locked
WCF WebScoket Service doesn't open after Close RRS feed

  • Question

  • Hi

    I have a WCF service that uses WebSocket with following binding

    <netHttpBinding>
      <binding>
        <webSocketSettings/>
        <security mode="None" />
      </binding>
    </netHttpBinding> 

    The client is C# generated Service Reference.

    Once the Client calls the CloseAsync() on service it cannot be opened anymore with OpenAsync(). 

    It throws ObjectDisposedException from System.ServiceModel.dll

    My usecase is, that on App Suspending event i try to close the Network Connection and on Resuming event i try to reopen that connection which fails.

    Should i even close the connection(websocket) on Suspending event?

    Thanks

    peter

    Friday, October 25, 2013 3:14 PM

All replies

  • Can you post the code you're using to open and close the socket?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, October 25, 2013 7:17 PM
    Moderator
  • Hi Matt

    public sealed partial class MainPage : Page
    {
      private ActiveServiceReference.ServiceClient client = new ActiveServiceReference.ServiceClient();
    
    	bool started = false;
    
            private async void OnStart(object sender, RoutedEventArgs e)
            {
                if (!started)
                {
                    client.UpdateReceived += client_UpdateReceived;
    
                    try
                    {
                        await client.OpenAsync();
                        await client.SubscribeAsync();
    
                        started = true;
                    }
                    catch (Exception ex)
                    {
                        this.errors.Text += string.Format("{0}\n", ex.ToString());
                    }
                }
            }
    
            private async void OnStop(object sender, RoutedEventArgs e)
            {
                if (started)
                {
                    client.UpdateReceived -= client_UpdateReceived;
    
                    try
                    {
                        await client.UnSubscribeAsync();
                        await client.CloseAsync();
    
                        started = false;
                    }
                    catch (Exception ex)
                    {
                        this.errors.Text += string.Format("{0}\n", ex.ToString());
                    }
                }
            }
    
    
    

    Basically I call OpenAsync(), CloseAsync() and after a while OpenAsync() again.  OpenAsync after CloseAsync fails.

    Wrote sample project on https://skydrive.live.com/redir?resid=97B9095A0159A8EB!1666 called WebSockSample.

    WebSockSample is WCF WebSocket Server that sends every 5 sec a random number to C# Store Client.

    In Client press Start , Stop and Start again. On 2nd Start it fails.

    Thank you

    Saturday, October 26, 2013 11:16 AM