My problem is that WCF doesn't keep session among Silverlight calls. Every call is a different asp.net Session. I know it can work as I have an example which uses an auto-generated proxy for WCF in Silverlight, but I use Channel Factory. I have searched all over the net, but sadly most people seem to use the proxy generator.
My web.config
<bindings>
<basicHttpBinding>
<binding name="databaseServiceBasicHttp" allowCookies="true" />
</basicHttpBinding>
</bindings>
<services>
<service name="database开发者_JAVA百科Service"
behaviorConfiguration="Debug">
<endpoint address=""
binding="basicHttpBinding" bindingConfiguration="databaseServiceBasicHttp" contract="BlueGazelle.DatabaseServiceContracts.IDatabaseService" />
</service>
</services>
Silverlight binding config
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding >
<binding name="databaseService" enableHttpCookieContainer="true"/>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:53392/Services/DatabaseService.svc"
binding="basicHttpBinding" bindingConfiguration="databaseService" contract="BlueGazelle.DatabaseServiceContracts.IDatabaseService"
name="BlueGazelle.DatabaseService.Code.DatabaseService"/>
</client>
</system.serviceModel>
</configuration>
How I create channel
var service = new ChannelFactory<IDatabaseService>("BlueGazelle.DatabaseService.Code.DatabaseService").CreateChannel();
Is there something wrong with ChannelFactory? Should I enable something for it to carry cookies?
The basicHttpBinding binding does not provide any WCF session capabilities. But you can still get ASP.NET session handling to work with a little care. Check you have done the following...
1, Added the following attribute to your service interface...
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
2, Inside your web.config add the following...
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
3, Inside your web.config add the following...
<sessionState mode="InProc" cookieless="false" regenerateExpiredSessionId="true" timeout="20"/>
Silverlight has no problem working with ASP.NET sessions that use the above cookie method of passing the session information around. Getting it working with cookieless sessions would be tricky because it would need to pass back information as part of the URL.
For more information I have found this blog entry.
Thanks for your response Phil, but I've done the stuff you mention earlier. It is of course needed, but that wasn't the problem in my case.
I needed to add a cookie container to my generated proxy. I've found the answer on one webpage, but can't find it now.
Anyways here's the code if someone's curious.
var service = new ChannelFactory<IDatabaseService>("BlueGazelle.DatabaseService.Code.DatabaseService").CreateChannel();
var cookieManager = ((IChannel)service).GetProperty<IHttpCookieContainerManager>();
if (cookieManager.CookieContainer == null)
{
cookieManager.CookieContainer = new CookieContainer();
}
精彩评论