title: What could be causing a "Cannot access a disposed object" error in WCF?
body: <p>I am using the following code:</p>
  
  <pre><code>private WSHttpBinding ws;
  private EndpointAddress Srv_Login_EndPoint;
  private ChannelFactory&lt;Srv_Login.Srv_ILogin&gt; Srv_LoginChannelFactory;
  private Srv_Login.Srv_ILogin LoginService;
  </code></pre>
  
  <p>The Login is my constructor:</p>
  
  <pre><code>public Login()
          {
              InitializeComponent(); 
              ws = new WSHttpBinding();
              Srv_Login_EndPoint = new EndpointAddress("http://localhost:2687/Srv_Login.svc");
              Srv_LoginChannelFactory = new ChannelFactory&lt;Srv_Login.Srv_ILogin&gt;(ws, Srv_Login_EndPoint);
          }
  </code></pre>
  
  <p>And I'm using service this way:</p>
  
  <pre><code>private void btnEnter_Click(object sender, EventArgs e)
  {
      try
      {
  
          LoginService = Srv_LoginChannelFactory.CreateChannel();
          Srv_Login.LoginResult res = new Srv_Login.LoginResult();
          res = LoginService.IsAuthenticated(txtUserName.Text.Trim(), txtPassword.Text.Trim());
          if (res.Status == true)
          {
              int Id = int.Parse(res.Result.ToString());
          }
          else
          {
              lblMessage.Text = "Not Enter";
          }
      }
      catch (Exception ex)
      {
          MessageBox.Show(ex.Message);
      }
      finally
      {
          Srv_LoginChannelFactory.Close();
      }
  }
  </code></pre>
  
  <p>When the user enters a valid username and password, everything is fine. When the user enters a wrong username and password, the first try correctly displays a "Not Enter" message, but on the second try, the user sees this message:</p>
  
  <pre><code>{System.ObjectDisposedException: Cannot access a disposed object.
  Object name: 'System.ServiceModel.ChannelFactory`1[Test_Poosesh.Srv_Login.Srv_ILogin]'.
     at System.ServiceModel.Channels.CommunicationObject.ThrowIfDisposed()
     at System.ServiceModel.ChannelFactory.EnsureOpened()
     at System.ServiceModel.ChannelFactory`1.CreateChannel(EndpointAddress address, Uri via)
     at System.ServiceModel.ChannelFactory`1.CreateChannel()
  </code></pre>
  
  <p>How can I fix my code to prevent this error from occurring?</p>