WCF + Silverlight + HttpContext.Current.Session - null

my problem....

I am trying to access a session from Silverlight and WCF basicHttpBinding ...

I saw several posts where this is possible (http://www.dotnetspider.com/Silverlight-Tutorial-317.aspx)

Mys cenario:

Silvelright 4 FW 3.5

in web.config i have

<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>

and my service:

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

and he calls

                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 is always null!

Any suggestions?

+5
source share
3 answers

Yes, the HttpContext should always be null because your service configuration does not establish ASP.NET compatibility, and your service does not require ASP.NET compatibility.

Add this to your configuration:

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />

AspNetCompatibilityRequirements, :

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
+6

, , .

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

aspNetCompatibilityEnabled="true" , allowCookies="true" .

+1

Update web.config to include

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

This should work as well as change the AspNetCompatibilityRequirementsMode attribute in the Contract to Required.

0
source

All Articles