Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF + Silverlight + HttpContext.Current.Session is null

my problem....

i am tryingo to access session from Silverlight and WCF basicHttpBinding...

i saw some posts where it's possible (http://www.dotnetspider.com/Silverlight-Tutorial-317.aspx)

Mys cenario is:

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 it''s call

                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?

like image 699
user756037 Avatar asked Jun 08 '11 13:06

user756037


1 Answers

Yes HttpContext must be always null because your service configuration doesn't setup ASP.NET compatibility and your service doesn't require ASP.NET compatibility.

Add this to your configuration:

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />

And change AspNetCompatibilityRequirements so that your service cannot be hosted without former configuration:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
like image 173
Ladislav Mrnka Avatar answered Oct 05 '22 23:10

Ladislav Mrnka