Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 894199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:20:42+00:00 2026-05-15T14:20:42+00:00

I am using Wcf in Sharp Architecture. I have configured my project following northwind

  • 0

I am using Wcf in Sharp Architecture. I have configured my project following northwind sample using WcfSessionStorage etc. I have a method in the wcf service that gets a list of business object using Repository<>.GetAll(). I am testing the service method using WcfTestClient. When the method is called the very first time, everything works fine. However on the subsequent call, I get the following exception on the Repository<>.GetAll() method

[System.ObjectDisposedException]
Session is closed!
Object name: 'ISession'

It seems like the NHibernate session gets disposed after each call. I have got around this problem by decorating my service with the following attribute

[ServiceBehavior( InstanceContextMode = InstanceContextMode.PerCall )]
public class WcfService : IWcfService
{
}

However this means, an instance of the service will be created on each call that in turn will create a new nhibernate session etc. In my scenario there is no need of creating a new service instance per call and I think its an expensive process and not the right solution. I would like to know what is the best practice in my scenario and how to get this thing work with creating a new service instace per call.

Thanks
Nabeel

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:20:42+00:00Added an answer on May 15, 2026 at 2:20 pm

    The easiest way is to create a new instance every time and it’s not an expensive process, because creating a new object in .NET is like 0.00000000000000001 second (I read that on Ayande’s blog or somewhere).

    I use Autofac DI in my projects and I usually make ISession as container scoped (one per request). And then every class that uses (directly or indirectly) ISession has to be container scoped or lower (factory scoped == every class usage get’s a new instance). If a class that uses ISession is higer scoped (session scoped == singleton) you’ll run into problems that you currently have.

    If your service is singleton service:
    At first run the service is created, this service uses ISession, which should be container scoped, and it is on the first run.
    The next request to service (service is now created) has still a reference to created ISession (which was closed on previous end request), so now it’s closed.

    I don’t recomend using the same ISession that you’ll open/close (it’s not recomended in the NHibernate documentation), just use container scoped (I do and I don’t have any performance issues), or you should create ISession manually in every method in your service like:

    using(ISession s = ISessionFactory.OpenSession())
         using(ITransaction t = .....)
    ....
    

    But that isn’t nice at all…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been reading about using Sharp Architecture to control nhiberate via Wcf. Is
I have an upload/download web service that I created with WCF. Im using c
I have a sharp architecture project and I am making use of ApplicationServices in
I am trying to access a WCF web service, that is using two way
I am using WCF service, which have two endpoint WsHttpBinding and NetTcpBinding and the
I configured traditional WCF Services using SOAP end points. In my client project I
I have a web service in WCF that consume some external web services, so
I have a WCF Service which exposes an endpoint using the webHttpBinding and is
We are using WCF service on the client side we are planning to explicitly
I am using WCF and REST, and I have complex types, which are working

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.