2

I've written this code:

interface:

public interface IService1
{
    [OperationContract]
    string Welcome(string fullName);

    [OperationContract]
    string Goodbye();

    [OperationContract]
    string GetSessionID();

    [OperationContract]
    CompositeType GetDataUsingDataContract(CompositeType composite);
}

service:

[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
public class Service1 : IService1
{
    private string UserFullName { get; set; }

    public string GetSessionID()
    {
        var sessionId = OperationContext.Current.SessionId;
        return sessionId.ToString();
    }

    public string Welcome(string fullName) 
    { 
        UserFullName = fullName ?? "Guest"; return string.Format("Welcome back, {0}!", UserFullName); 
    }    

    public string Goodbye() 
    {
        return string.Format("Come back soon, {0}!", UserFullName ?? "Guest"); 
    }

    public CompositeType GetDataUsingDataContract(CompositeType composite)
    {
        if (composite == null)
        {
            throw new ArgumentNullException("composite");
        }
        if (composite.BoolValue)
        {
            composite.StringValue += "Suffix";
        }
        return composite;
    }
}

webconfig: enter image description here

Why is UserFullName always null?

wonea
  • 4,783
  • 17
  • 86
  • 139
Arian
  • 12,793
  • 66
  • 176
  • 300

1 Answers1

1

Change InstanceContextMode.PerCall to PerSession.

In your example an instance of the service is created every call.

Maxim
  • 7,268
  • 1
  • 32
  • 44
  • OK, to have a session you either need to use **message security** or turn on **reliable session**. Another solution is to create a custom binding with this - – Maxim May 27 '11 at 23:23
  • reliable session is turn on but does not work.what do you mean about custom binding? – Arian May 28 '11 at 03:53
  • Did you try to turn on Message Security? For custom binding Look @ this answer - http://stackoverflow.com/questions/4396961/why-wcf-instancecontextmode-persession-is-not-working-over-https – Maxim May 28 '11 at 07:17
  • OK is it true that wsHttpBinding does not support sessions? – Arian May 29 '11 at 18:51
  • It should. Did you try message security? – Maxim May 29 '11 at 21:14