Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# Classes in a Client server environment

Posted on 2014-11-02
4
Medium Priority
?
145 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
4 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 668 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 36

Assisted Solution

by:ste5an
ste5an earned 668 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 664 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

972 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