CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    [RESOLVED] WCF realtime

    Does anyone know how to make C# WCF realtime.

    I have a WFC client ans host application and they work fine. But I want to make it realtime, but I don't know how. So, if a client makes a change to something, other clients should be updated. So the host need to hold a list with all the clients that are logged in, but I jave no idea to resolve this..

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

  3. #3
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: WCF realtime

    thank you for the example, just what I needed.
    I only don't don't understand the next method

    Code:
          public void NotifyBeerInventoryChanged(string guestName, int numberOfBeers) {
    
             string message = null;
             if (numberOfBeers > 0) {
                message = String.Format("{0} made a beer run and brought back {1} beers.", guestName, numberOfBeers.ToString());
             }
             else if (numberOfBeers < 0) {
                message = String.Format("{0} just chugged another beer.", guestName);
             }
    
             // The UI thread won't be handling the callback, but it is the only one allowed to update the controls.  
             // So, we will dispatch the UI update back to the UI sync context.
             SendOrPostCallback callback =
                 delegate(object state) {
                    string[] splitValues = state.ToString().Split('|');
                    this.BeerInventory += Convert.ToInt32(splitValues[0]);
                    this.WritePartyLogMessage(splitValues[1]);
                 };
    
             _uiSyncContext.Post(callback, String.Concat(numberOfBeers.ToString(), "|", message));
          }
    What I don't understand is "SendOrPostCallback callback =
    delegate(object state) {
    string[] splitValues = state.ToString().Split('|');
    this.BeerInventory += Convert.ToInt32(splitValues[0]);
    this.WritePartyLogMessage(splitValues[1]);
    };"


    why is splitValues[0] the value of this.BeerInventory and splitValues[1] the (local)variable message?

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: WCF realtime

    Quote Originally Posted by dannystommen
    why is splitValues[0] the value of this.BeerInventory and splitValues[1] the (local)variable message?
    Step through the code with a debugger and see the values. I don't think the use of split really matters with regard to the duplex functionality. The author just chose to pass in more data within a single string. Instead he could have just used two strings. This is simply an implementation detail with regard to chugging beers.

  5. #5
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: WCF realtime

    Quote:
    Step through the code with a debugger and see the values. I don't think the use of split really matters with regard to the duplex functionality. The author just chose to pass in more data within a single string. Instead he could have just used two strings. This is simply an implementation detail with regard to chugging beers.
    But where is the data passed in as a single string?? The method has two parameters? How is it possible that the local variable 'message' is in that single string?

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: WCF realtime

    Quote Originally Posted by dannystommen
    But where is the data passed in as a single string?? The method has two parameters? How is it possible that the local variable 'message' is in that single string?
    I'm not sure I'm following you.

    If you are talking about how the multiple parameters of NotifyBeerInventoryChanged get put into the message variable, it happens at the start of the method (i.e notice the two calls to String.Format).

    Code:
    public void NotifyBeerInventoryChanged(string guestName, int numberOfBeers) {
    
             string message = null;
             if (numberOfBeers > 0) {
                message = String.Format("{0} made a beer run and brought back {1} beers.", guestName, numberOfBeers.ToString());
             }
             else if (numberOfBeers < 0) {
                message = String.Format("{0} just chugged another beer.", guestName);
             }
    Again, this should be obvious if you step through the code in a debugger.

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: WCF realtime

    I think I understand what you are asking.

    The two strings are concatinated into the state value, in this line:
    Code:
    _uiSyncContext.Post(callback, String.Concat(numberOfBeers.ToString(), "|", message));
    Btw, this is one approach.

    Another approach (rather than to concatinate the data into a single string) is to pass a class or a struct as the state param). That way on the receiving end, you don't need to parse the string to get the variable data back out.
    Last edited by Arjay; September 30th, 2008 at 12:11 PM.

  8. #8
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: WCF realtime

    Yes that was what I meant. Thank you

  9. #9
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: [RESOLVED] WCF realtime

    I have a question about the threadsafety of the _callbackList of the example.

    On the MSDN site you can read the next about the List(T) Class:
    Thread Safety
    Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

    Next, I've set the maxConcurrentInstances to 10 in the app.config file
    Code:
    <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior name="MetadataBehavior">
              <serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:8001/BeerInventory/mex" />
              <serviceThrottling
                   maxConcurrentCalls     = "10"
                   maxConcurrentSessions  = "10"
                   maxConcurrentInstances = "10"
                />
            </behavior>
          </serviceBehaviors>
        </behaviors>
        ...
    For updating the clients the next statement is used:
    Code:
    _callbackList.ForEach(
                 delegate(IBeerInventoryCallback callback) { callback.NotifyBeerInventoryChanged(guestName, -1); });
    What happens when someone Leaves the party while the foreach loop is running. Normally you get an execption that the collection has changed. I can not really test it because the small number of clients that are connected.

    To make a list thread safe the lock statement must be used.

    So I tried the next
    Code:
             lock (_callbackList) {
                Thread.Sleep(3500);
                _callbackList.ForEach(
                    delegate(IBeerInventoryCallback callback) { callback.NotifyBeerInventoryChanged(guestName, -1); });
             }
    During the time of the sleep I click another client to leave the party (so the client is removed from the _callbackList). When the Thread.Sleep has finished, the client is allready been removed from the list although the list had been locked.

    Does anybodu know how this exactly works?

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: [RESOLVED] WCF realtime

    Put some Debug.WriteLine statements in the related methods (and perhaps listing the ThreadIDs).

    Then look at the debug spew in the Output window.

    Sometimes the order that things occur may surprise you and the debug spew can be helpful in figuring this out.

  11. #11
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: [RESOLVED] WCF realtime

    Quote Originally Posted by dannystommen
    During the time of the sleep I click another client to leave the party (so the client is removed from the _callbackList). When the Thread.Sleep has finished, the client is allready been removed from the list although the list had been locked.

    Does anybodu know how this exactly works?
    Yout must have
    Code:
             lock (_callbackList) {
    Around the Removal code also, any you must make sure that both are locking on the same object.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured