Reputation: 25
I Have a service that generate a Sudoku Game, the client should be Windows Phone User, I'm making online competition.
Question #1 Is how can I generate the Same Sudoku Game For all Clients (who access the service) in a specific time say in 20 minutes. I read about this and i try to use the following :
[ServiceBehavior (InstanceContextMode = InstanceContextMode.Single,
ConcurrencyMode = ConcurrencyMode.Single)]
but it isn't working properly.
Question # 2 is how to close the service for all clients after specific time.
thanks.
Upvotes: 0
Views: 77
Reputation: 2597
The default behaviour of a WCF service, as you have probably figured out, will create a new instance of the service implementation for every call. This is intentional, as the context may be different depending on the identity of the client. I would recommend not trying to change this behavior.
As Guanxi said, a good approach is to implement a static cache - like a singleton, which re-generates it's self after a timeout of 20 minutes.
Example C# code:
public static class SudokuCache
{
private static Sudoku _game;
private static DateTime _timestamp;
public static Sudoku Game
{
get {
if (_timestamp.AddMinutes(20) < DateTime.Now) {
_game = new Sudoku();
_timestamp = new DateTime.Now;
}
return _game;
}
}
}
public class Sudoku { }
With this approach your service can handle client authentication/identity, keep scores etc and just provide a new game via a call to SudokuCache.Game.
As with anything WCF, make sure you use DataContract/DataMember attributes so you can correctly serialize your Sudoku object.
Upvotes: 3
Reputation: 3131
Answer#1: Generate Sudoko and cache it on server with time-stamp. Then all the request coming in next 20 mins of timestamp, return the cached result. Any request that doesn't satisfy criteria of time will trigger generation and caching of new Sudoku.
Answer#2: just put time check in you service and returning a flag indicating Service unavailable.
Nothing is WCF specific, as in comments, you will have to write the logic.
Upvotes: 0