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