views:

35

answers:

1

I am having trouble in configuring WCF service to run in session mode. As a test I wrote this simple service :

[ServiceContract]
public interface IService1
{
    [OperationContract]
    string AddData(int value);
}

[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
internal class Service1 : IService1,IDisposable
{
    private int acc;

    public Service1()
    {
        acc = 0;
    }

    public string AddData(int value)
    {
        acc += value;
        return string.Format("Accumulator value: {0}", acc);
    }

    #region IDisposable Members

    public void Dispose()
    {           
    }

    #endregion
}

I am using Net.TCP binding with default configuration with reliable session flag enabled. As far as I understand , such service should run with no problems in session mode. But , the service runs as in per call mode - each time I call AddData , constructor gets called before executing AddData and Dispose() is called after the call. Any ideas why this might be happening? Perhaps I am missing something?

note : I do not know if it is related , but I am using VS2008 to run this.

Update: I've noticed here that wcftestclient does not maintain session with clients - maybe it was my problem. Indeed that was the problem. Connecting to the service from simple console client confirmed that the service works as it should.

A: 

Try requiring a SessionMode when defining the ServiceContract:

[ServiceContract(SessionMode = SessionMode.Required)]
public interface IService1
{
  [OperationContract]
  string AddData(int value);
}
Ozan
tried that. the service still runs as if in PerCall mode.
Michael
@Michael: just to be sure, you do use only one proxy and do not close it between calls, yes?
Ozan
@Michael: is the "Start a new proxy" checkbox unchecked? (edit: that functionality seems to be missing from you version of wcftestclient)
Ozan
wcftestclient in my vs2008 doesn't have "Start a new proxy" checkbox. (looks exactly like in this page http://msdn.microsoft.com/en-us/magazine/cc163289.aspx)I have just checked and wcftestclient indeed starts new proxy each time. Wrote simple client in console project and the service functions as it should - with session. HUGE Thanks for pointing me into right direction. Marked your answer as accepted because your comments helped me greatly.
Michael