views:

33

answers:

1

Hi,

I'm new to Silverlight and WCF services. I'm trying to write a client application that can manipulate an object server side.

My problem is that each time my Silverlight client makes a call to the service, it enters into the constructor systematically

public SilverLightEnabledWcfService()
        {
        }

In the below example, I simply want to increment or decrement a number depending on the activity client side.

How am I supposed to do this properly?

I also tried to create a regular ASP.net client page and I got the same result, ie the server doesn't remember the session. So I don't think the problem is in my client, but I'm still happy to post the code if it helps.

Thanks !!

using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using Count.Library;

namespace Count.WebApp
{
    [ServiceContract(Namespace = "")]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class SilverLightEnabledWcfService
    {
        public SilverLightEnabledWcfService()
        {
        }

        private Class1 _class1;

        [OperationContract]
        public int Add1()
        {
            if (_class1 == null)
                _class1 = new Class1(0);
            _class1.Add1();
            return Value;
         }

        [OperationContract]
        public int Remove1()
        {
            if (_class1 == null)
                _class1 = new Class1(0);
            _class1.Remove1();
            return Value;
         }

        public int Value
        {
            get
            {
                return _class1.Count;
            }
        } 
    }
}
+1  A: 

Sessions require the wsHttpBinding, but this is not supported by Silverlight. There are workarounds, though:

http://web-snippets.blogspot.com/2008_08_01_archive.html

http://forums.silverlight.net/forums/t/14130.aspx

Ozan
Thanks for the links. These make me wonder whether I have the correct approach. I might not need to use sessions, I'm not sure. Basically in a Silverlight client, I want to call a WCF service on the server that will keep the previously instantiated objects called by this specific client. I want to call from the client successive methods on the same object stored on the server. Do I need to use sessions to do this?Thanks!
bmanu
I would not store any state in the SilverLightEnabledWcfService class. Let it relay any service request to either an object in the same application domain or a second wcf service, and try to use the FormsAuthentication method to give that object or second service the user info it needs to fulfill that request.
Ozan