Overload119
Overload119

Reputation: 5396

WCF: Session Data, Server Objects, and Frustrations

I'm new to WCF and have been dib-dabbing in different sections but still haven't found what I ultimately want. I want to create a server: I have my server object finished along with my data contracts, in the format:

public class SmartShopService : SmartShopInterface
{
        private Shopper sh;
        private List<ShoppingTripEvaluation> bestTrips;

        public SmartShopService() {
            sh = new Shopper();            
            bestTrips = new List<ShoppingTripEvaluation>();
        }

        // (Various methods go here)
}

The client is an ASP.NET page that looks like this:

public partial class Garfield : System.Web.UI.Page {

    static GarfieldService.SmartShopInterfaceClient client = new GarfieldService.SmartShopInterfaceClient();

    protected void Page_Load(object sender, EventArgs e) {

    }

    [WebMethod]
    public static bool SomeMethod() {
    }

    [WebMethod]
    public static bool SomeMethod2() {
    }
}

Now, in this page there are numerous forms. For sake of example, let's say the client is supposed to find the fastest route between A and B. It stores all the possible ways in a List variable on the server. The calculations are done in one click of a button on the client. Then, another button can display the information (lets assume it displays it chunks at a time - the user can opt to see top 25, top 50, top 100 results etc).

The problem I'm having lies here: Assume the scenario User A optimizes result -> Server holds results in variable server-side. User B comes along and optimizes result -> Server now holds result of User B. User A views his data -- but he's actually viewing user B's data!!!

What I ultimately want is for each person to log in to the page have a unique service object specificed to them until the point where they leave the page.

Thanks.

Upvotes: 3

Views: 456

Answers (1)

Sixto Saez
Sixto Saez

Reputation: 12680

The problem is the static client variable. All web app sessions will share the same client instance. Even if the web app has many sessions going, it only has one session to the WCF service.

You should just make it private variable to the page. On the WCF service side, make sure the service implementation class is set to the Per Call mode so each page instance will receive its own service instance.

Upvotes: 3

Related Questions