我需要使用 wsHttpBinding 绑定,因为我的服务需要访问HttpContext.Current.Session. 我从我的研究中了解到,这对于 webHttpBinding 是不可能的。然而,我所有的 ajax 都是使用 JSON 编写的,如果我不必重写所有它,我会非常喜欢它。
在我需要使用会话之前,我的服务与 webHttpBinding 完美配合。
或者,有没有办法让 webHttpBinding 访问会话?
编辑:
  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    <behaviors>
      <endpointBehaviors>
        <behavior name="LiteBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="LiteBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="LiteBehavior" name="Lite.CMS.Services.LiteService">
        <endpoint behaviorConfiguration="LiteBehavior" address="" binding="webHttpBinding" contract="Lite.CMS.Services.Interfaces.ILiteService" />
      </service>
    </services>
  </system.serviceModel>
还有我的合同:
[ServiceContract (SessionMode=SessionMode.Allowed)]
public interface ILiteService
{
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    [OperationContract]
    void Item_Save(string Name);
}
和实施:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class LiteService : ILiteService
{
    public void Item_Save(string Name)
    {
        // I can't get access to HttpContext.Current.Session variables here.
    }
}