WCF + Silverlight + HttpContext.Current.Session为null

时间:2011-06-08 13:47:03

标签: silverlight wcf session basichttpbinding

我的问题......

我正在尝试从Silverlight和WCF basicHttpBinding ...

访问会话

我看到了一些可能的帖子(http://www.dotnetspider.com/Silverlight-Tutorial-317.aspx)

Mys cenario是:

Silvelright 4 FW 3.5

在web.config中我有

<system.serviceModel>
    <behaviors>
        <serviceBehaviors>
            <behavior name="ViewModelDemo.Web.Service1Behavior">
                <serviceMetadata httpGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <services>
        <service behaviorConfiguration="ViewModelDemo.Web.Service1Behavior" name="ViewModelDemo.Web.Service1">
            <endpoint address="" binding="basicHttpBinding" contract="ViewModelDemo.Web.Service1">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        </service>
    </services>
</system.serviceModel>

和我的服务:

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class Service1
{
    [OperationContract]
    publicvoid Test()
    {
        var session = System.Web.HttpContext.Current.Session;
    }
}

并且正在致电

                var client = new Service1Client();
                client.GetUserMacroFunctionsCompleted += new System.EventHandler<GetUserMacroFunctionsCompletedEventArgs>(client_GetUserMacroFunctionsCompleted);
                client.GetUserMacroFunctionsAsync();


void client_GetUserMacroFunctionsCompleted(object sender, GetUserMacroFunctionsCompletedEventArgs e)
    {
        var test =  ((Collection<Function>)e.Result);
    }

HttpContext.Current总是为空!

有什么建议吗?

3 个答案:

答案 0 :(得分:6)

是的HttpContext必须始终为null,因为您的服务配置不会设置ASP.NET兼容性,并且您的服务不需要ASP.NET兼容性。

将此添加到您的配置中:

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />

并更改AspNetCompatibilityRequirements,以便在没有以前配置的情况下无法托管您的服务:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

答案 1 :(得分:1)

此链接可能对您有帮助。

http://blogs.msdn.com/b/sajay/archive/2006/08/03/687361.aspx

在客户端绑定配置上设置aspNetCompatibilityEnabled="true"之前,

allowCookies="true"对我没有帮助。

答案 2 :(得分:0)

更新您的web.config文件以包含

<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />    
</system.serviceModel>

这应该可以,或者也可以将合同上的AspNetCompatibilityRequirementsMode属性更改为Required。