2011-08-21 26 views
2

我是wcf的新手,希望有人能幫上忙。我有一個wcf項目和一個web應用程序。 Wcf項目作爲應用程序駐留在Web項目的IIS中,使用同一個應用程序池。wcf(webhttpbinding)通過jquery調用 - asp.net會話沒有填充

我已經添加sessionmode.allowed到我的wcf接口,啓用aspnetcompatibilitymode。對於綁定,我設置了transport作爲安全性,並嘗試allowCookies爲false和true。

當我點擊該網站,並檢查會話中的鍵大約5,但是當它然後通過jquery調用我的服務,我檢查會話中的鍵沒有。似乎Cookie不會爲每個請求發送。

有沒有人遇到類似的行爲?

感謝

+0

這可能是在不同域中託管的WCF和ASP.Net應用程序的問題。你有沒有這樣做? – Chandermani

回答

0

我甚至不知道這是可能的,所以我只設置了我自己的測試項目。 會話狀態對我來說工作得很好。

如果有幫助,這裏是我寫的代碼工作:

using System.ServiceModel; 
using System.ServiceModel.Activation; 
using System.ServiceModel.Web; 
using System.Web; 

[ServiceContract(Namespace = "")] 
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
public class MyService 
{ 
    [OperationContract] 
    [WebGet] 
    public string DoWork() 
    { 
     var httpContext = HttpContext.Current; 
     var sessionState = httpContext.Session; 

     return (string)sessionState["Foo"]; // This returns "Bar" 
              // which I set in the Page_Load 
              // server method 
    } 
} 

一個在我的默認頁面的加載方法:

protected void Page_Load(object sender, EventArgs e) 
{ 

    System.Web.SessionState.HttpSessionState sessionState = this.Session; 
    sessionState["Foo"] = "Bar"; 
} 

在我的web.config文件:

<system.serviceModel> 
    <behaviors> 
     <endpointBehaviors> 
     <behavior name="MyServiceAspNetAjaxBehavior"> 
      <enableWebScript/> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment 
      aspNetCompatibilityEnabled="true" 
      multipleSiteBindingsEnabled="true"/> 
    <services> 
     <service name="MyService"> 
     <endpoint 
       address="" 
       behaviorConfiguration="MyServiceAspNetAjaxBehavior" 
       binding="webHttpBinding" 
       contract="MyService"/> 
     </service> 
    </services> 
    </system.serviceModel> 
+0

謝謝安德魯,我可以看到我和你之間唯一不同的是你使用.net 4和enableWebScript。我在哪裏使用.net 3.5和webhttp。我改變了我的腳本,但沒有運氣,這很奇怪。出於好奇,你在IIS中託管的wcf服務?我也在SSL下運行它buti不認爲這是問題。如果我自己主持wcf它的作品。 – John

+0

我在我的Visual Studio調試器中運行這一切,所以我沒有在IIS中測試它。這總是會引發大量意想不到的問題! –