title: NHibernate in Web API ASP.NET: No session bound to the current context
body: <p>I'm new to NHibernate and trying to use it in ASP.NET WEB API. Firstly I used it successfully with one table named "Category" which the controller class is as follow:</p>
  
  <pre><code>using System.Collections.Generic;
  using System.Linq;
  using System.Net;
  using System.Net.Http;
  using System.Web.Http;
  using TestMVCProject.Web.Api.HttpFetchers;
  using TestMVCProject.Web.Api.Models;
  using TestMVCProject.Web.Api.TypeMappers;
  using TestMVCProject.Web.Common;
  //using TestMVCProject.Web.Common.Security;
  using NHibernate;
  
  namespace TestMVCProject.Web.Api.Controllers
  {
      [LoggingNHibernateSession]
      public class CategoryController : ApiController
      {
          private readonly ISession _session;
          private readonly ICategoryMapper _categoryMapper;
          private readonly IHttpCategoryFetcher _categoryFetcher;
  
          public CategoryController(
              ISession session,
              ICategoryMapper categoryMapper,
              IHttpCategoryFetcher categoryFetcher)
          {
              _session = session;
              _categoryMapper = categoryMapper;
              _categoryFetcher = categoryFetcher;
          }
  
          public IEnumerable&lt;Category&gt; Get()
          {
              return _session
                  .QueryOver&lt;Data.Model.Category&gt;()
                  .List()
                  .Select(_categoryMapper.CreateCategory)
                  .ToList();
          }
  
          public Category Get(long id)
          {
              var category = _categoryFetcher.GetCategory(id);
              return _categoryMapper.CreateCategory(category);
          }
  
  
          public HttpResponseMessage Post(HttpRequestMessage request, Category category)
          {
              var modelCategory = new Data.Model.Category
              {
                  Description = category.Description,
                  CategoryName = category.CategoryName
              };
  
              _session.Save(modelCategory);
  
              var newCategory = _categoryMapper.CreateCategory(modelCategory);
  
              //var href = newCategory.Links.First(x =&gt; x.Rel == "self").Href;
  
              var response = request.CreateResponse(HttpStatusCode.Created, newCategory);
              //response.Headers.Add("Location", href);
  
              return response;
          }
  
  
          public HttpResponseMessage Delete()
          {
              var categories = _session.QueryOver&lt;Data.Model.Category&gt;().List();
              foreach (var category in categories)
              {
                  _session.Delete(category);
              }
  
              return new HttpResponseMessage(HttpStatusCode.OK);
          }
  
  
          public HttpResponseMessage Delete(long id)
          {
              var category = _session.Get&lt;Data.Model.Category&gt;(id);
              if (category != null)
              {
                  _session.Delete(category);
              }
  
              return new HttpResponseMessage(HttpStatusCode.OK);
          }
  
  
          public Category Put(long id, Category category)
          {
              var modelCateogry = _categoryFetcher.GetCategory(id);
  
              modelCateogry.CategoryName = category.CategoryName;
              modelCateogry.Description = category.Description;
  
              _session.SaveOrUpdate(modelCateogry);
  
              return _categoryMapper.CreateCategory(modelCateogry);
          }
      }
  }
  </code></pre>
  
  <p>But when I add The "Product" table which has a foreign key of the Category table, the product controller doesn't work and throws below exception:</p>
  
  <blockquote>
    <p>No session bound to the current context</p>
  </blockquote>
  
  <p><code>ProductController</code> class is as follow:</p>
  
  <pre><code>using System.Collections.Generic;
  using System.Linq;
  using System.Net;
  using System.Net.Http;
  using System.Web.Http;
  using TestMVCProject.Web.Api.HttpFetchers;
  using TestMVCProject.Web.Api.Models;
  using TestMVCProject.Web.Api.TypeMappers;
  using TestMVCProject.Web.Common;
  //using TestMVCProject.Web.Common.Security;
  using NHibernate;
  
  namespace TestMVCProject.Web.Api.Controllers
  {
      [LoggingNHibernateSession]
      public class ProductController : ApiController
      {
          private readonly ISession _session;
          private readonly IProductMapper _productMapper;
          private readonly IHttpProductFetcher _productFetcher;
  
  
          public ProductController(
              ISession session,
              IProductMapper productMapper,
              IHttpProductFetcher productFetcher)
          {
              _session = session;
              _productMapper = productMapper;
              _productFetcher = productFetcher;
          }
  
          public IEnumerable&lt;Product&gt; Get()
          {
              return _session
                  .QueryOver&lt;Data.Model.Product&gt;()
                  .List()
                  .Select(_productMapper.CreateProduct)
                  .ToList();
          }
  
          public Product Get(long id)
          {
              var product = _productFetcher.GetProduct(id);
              return _productMapper.CreateProduct(product);
          }
  
  
          public HttpResponseMessage Post(HttpRequestMessage request, Product product)
          {
              var modelProduct = new Data.Model.Product
              {
                  Description = product.Description,
                  ProductName = product.ProductName
              };
  
              _session.Save(modelProduct);
  
              var newProduct = _productMapper.CreateProduct(modelProduct);
  
              //var href = newproduct.Links.First(x =&gt; x.Rel == "self").Href;
  
              var response = request.CreateResponse(HttpStatusCode.Created, newProduct);
              //response.Headers.Add("Location", href);
  
              return response;
          }
  
  
          public HttpResponseMessage Delete()
          {
              var categories = _session.QueryOver&lt;Data.Model.Product&gt;().List();
              foreach (var product in categories)
              {
                  _session.Delete(product);
              }
  
              return new HttpResponseMessage(HttpStatusCode.OK);
          }
  
  
          public HttpResponseMessage Delete(long id)
          {
              var product = _session.Get&lt;Data.Model.Product&gt;(id);
              if (product != null)
              {
                  _session.Delete(product);
              }
  
              return new HttpResponseMessage(HttpStatusCode.OK);
          }
  
  
          public Product Put(long id, Product product)
          {
              var modelProduct = _productFetcher.GetProduct(id);
  
              modelProduct.ProductName = product.ProductName;
              modelProduct.Description = product.Description;
  
              _session.SaveOrUpdate(modelProduct);
  
              return _productMapper.CreateProduct(modelProduct);
          }
      }
  }
  </code></pre>
  
  <p>and the mapping class for Product table:</p>
  
  <pre><code>using TestMVCProject.Data.Model;
  using FluentNHibernate.Mapping;
  
  namespace TestMVCProject.Data.SqlServer.Mapping
  {
      public class ProductMap : ClassMap&lt;Product&gt;
      {
          public ProductMap()
          {
              Id(x =&gt; x.ProductId);
              Map(x =&gt; x.ProductName).Not.Nullable();
              Map(x =&gt; x.Description).Nullable();
              Map(x =&gt; x.CreateDate).Not.Nullable();
              Map(x =&gt; x.Price).Not.Nullable();               
  
              References&lt;Category&gt;(x =&gt; x.CategoryId).Not.Nullable();               
          }
      }
  }
  </code></pre>
  
  <p>What is wrong?</p>