Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

Another WCF polling/duplex question. How to send a "br... RSS

16 replies

Last post Jan 13, 2012 07:00 AM by Piyush Dixit

(0)
  • prujohn

    prujohn

    Contributor

    3609 Points

    713 Posts

    Another WCF polling/duplex question. How to send a "broadcast" message?

    Oct 18, 2008 03:17 AM | LINK

    I've got a basic handle about how to send/receive messages.  I'm wondering if it's possible to send a "broadcast" message to all clients. 

    I tried to implement a model where each server-side instance would subscribe to an event, which when fired would then send a message to all of the service clients just fine.  However, when one of the client drops (browser closes, or other disconnect).  This technique causes the broadcasting component of the service to hang after a while (other send/receive messages continue to work fine).  I'm not sure how to detect this, and then clean up and unsubscribe from the event.

    I guess I'm asking if there is a better way to do accomplish this?

    Thanks,
    John
    LUCA Studios
  • bartczernicki

    bartczernicki

    Contributor

    5212 Points

    953 Posts

    Re: Another WCF polling/duplex question. How to send a "broadcast" message?

    Oct 18, 2008 06:41 AM | LINK

    Sockets might be a better option (if you can use it).  I have seen a demo of a teacher-student prototype (nothing fancy) where the lecture is given on one Silverlight main screen and the other clients (students) all receive the same view.  Behind the scenes all that is happening is that the mouse coordinates are being sent to the students and everyone has the same app just that the teacher is "controlling" what the client sees.  There was another demo of basketballs scores using sockets for this kind of broadcasting type scenario as well.

    In your scenario, I wonder if you can somehow send a kill/dispose command to the service via a Javascript AJAX call to your main service to tell it someone is closing the browser.  I have never implemented this type of thing, but I would guess that WCF Duplex Service has some data structure for maintaining the list of clients it is sending messages too.

    I am leaving the option that there just might be something you can do with the code (maybe there is a bug on your end) and not get that hang up.  I know the WCF Duplex support is not the true duplex in WCF 3.x, so there might be some weird anomalys.

    Don't forget to "Mark as Answer"
    Bart Czernicki
    http://www.silverlighthack.com | My new Silverlight and Business Intelligence Book
  • jdstuart

    jdstuart

    Member

    23 Points

    21 Posts

    Re: Another WCF polling/duplex question. How to send a "broadcast" message?

    Oct 18, 2008 01:38 PM | LINK

    In the interface of the duplex client you specify the method that is called when you send stuff back to the client. Make this a synchronous call, then the server won't block on the thread waiting to send data to a client that doesn't exist.
  • prujohn

    prujohn

    Contributor

    3609 Points

    713 Posts

    Re: Another WCF polling/duplex question. How to send a "broadcast" message?

    Oct 18, 2008 04:32 PM | LINK

    bartczernicki

    In your scenario, I wonder if you can somehow send a kill/dispose command to the service via a Javascript AJAX call to your main service to tell it someone is closing the browser.  I have never implemented this type of thing, but I would guess that WCF Duplex Service has some data structure for maintaining the list of clients it is sending messages too.

    After futher testing, what I am really after is what I have quoted above, which is a way to detect when the service host (the service-side of the contract) is not longer available, before I attempt to make a call to it's client endpoint.

    So far I've implemented the broadcasting scheme using two different techniques:

    Technique #1:  Each service host subscribes to an event from a static class.  When this event fires, the service sends the message to client.

    Technique #2.  Each service host registers to a static class Dictionairy with it's client object.  When a broadcast message is sent, this class iterates through each client and sends the message.

    Both of these work fine, up until the point when one of the clients disconnects (browser closed).  Now (in either case) I have a reference to a service host's client object that is no longer valid.  So the question now becomes:  How do I detect this condition server-side, so that I can remove either the 1) event from the invocation list, or 2) remove it's registration from the Dictionary?  Or perhaps there is a better technique...

    Thanks,
    John
    LUCA Studios
  • pbromberg

    pbromberg

    Contributor

    3138 Points

    531 Posts

    Re: Another WCF polling/duplex question. How to send a "broadcast" message?

    Oct 19, 2008 12:41 AM | LINK

    WCF duplex polling is not designed to provide support for "broadcast" messages. What you are describing is more like Multicast IP, but I doubt that would ever be supported in Silverlight.  What you could do is have a stateful "chat server" with each client polling to get the refreshed list of messages.

    [C# MVP]
    Eggheadcafe.com
  • prujohn

    prujohn

    Contributor

    3609 Points

    713 Posts

    Re: Another WCF polling/duplex question. How to send a "broadcast" message?

    Oct 19, 2008 02:44 AM | LINK

    pbromberg

    WCF duplex polling is not designed to provide support for "broadcast" messages. What you are describing is more like Multicast IP, but I doubt that would ever be supported in Silverlight.  What you could do is have a stateful "chat server" with each client polling to get the refreshed list of messages.

    Yup just implemented that pattern today.  While it is more overhead to code the message queue/dequeue system (and more chatty I would suspect), I'm still staying with duplex/polling for other types of communication - so that should work for now.

    Thanks,
    John
    LUCA Studios
  • robmaransky

    robmaransky

    Member

    24 Points

    34 Posts

    Re: Another WCF polling/duplex question. How to send a "broadcast" message?

    Oct 31, 2008 02:02 PM | LINK

    John,

    I have been attempting something similar to what you described. I have clients registering with the duplex service, then I store the callback channel in a static Dictionary. Within the Register method of my duplex service I subscribe to the channel's Faulted and Closing events so that the server is informed when the channel faults. The problem I am encountering is that when the channel faults, I have no way to identify which channel to remove from the Dictionary. There doesn't seem to be any useful information in the ICommunicationObject.Faulted event.

    Did you ever figure this out? Is there any way to uniquely identify the channel? I'm sure I must be missing something basic here.

    Thanks
    Rob

  • prujohn

    prujohn

    Contributor

    3609 Points

    713 Posts

    Re: Another WCF polling/duplex question. How to send a "broadcast" message?

    Oct 31, 2008 02:44 PM | LINK

    I haven't cracked this one yet.  I did implement the pattern recommended by the Silverlight Web Services Team (Eugene's sample chat application), and I do see some promising areas there where I might be able to hook into faulted channels, but I haven't had a chance to revisit it yet.   Please let me know if you do, and I'll do the same.

    Thanks,
    John
    LUCA Studios
  • robmaransky

    robmaransky

    Member

    24 Points

    34 Posts

    Re: Another WCF polling/duplex question. How to send a "broadcast" message?

    Oct 31, 2008 04:25 PM | LINK

    John,

    Thanks for that pointer to Eugene's sample. That's actually the first working sample I've seen that does something similar to what I want to do. Huge help! Thanks. I'll let you know if I find anything interesting.

    Rob

  • prujohn

    prujohn

    Contributor

    3609 Points

    713 Posts

    Re: Another WCF polling/duplex question. How to send a "broadcast" message?

    Nov 01, 2008 03:24 PM | LINK

    Glad you found it helpful.
    Thanks,
    John
    LUCA Studios