2012-06-19 62 views
3

我正嘗試訪問WCF服務中的asp.net會話。我正在從我的asp.net應用程序調用jQuery AJAX到wcf服務。我已經通過許多SO問題和文章,並嘗試了他們所說的一切,但仍然無法訪問wcf服務中的客戶端會話。當我寫DataSet ds = HttpContext.Current.Session["data"] as DataSet;ds始終爲空。無法在wcf服務中訪問asp.net會話

我在這裏失蹤了什麼?

這是我的WCF服務看起來像: //接口

[ServiceContract(SessionMode = SessionMode.Allowed)] 
public interface IMyService 
{ 
    [OperationContract(IsInitiating=true)] 
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] 
    string GetData(); 

    [OperationContract] 
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] 
    bool SaveData(string data); 
} 

//服務

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] 
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] 
public class MyService : IMyService 
{ 
    public string GetData() 
    { 
     return "something"; 

    } 
    public bool SaveData(string data) 
    { 
     DataSet ds = HttpContext.Current.Session["data"] as DataSet; 
     return true; 
    } 

} 

我創造的Global.asaxsession_start的數據集,並把它在會議這樣只要用戶會話有效,我就可以在整個應用程序中使用它。

protected void Session_Start(object sender, EventArgs e) 
    { 
     DataSet ds = new DataSet(); 

     //Table to hold Product Selection 
     DataTable dt = new DataTable("T1"); 
     dtSSNCertification.Columns.Add("Col1"); 
     ds.Tables.Add(dt); 

     Session.Add("data", ds); 
    } 

這是我的WCF項目web.config bindings的樣子:

<service name="MyService"> 
    <endpoint address="" behaviorConfiguration="JSONPBehaviorConfiguration" 
     binding="customBinding" bindingConfiguration="jsonpBinding" 
     contract="IMyService"> 
    </endpoint> 
    </service> 

<customBinding> 
    <binding name="jsonpBinding" > 
     <jsonpMessageEncoding /> 
     <httpTransport manualAddressing="true"/> 
    </binding> 
    </customBinding> 


    <system.serviceModel> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> 
    </system.serviceModel> 

回答

5

要使用ASP.NET功能,你需要啓用ASP.NET兼容性與WCF。在你的web.config中設置爲:

<system.serviceModel> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"> 
+0

是的。在那。 – Asdfg

+1

它不工作,因爲我正在對我的服務進行AJAX調用? – Asdfg

+0

它沒有工作的人! –