WCF中使用HttpSession

在WCF中不能使用HttpSession,即便Host是IIS也不能夠,這就形成在WEB應用中集成WCF不太方便,其實能夠經過配置搞定,關鍵在於三點:Host、契約類、Client端。cookie

Host上要求Web.config中有定義:session

  
  
           
  
  
  1. <system.serviceModel> 
  2.     <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> 
  3. </system.serviceModel> 

契約實現類上要有Attribute指明容許使用session,要設在實現類上而不是契約接口上:ide

  
  
           
  
  
  1. [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
  2. publicic class Hello : IHello 
  3.     //...... 

Client端配置文件中定義binding時要容許使用cookie,設置allowCookies=true:ui

  
  
           
  
  
  1. <system.serviceModel> 
  2.     <binding name="WSHttpBinding_IHello" allowCookies="true" /> 
  3. </system.serviceModel> 

這樣就能夠放心使用 HttpContext.Current.Session 了。spa

相關文章
相關標籤/搜索