Silverlight启用Web服务中的会话

时间:2011-04-07 06:36:17

标签: silverlight silverlight-4.0 wcf

我创建了一个支持Silverlight的WCF服务,它看起来像: 我正在使用basicHttpBinding

[ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class SLWebService
    {


        [OperationContract]
        public string Login(string username, string password)
        {
            //Validate Username and Password against the Database.
           // myctxt.Session["Uname"] = username;
            HttpContext.Current.Session["username"] = username ;
            return username;
        }

        [OperationContract]
        public string ReturnUser()
        {
            return (string)HttpContext.Current.Session["username"];
        }

    }
}

我面临的问题是我每次都会获得用户名值为null。 我在Silverlight客户端中使用此WCF服务,如下所示:

Page.xaml.cs:

private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {
            SessionService.SLWebServiceClient client = new SessionService.SLWebServiceClient();
            client.LoginAsync(txtuser.Text, txtpassword.Password);    
            NavigateRequest(1); 

        }

Welcome.xaml.cs

void Welcome_Loaded(object sender, RoutedEventArgs e)
        {
            SessionService.SLWebServiceClient client = new SessionService.SLWebServiceClient();
            client.ReturnUserCompleted += new EventHandler<SessionService.ReturnUserCompletedEventArgs>(client_ReturnUserCompleted);
            client.ReturnUserAsync(); 
        }

        void client_ReturnUserCompleted(object sender, SessionService.ReturnUserCompletedEventArgs e)
        {
            txtusername.Text = e.Result; 
        }

我正在创建一个登录页面(Page.xaml)并输入Userid和密码,然后重定向到欢迎页面,欢迎当前用户,但我的服务没有返回会话为空的用户名。

感谢。

1 个答案:

答案 0 :(得分:2)

在“公共类SLWebService”

之上添加[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]