Click to See Complete Forum and Search --> : [RESOLVED] WCF realtime


dannystommen
September 29th, 2008, 03:20 AM
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..

Arjay
September 29th, 2008, 04:25 AM
See this link:
http://69.10.233.10/KB/WCF/WCF_Duplex_UI_Threads.aspx

dannystommen
September 29th, 2008, 07:27 AM
thank you for the example, just what I needed.
I only don't don't understand the next method


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?

Arjay
September 29th, 2008, 12:25 PM
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.

dannystommen
September 30th, 2008, 02:32 AM
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?

Arjay
September 30th, 2008, 12:03 PM
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).

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.

Arjay
September 30th, 2008, 12:08 PM
I think I understand what you are asking.

The two strings are concatinated into the state value, in this line:
_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.

dannystommen
October 1st, 2008, 04:06 AM
Yes that was what I meant. Thank you

dannystommen
October 3rd, 2008, 07:55 AM
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

<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:

_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

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?

Arjay
October 3rd, 2008, 02:17 PM
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.

TheCPUWizard
October 3rd, 2008, 03:22 PM
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

lock (_callbackList) {

Around the Removal code also, any you must make sure that both are locking on the same object.