Pranesh Nair
Pranesh Nair

Reputation: 313

Session Creation in WCF

I am new to WCF,my task is to create,maintain sessions in WCF

I have a requirement in my project,what it says is I need to have a service(WCF) which has to be session enabled.More than one client will contact the above said service and the service has to deliver the required information that client wants.

For example: The service will hold a DOM object,here DOM means a database object which will have say Employee information.Each client will ask for different information from the DOM object,and our service has to deliver the information.Our servvice should not goto Database each time when the client calls,so for this we need to implement session management in service(WCF).

It will be of great help if someone provide some ideas,suggestions,or sample code for implementing my task...

Thanks...

Upvotes: 0

Views: 454

Answers (1)

Greg Sansom
Greg Sansom

Reputation: 20870

First I'll point out that it is usually a very bad idea to use sessions with WCF. Having too many sessions open will consume lots of resources (eg memory and database connections). You mentioned that you are also storing database objects in the session - this is also likely to end up hurting you as most databases only allow a limited number of sessions.

All that said, if you really need to use sessions, there is some info for configuring it on MSDN.

You can configure your binding to use sessions as follows:

<wsHttpBinding>
    <binding name="wsHttpBinding">
      <reliableSession enabled="true" />
    </binding>
</wsHttpBinding>

You can then mark your ServiceContract with SessionMode=SessionMode.Required:

    [ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples", 
SessionMode=SessionMode.Required)]
    public interface IMyService
    {
        ...
    }

Upvotes: 1

Related Questions