none
Session error in WCF Service when try to host the application in IIS RRS feed

  • Question

  • I'm using wcf application where I have used session for storing the session data. My code look like,

     

    Below is my svc file,

    ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
     
     
    public class WcfOffice : IWcfOffice
    {
        SubClass.ClsSession objsession = new SubClass.ClsSession();
        SubClass.Clslogin objlogin = new SubClass.Clslogin();
     
        public void puttosession(int loginid)
        {
            objsession.assign(loginid);
        }
        public int getsession(string UName)
        {
            int empid= objlogin.getempid(UName);
            return empid;          
        }
      }
    }

    }

    And my Interface look like,

    [ServiceContract]
    public interface IWcfOffice
    {      
        [OperationContract]
        void puttosession(int loginid);
     
        [OperationContract]
        int getsession(string UName);
    }

    Im using below code to write and read from session,

    public class ClsSession
    {
        public void assign(int loginid)
        {
            HttpContext.Current.Session["loginid"] =Convert.ToString(loginid);
        }
        public static int getid()
        {          
            int loginId = Convert.ToInt32((string)HttpContext.Current.Session["loginid"]);
            return loginId;
        }
    }

    Im publishing the web service and creating in my client side the service reference to the published web. Problem is that the session is not maintaining as "persession" after publishing to IIS.  The old request is getting override with the new value if new request is been made. When I publish this application in IIS, the loginId is overrrding with the recent request for the logins.

    Regards,

    Dheeraj Sharma
    Tuesday, June 3, 2014 10:40 AM

All replies

  • Hi,

    Which binding are you using?

    In WCF Service not all bindings support sessions. For example the basicHttpBinding does not support this due to the connectionless nature of the HTTP protocol. You may take a look at the following blog post:

    The BasicHttpBinding can never have a transport-level session due to the connectionless nature of the HTTP protocol. The WSHttpBinding without security and without reliable messaging will also not maintain a transport-level session. In both of these cases, even though the service is configured with  InstanceContextMode.PerSession and the contract with SessionMode.Allowed, the service will behave as a per-call service,  and the calls to Dispose() are asynchronous; that is, the client is not blocked after the call while WCF disposes of the instance.

    However, if you use the WSHttpBinding with security (its default configuration) or with reliable messaging, or the NetTcpBinding, or the NetNamedPipeBinding, then the service will behave as a per-session service.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, June 4, 2014 10:03 AM
    Moderator
  • Im using WSHttpBinding  and my server side web config looks like,

    <system.serviceModel>
         <bindings>
          <wsHttpBinding>         
            <binding name="PictureBinding" closeTimeout="00:01:00"              openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                  allowCookies="true" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                  maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                  messageEncoding="Text" textEncoding="utf-8"
                  useDefaultWebProxy="true">
              
              <readerQuotas maxDepth="32" maxStringContentLength="8192"
                 maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
              <security mode="None">
                <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
                <message clientCredentialType="UserName" algorithmSuite="Default" />
              </security>
            </binding>
          </wsHttpBinding>
    
        </bindings>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
            multipleSiteBindingsEnabled="true" />
        <behaviors>
          <serviceBehaviors>
            <behavior name="">
              <serviceMetadata httpGetEnabled="true" />
              <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
              <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
          </serviceBehaviors>
          
          
        </behaviors>
        <services>
          <service name="Office.Web.wcfOffice">
            <endpoint address="" binding="wsHttpBinding" bindingConfiguration="PictureBinding" contract="Office.Web.IwcfOffice">
              <identity>
                <dns value="localhost"/>
              </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
          </service>
        </services>
      </system.serviceModel>
      

    And my client side,

    <system.serviceModel>
            <bindings>
                <wsHttpBinding>
                   
                    <binding name="LEndpoint1" closeTimeout="00:01:00" openTimeout="00:01:00"
                        receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false"
                        transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                        maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                        messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                        allowCookies="true">
                        <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                            maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                        <security mode="None">
                            <transport clientCredentialType="Windows" proxyCredentialType="None"
                                realm="" />
                            <message clientCredentialType="Windows" negotiateServiceCredential="true" />
                        </security>
                    </binding>
                </wsHttpBinding>
            </bindings>
            <client>
    <endpoint address="http://IPAddress/Office/wcfOffice.svc"
                    binding="wsHttpBinding" bindingConfiguration="LEndpoint1"
                    contract="ServiceReference.IwcfOffice" name="LEndpoint1">
                    <identity>
                        <dns value="localhost" />
                    </identity>
                </endpoint>
                
            </client>
        </system.serviceModel>
    If I make allowcookies = true then my session is working. And if I host to IIS the session values are still overriding


    Dheeraj Sharma


    Monday, July 14, 2014 10:29 AM