Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 147
  • Last Modified:

C# Classes in a Client server environment

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
pclarke7
Asked:
pclarke7
3 Solutions
 
Jacques Bourgeois (James Burger)PresidentCommented:
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
 
ste5anSenior DeveloperCommented:
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
 
ambienceCommented:
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
 
pclarke7Author Commented:
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now