Solved

C# Classes in a Client server environment

Posted on 2014-11-02
4
138 Views
Last Modified: 2014-11-15
Hello,
I am attempting to develop a simple client/server application in WCF as a learning exercise. I have a couple of basic questions about the behaviour of classes in the server environment.

1. If there are numerous people on a client/server application and each client creates their own instance of a class (say MyUser )  then as they dip in and out of the server application ,  how does the server  application know which object belongs to which client ?

2. If , in my server application I want to create  global variables for each client rather than for all clients on the server then how would I go about this. My understanding is that a static class would make the variables available to everyone.

thanks
Pat
0
Comment
Question by:pclarke7
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 167 total points
ID: 40418903
You need to understand that when the user creates an instance with New, the object itself is not on your server, it's on the user's computer. All your server does is provide methods.

By default, when an application calls your service, through its own object, the server creates an object that receives the call, calls the requested method, returns the result and drop the instance. There is no persistence on the server side.

This then precludes you from defining global variables.

This is the default, what Microsoft calls a per-call service. If you want to maintain some information on the server side, you need to create a per-session service, which has to be configured in a different way. Be aware then that you can end up with an overloaded server, and this creates a service that won't scale easily. If the number of users goes up, the number of persistent instances also goes up and you end up with the same problems that client-server application have, these problem that services were designed to solve.

You can get all the details in an article of the MSDN Magazine at http://msdn.microsoft.com/en-us/magazine/cc163590.aspx
0
 
LVL 34

Assisted Solution

by:ste5an
ste5an earned 167 total points
ID: 40418970
And for 2):

You shouldn't do that. Global variables are evil. The represent state, which makes it harder than necessary to write testable code.

When you don't find a better idea than "global variables" then use a context object which stores those variables. And inject the object to the "consuming" objects.
0
 
LVL 22

Assisted Solution

by:ambience
ambience earned 166 total points
ID: 40419221
1. If there are numerous people on a client/server application and each client creates their own instance of a class (say MyUser )  then as they dip in and out of the server application ,  how does the server  application know which object belongs to which client ?

If you create an instance of what is truly "MyUser" then you get just that - a local instance of MyUser. Normally, in WCF, on the client-side, you create an instance of a Proxy to MyUser - and that is a different "type" as such. It may provide an identical interface to the actual MyUser but its a different class that proxies method calls to remote server. Note that this is true if MyUser is a service (ServiceContract).

2. If , in my server application I want to create  global variables for each client rather than for all clients on the server then how would I go about this. My understanding is that a static class would make the variables available to everyone.

global variables for each client are commonly known as session variables and as already explained, WCF allows you to have session based services. If you enable ASP Compatibility Mode, you can even use ASP.NET's session object in your services.
0
 

Author Comment

by:pclarke7
ID: 40422537
Thank you all for your comments. I have certainly learned quite a lot from them. What I am designing is a process where a transaction , which resides on the server, is invoked by the user. The transaction has many record types (Tag records, calculation records, Logic records, Decision records , input records  & output records etc..) When the next transaction record to be processed is  an "Input" type then processing will return to the client for an input. The screen displayed on the client will contain the Outputs which were processed by the transaction prior to the Input type.

A simple example might be where the user invokes a transaction to change printers. The transaction will read through the list of printers available and output the name and location to MyDisplayclass. When the transaction hits an Input record to request the input of the selected printer then control will return to the client which will display the list of printers (from MyDisplayClass object) and user will be prompted to select from the list.  The input value is then returned to the server which will resume the transaction at the line after the input statement.  

I assume that this will require me to configure a per session service ?    

regards
Pat
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question