How to enumerate and access client channels on a WCF server

Is there a way to enumerate connections on a WCF server

I have a scenario where I'd like some code on the server to be able to get a
list of all the connected clients, e.g. to display which clients are
connected. But more than that, I'd like to be able to add custom state
information to connected client channels and have a service method then have access to all the other client channels which are connected to the server.

I've looked at the IExtensibleObject<T> but it's not clear from the channel
extensibility example how this should be used.

Any suggestions anyone

Cheers,

MikeS.


Answer this question

How to enumerate and access client channels on a WCF server

  • Sudhir Chawla

    Uhm, ServiceHost.GetInstanceContexts() is not a public method on ServiceHost...

  • DimmuZangetsu

    Hi Mike,

    You can call ServiceHost.GetInstanceContexts() to get a list of the active InstanceContext objects. Depending on your InstanceContextMode setting, you may have a single instance, one per client session, or one per client call.

    You can then enumerate the set of "client channels" associated with an instance by accessing the InstanceContext.IncomingChannels collection.

    The InstanceContext class implements IExtensibleObject so you can attach state to it. The channels in the IncomingChannels collection all implement IExtensibleObject so you can also attach state to them.

    Regards,
    David



  • Chris Marts

    Hi Ploeh,

    As you mentioned you can use the IExtension<InstanceContext> to attach data off to each InstanceContext created.

    Your extension will hold a reference to a global class and that global class will then have access to all InstanceContext's. Check this sample out

    http://msdn2.microsoft.com/en-us/library/ms733816.aspx



  • a9192shark

    Hi Mike,

    Oops. You're quite right that GetInstanceContexts is not public. My mistake.

    It looks like Mahesh was able to offer another solution. I hope that works for you.

    Regards,
    David



  • How to enumerate and access client channels on a WCF server