none
Is this a good practice to close the client proxy? RRS feed

  • Question

  • I have a proxy client like that:

    public class WCFCalculadoraServiceProxy : ClientBase<ICalculadoraService>
        {
            public int Add(int num1, int num2)
            {
                try
                {
                    return base.Channel.Add(num1, num2);
                }
                catch
                {
                    throw;
                }
            }
    
        }


    In my client application I use the proxy in this way:

    public MainWindow()
    	{
    		try
    		{
    			InitializeComponent();
    
    			_proxy = new WPFCalculadoraServiceProxy.WCFCalculadoraServiceProxy();
    		}
    		catch(Exception ex)
    		{
    			MessageBox.Show(ex.Message);
    		}
    		
    	}
    	
    	
    	private void button_Click(object sender, RoutedEventArgs e)
    	{
    		try
    		{
    			int resultado = _proxy.Add(1, 2);
    		}
    		catch
    		{
    			MessageBox.Show("Error al sumar.");
    		}
    	}
    }

    _proxy is a class variable in the client application, that is instantiate at startup and never is closed until the application is closed.

    But I have read that the _proxy should be closed as soon as the method is finished, so I was thinking to modify the method in my client application to this:

    	private void button_Click(object sender, RoutedEventArgs e)
    	{
    		try
    		{
    			_proxy = new MyProxy();
    			int resultado = _proxy.Add(1, 2);
    			_proxy.Close();
    		}
    		catch
    		{
    			MessageBox.Show("Error al sumar.");
    			_proxy.Abort();
    		}
    	}

    But my doubt is if this a good practice, because I have instantiate a new proxy in each call, because I can not reopen because I get an exception, so I instantiate a new one.

    Is this a good practice? Or is it not so bad to keep alive the proxy and only close it when the client application is closed?

    Thanks so much.

    EDIT: the problem with this second solution, when I close the proxy in every method call, if a method is set as IsInitiating to false because first is needed to login into the service, I have never will can to call any method less login, because login close the proxy when it finished, so when I tried to call another method, I get an error that first I need to login.

    So, my question is, how can I close the proxy in each method call when I use the IsInitiating attribute in the methods of the service?


    Wednesday, December 9, 2015 12:00 PM

Answers

  • Hi ComptonAlvaro,

    According to your description, as far as I know, when we used the session mode

    as required. And set the  InstanceContextMode  as  PerSession. Then it will keep

    the session alive until you finished all operation in client. When you closed the client,

    the session will be closed. Also, we can closed it by manual.

    But, for different client, when we call the WCF service, the WCF service will create 

    different sessions for different clients  to correspond.

    For more information, please refer to the following articles:

    1.Sessions, Instancing, and Concurrency

    https://msdn.microsoft.com/en-us/library/ms731193(v=vs.110).aspx

    I hope that will be helpful to you.

    Best Regards,

    Wanjun Dong



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Friday, December 11, 2015 9:41 AM
    Moderator