【问题标题】:wcf (webhttpbinding) calling via jquery - asp.net session not populatedwcf(webhttpbinding)通过 jquery 调用 - 未填充 asp.net 会话
【发布时间】:2011-08-21 02:31:15
【问题描述】:

我对 wcf 很陌生,希望有人能提供帮助。我有一个 wcf 项目和一个网络应用程序。 Wcf 项目在 Web 项目中作为 IIS 中的应用程序托管,两者都使用相同的应用程序池。

我已将 sessionmode.allowed 添加到我的 wcf 界面,启用了 aspnetcompatibilitymode。对于绑定,我将传输设置为安全性,并尝试将 allowcookies 设置为 false 和 true。

当我点击站点并检查会话中的密钥时,大约有 5 个,但是当它通过 jquery 调用我的服务并且我检查会话中的密钥时,没有。似乎没有为每个请求发送 cookie。

有没有人经历过类似的行为?

谢谢

【问题讨论】:

  • 这可能是托管在不同域中的 WCF 和 ASP.Net 应用程序的问题。你检查过吗?

标签: wcf


【解决方案1】:

我什至不知道这是可能的,所以我只是建立了自己的测试项目。 会话状态对我来说很好。

如果有帮助,这是我编写的有效代码:

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>

【讨论】:

  • 谢谢 Andrew,我唯一能看出我的和你的不同的是你使用的是 .net 4 和 enableWebScript。我在哪里使用 .net 3.5 和 webhttp。我将我的更改为 enablescript 但没有运气,这很奇怪。出于好奇,您的 wcf 服务是否托管在 IIS 中?我也在 SSL 下运行它,但我认为这不是问题。如果我自己托管 wcf,它就可以工作。
  • 我在我的 Visual Studio 调试器中运行这一切,所以我没有在 IIS 中测试它。这总是会引发大量意想不到的问题!
猜你喜欢
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 2011-02-27
  • 1970-01-01
  • 2019-01-03
  • 2018-07-09
  • 1970-01-01
相关资源
最近更新 更多