Solved

C# Classes in a Client server environment

Posted on 2014-11-02
4
133 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
Comment Utility
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 32

Assisted Solution

by:Stefan Hoffmann
Stefan Hoffmann earned 167 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
C# Update Status Bar.... 1 22
Hovering effect 9 28
Image(2) 3 21
String manipulation 15 49
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now