10

I have a method CreateContextForGlobalCatalog that returns a PrincipalServer that connects to a global catalog:

PrincipalContext = new PrincipalContext(ContextType.Domain,
                                        "forest.name:3268", 
                                        "dc=forest,dc=name", 
                                        ContextOptions.Negotiate, 
                                        userName, password);

Note: That is a reduced version of the method, normally the name and the container are parameters.

With this context I'm looping over objects from the database to get information from the global catalog in ActiveDirectory:

using (PrincipalContext principalContext = CreateContextForGlobalCatalog())
{
    foreach (ADAccount adAccount in accounts){
       Log.Debug("Connected server: " + principalContext.ConnectedServer);

    // get some information from AD here ...
    }
}

The Log.Debug line logs the connected server from the PrincipalContext. I have a test setup containing only virtual machines.

My Problem: When I now disconnect that connected server (disable network adapter) I don't get an exception and get connected to a new server but the log messages still shows the original connected server albeit the server is not available anymore.

Is there a way to somehow refresh the connected server property or to get the information from somewhere else?

Community
  • 1
  • 1
Ocaso Protal
  • 19,362
  • 8
  • 76
  • 83
  • Obviously you have to implement your own *ChangeTracker* Have a look [here](http://stackoverflow.com/a/2002763/4558029) and see if this helps – lokusking Aug 10 '16 at 18:11
  • you could just check, if your network is connected ? like this: System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable() – ralf.w. Aug 10 '16 at 20:07
  • @lokusking Haha, in fact I'm doing a lot with change notifications and they are really a solution for the underlying problem of synching objects. But this time I just want to get the new `ConnectedServer`. – Ocaso Protal Aug 11 '16 at 05:30
  • @ralf.w. The problem is not that the network is disconnected. When I disconnect the machine my code is running on, which means I cut of all GCs, I get an exception. But if I only disconnect the GC I'm currently connected to, the AD and the PrincipalContext switches to anoter GC and I want to get the information which GC is used. – Ocaso Protal Aug 11 '16 at 05:33

2 Answers2

2

unfortunately, It will never be updated after initiation, the only workaround you can do is to recheck with every iteration if you are still connected or not

Hussein Khalil
  • 1,395
  • 11
  • 29
  • I am connected, otherwise there would be an exception. I just want to know to which server I'm connected. But seeing the shiploads of answers to this question ;-) it looks as it is really impossible – Ocaso Protal Aug 16 '16 at 05:27
0

The most simple solution might be to try and ping the remote server using .Net's Ping class.

Itay Podhajcer
  • 2,616
  • 2
  • 9
  • 14
  • Sorry, that doesn't solve my problem: When connecting to GC and the server that I'm automatically connected to gets lost I get connected to another server. And I want to know the adress of that server, but I only get the adress of the first server. Sorry if you misunderstood my question. Updated the question. – Ocaso Protal Aug 17 '16 at 09:50