簡體   English   中英

在 wcf 中使用 wsHttpBinding 啟用 session

[英]Enable session with wsHttpBinding in wcf

我寫了這段代碼:

界面:

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

    [OperationContract]
    string Goodbye();

    [OperationContract]
    string GetSessionID();

    [OperationContract]
    CompositeType GetDataUsingDataContract(CompositeType composite);
}

服務:

[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;
    }
}

網絡配置:在此處輸入圖像描述

為什么 UserFullName 總是 null?

更改InstanceContextMode。 PerCallPerSession

在您的示例中,每次調用都會創建一個服務實例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM