Solved

C# Classes in a Client server environment

Posted on 2014-11-02
4
136 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 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 33

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

810 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