Reputation: 3
I am trying to make a Minesweeper game using a WCF service. It appears that I'm able to connect to the service from client, call methods remotely and get what they return.
The problem is, on the Service side, I use an array. Everytime a client connect to the service, the array is supposed to update itself, and it does, but everytime a client connect, the array gets reseted to 0, as if there was multiple instance of the service...
Here is the code you might want to look at:
On the service interface, IService1.cs
public interface IDemineur
{
[OperationContract]
string CreationJoueur(string Name);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
// TODO: Add your service operations here
}
The Service1.svc
public class Service1 : IDemineur
{
Dictionary<int, Grille> Gamepad = new Dictionary<int, Grille>();
int[,] tableGrilles = new int[20, 3];
public string CreationJoueur(string Name)
{
string joueur = "";
for (int i = 0; i < 20; i++)
{
if (tableGrilles[i, 0] != 1)
{
tableGrilles[i, 0] = 1;
tableGrilles[i, 2] = i;
joueur = "1/" + tableGrilles[i, 2];
Gamepad.Add(i, new Grille());
break;
}
if (tableGrilles[i, 1] != 2)
{
tableGrilles[i, 1] = 2;
joueur = "2/" + tableGrilles[i, 2];
break;
}
}
return joueur;
}
Here, the point was to update the array tableGrilles. When I run the debug for the host, I can clearly see that it does update it, but whenever I connect with another client, it goes into debug mode again at my breakpoint and the array is back to 0.
Client side:
public partial class DemineurGrille : Window
{
ServiceReference1.DemineurClient leDemineur;
int noJoueur;
int noGrille;
public DemineurGrille(string name)
{
InitializeComponent();
leDemineur = new ServiceReference1.DemineurClient("*");
string noJoueurGrille = leDemineur.CreationJoueur(name);
string[] leSplit = noJoueurGrille.Split('/');
noJoueur = Convert.ToInt32(leSplit[0]);
noGrille = Convert.ToInt32(leSplit[1]);
}
Please help, that would be much appreciated...
Upvotes: 0
Views: 52
Reputation: 62
It's not a problem. Your breakpoint might be hit in another session initiated by another request, and in another thread.
Upvotes: 0
Reputation: 134
That's because the InstanceContextMode is set to PerSession by default. In other words, you are creating a new service every time you connect a client, so the objects will be empty.
The quick solution would be to change the InstanceContextMode to Single, which means all requests will share an instance until the process (website) is reset. However, this means that every client is updating the same array, which may not be what you want.
The more robust solution is to use a data store, such as a database, which is going to store a separate array for each user, thus ensuring that User A updates Array A, User B updates Array B, etc.
Upvotes: 1