在WCF中不能使用HttpSession,即便Host是IIS也不能够,这就形成在WEB应用中集成WCF不太方便,其实能够经过配置搞定,关键在于三点:Host、契约类、Client端。cookie
Host上要求Web.config中有定义:session
- <system.serviceModel>
- <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
- </system.serviceModel>
契约实现类上要有Attribute指明容许使用session,要设在实现类上而不是契约接口上:ide
- [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
- publicic class Hello : IHello
- {
- //......
- }
Client端配置文件中定义binding时要容许使用cookie,设置allowCookies=true:ui
- <system.serviceModel>
- <binding name="WSHttpBinding_IHello" allowCookies="true" />
- </system.serviceModel>
这样就能够放心使用 HttpContext.Current.Session 了。spa