Silverlight 4 HtmlPage.Document.Cookies is always empty
I'm very much new to silverlight, so please assume I've done something very daft....
I am trying to make a call from a silverlight app to a WCF service and check a value in the session. The value will have been put there by an aspx page. It's a little convoluted, but that's where we are.
My service looks like this:
[ServiceContract]
public interface IExportStatus
{
[OperationContract]
ExportState RequestExportComplete();
}
public enum ExportState
{
Running,
Complete
}
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class ExportStatus : IExportStatus
{
ExportState IExportStatus.RequestExportComplete()
{
// check value of session variable here.
}
}
The site that hosts the silverlight app also hosts the wcf service. Its web config looks like this:
<configuration>
<system.serviceModel>
<services>
<service name="SUV_MVVM.Web.Services.ExportStatus" behaviorConfiguration="MyBehavior">
<endpoint binding="basicHttpBinding"
bindingConfiguration="MyHttpBinding"
contract="SUV_MVVM.Web.Services.IExportStatus"
address="" />
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="MyHttpBinding" />
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="MyBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModul开发者_运维知识库esForAllRequests="true"/>
</system.webServer>
</configuration>
I added the service reference to my silverlight app using the VS tooling acepting the defaults (apart for the namespace)
Initially I was just trying to call the service like this:
var proxy = new ExportStatusClient();
proxy.RequestExportCompleteCompleted += (s, e) =>
{
//Handle result
};
proxy.RequestExportCompleteAsync();
But the session in the service was always empty (not null, just empty), so I tried manually setting the session Id into the request like this:
var proxy = new ExportStatusClient();
using (new OperationContextScope(proxy.InnerChannel))
{
var request = new HttpRequestMessageProperty();
//this might chnage if we alter the cookie name in the web config.
request.Headers["ASP.NET_SessionId"] = GetCookie("ASP.NET_SessionId");
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = request;
proxy.RequestExportCompleteCompleted += (s, e) =>
{
//Handle result
};
proxy.RequestExportCompleteAsync();
}
private string GetCookie(string key)
{
var cookies = HtmlPage.Document.Cookies.Split(';');
return (from cookie in cookies
select cookie.Split('=')
into keyValue
where keyValue.Length == 2 && keyValue[0] == key
select keyValue[1]).FirstOrDefault();
}
But what I'm finding is that the HtmlPage.Document.Cookies
property is always empty.
So Am I just missing something really basic, or are there any other things that I need to change or test?
I just did a test from a Silverlight 4 application.
System.Windows.Browser.HtmlPage.Document.Cookies = "KeyName1=KeyValue1;expires=" + DateTime.UtcNow.AddSeconds(10).ToString("R"); System.Windows.Browser.HtmlPage.Document.Cookies = "KeyName2=KeyValue2;expires=" + DateTime.UtcNow.AddSeconds(60).ToString("R");
Each cookie expired as expected.
The value of System.Windows.Browser.HtmlPage.Document.Cookies...
Immediately after setting the cookies: "KeyName1=KeyValue1; KeyName2=KeyValue2"
30 seconds later: "KeyName2=KeyValue2"
60+ seconds later: ""
精彩评论