WCF + Silverlight + HttpContext.Current.Session为null

use*_*037 5 silverlight session wcf 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>
Run Code Online (Sandbox Code Playgroud)

和我的服务:

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

这是电话

                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);
    }
Run Code Online (Sandbox Code Playgroud)

HttpContext.Current总是为null!

有什么建议?

Lad*_*nka 6

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

将其添加到您的配置中:

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
Run Code Online (Sandbox Code Playgroud)

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

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
Run Code Online (Sandbox Code Playgroud)