What could be causing a "Cannot access a disposed object" error in WCF?

The problem here (which I think Conrad missed) is that Kerezo is closing the ChannelFactory (Srv_LoginChannelFactory) which closes (disposes) all its channels, when he probably wants to close just the Channel (LoginService).

So change:

    Srv_LoginChannelFactory.Close();

to:

    try
    {
        LoginService.Close();
    }
    catch
    {
        LoginService.Abort();
    }

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?)