简体   繁体   中英

Enable session with wsHttpBinding in wcf

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:在此处输入图像描述

Why is UserFullName always null?

Change InstanceContextMode. PerCall to PerSession .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM