Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3633412
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:38:59+00:00 2026-05-19T00:38:59+00:00

I am using the following code: private WSHttpBinding ws; private EndpointAddress Srv_Login_EndPoint; private ChannelFactory<Srv_Login.Srv_ILogin>

  • 0

I am using the following code:

private WSHttpBinding ws;
private EndpointAddress Srv_Login_EndPoint;
private ChannelFactory<Srv_Login.Srv_ILogin> Srv_LoginChannelFactory;
private Srv_Login.Srv_ILogin LoginService;

The Login is my constructor:

public Login()
        {
            InitializeComponent(); 
            ws = new WSHttpBinding();
            Srv_Login_EndPoint = new EndpointAddress("http://localhost:2687/Srv_Login.svc");
            Srv_LoginChannelFactory = new ChannelFactory<Srv_Login.Srv_ILogin>(ws, Srv_Login_EndPoint);
        }

And I’m using service this way:

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();
    }
}

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:

{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()

How can I fix my code to prevent this error from occurring?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T00:39:00+00:00Added an answer on May 19, 2026 at 12:39 am

    Srv_LoginChannelFactory.Close() is where it’s being disposed. When you call close you are giving up whatever unmanaged resource you had. Attempting to do something other then inspecting its state or re-opening it results in the “Cannot access a disposed object” exception.

    This is true whenever you close a disposable object and try and do something with it afterwards. For example writing to a file that’s closed, or executing a sql statement on a closed database connection.

    To address this you have three options.

    1. Don’t make the Srv_LoginChannelFactory a field. Instead make it local to the button click. If this is the only place you are using it, this probably makes sense to do because it shortens the amount of time you are using an unmanaged resource.

    2. Implement IDisposable (you are supposed do this whenever you have field that is Disposable) don’t close Srv_LoginChannelFactory except in Login.Dispose.

    3. Change the button click to check the State of Srv_LoginChannelFactory before you try and create a channel with it. You still need to implement IDisposable in case the button click doesn’t happen.

    Note: EnsureOpened looks like it could be used to check the state, but it only works before its opened. Once its been closed it will throw.

    Regarding Close() being the same as Dispose.

    From the section ‘Customizing a Dispose Method Name’ in Implementing Finalize and Dispose to Clean Up Unmanaged Resources in the Design Guidelines for Developing Class Libraries

    Occasionally a domain-specific name is
    more appropriate than Dispose. For
    example, a file encapsulation might
    want to use the method name Close. In
    this case, implement Dispose privately
    and create a public Close method that
    calls Dispose. The following code
    example illustrates this pattern. You
    can replace Close with a method name
    appropriate to your domain. This
    example requires the System namespace.

    The idea here is to give parity to the Open method. Personally I think it causes a lot of confusion, but I can’t think of anything better (CloseAndDispose?)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making a GUI using SpringLayout using the following code: private void createAndShowGUI()
I want to draw some sort graphics on winform using following code : private
Hi I have some strings generated using the following code: private static string CalcHashCode(byte[]
I am using a spinner using following code private String `DEFAULT_CURRENCY_TYPE`; ArrayAdapter<CharSequence> adapter =
I'm trying to set an alarm in windows phone using the following code private
I'm trying to alter how a combobox is displayed using the following code: private
I have the following code: private void Process(string path) { using (FileStream fs =
I'm having an issue with the following code: private void DataPortal_Fetch(TaskCriteria criteria) { using
I want to update XDocument using the following code private static bool ResetUpdateVersion() {
I try to close my browser in C# using the following code: private void

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.