chrc2
08-05-2008, 04:45 PM
Hi,
I might be missing the point completely, but, in InstanceContextMode.PerCall a new instance of the service is created for each operation, and it is disposed immediately afterwards. Therefore, given a factory that can provide nhibernate sessions, a reference to the current session could be held in the service itself, and the session could be disposed of in the dispose method.
Wouldn’t this work?
Or why are there so many posts and blogs about people using ICallContextInitializer and the like to hold sessions?
example code:
namespace Spring.WcfQuickStart
{
[ServiceBehavior(
ConcurrencyMode = ConcurrencyMode.Multiple,
InstanceContextMode = InstanceContextMode.PerCall,
IncludeExceptionDetailInFaults = true)]
public class CalculatorService : HasAdaptors, ICalculator, IDisposable
{
private ISession thisSession;
private ISession Session
{
get
{
if (thisSession == null)
{
thisSession = SessionFactory.CreateSession();
}
return thisSession;
}
}
private int sleepInSeconds;
public int SleepInSeconds
{
get { return sleepInSeconds; }
set { sleepInSeconds = value; }
}
[Required]
public ISessionFactory SessionFactory
{
get { return sessionProvider; }
set { sessionProvider = value; }
}
private ISessionFactory sessionProvider;
public double Add(double n1, double n2)
{
using (TransactionScope tx = new TransactionScope())
{
Console.WriteLine("--- Start : Add ---");
OperationContext.Current.OperationCompleted += Current_OperationCompleted;
Transaction.Current.TransactionCompleted += Current_TransactionCompleted;
Folder folder = new Folder();
folder.Name = "dada";
folder.SecurityClassification = SecurityClassification.Secret;
Session.CreateOrUpdate(folder);
Thread.Sleep(sleepInSeconds*1000);
tx.Complete();
}
foreach (IAdaptor adaptor in Adaptors)
{
adaptor.Notify();
}
Console.WriteLine("--- End : Add ---");
double result = n1 + n2;
//CalculatorSubscription.NotifyClientsSomeThingHappe ned(result);
new LayerNotifier().SomeThingHappened(result);
return result;
}
#region IDisposable Members
public void Dispose()
{
Session.Close();
thisSession.Dispose();
thisSession = null;
Console.WriteLine("calc service shutting down");
}
#endregion
}
}
I might be missing the point completely, but, in InstanceContextMode.PerCall a new instance of the service is created for each operation, and it is disposed immediately afterwards. Therefore, given a factory that can provide nhibernate sessions, a reference to the current session could be held in the service itself, and the session could be disposed of in the dispose method.
Wouldn’t this work?
Or why are there so many posts and blogs about people using ICallContextInitializer and the like to hold sessions?
example code:
namespace Spring.WcfQuickStart
{
[ServiceBehavior(
ConcurrencyMode = ConcurrencyMode.Multiple,
InstanceContextMode = InstanceContextMode.PerCall,
IncludeExceptionDetailInFaults = true)]
public class CalculatorService : HasAdaptors, ICalculator, IDisposable
{
private ISession thisSession;
private ISession Session
{
get
{
if (thisSession == null)
{
thisSession = SessionFactory.CreateSession();
}
return thisSession;
}
}
private int sleepInSeconds;
public int SleepInSeconds
{
get { return sleepInSeconds; }
set { sleepInSeconds = value; }
}
[Required]
public ISessionFactory SessionFactory
{
get { return sessionProvider; }
set { sessionProvider = value; }
}
private ISessionFactory sessionProvider;
public double Add(double n1, double n2)
{
using (TransactionScope tx = new TransactionScope())
{
Console.WriteLine("--- Start : Add ---");
OperationContext.Current.OperationCompleted += Current_OperationCompleted;
Transaction.Current.TransactionCompleted += Current_TransactionCompleted;
Folder folder = new Folder();
folder.Name = "dada";
folder.SecurityClassification = SecurityClassification.Secret;
Session.CreateOrUpdate(folder);
Thread.Sleep(sleepInSeconds*1000);
tx.Complete();
}
foreach (IAdaptor adaptor in Adaptors)
{
adaptor.Notify();
}
Console.WriteLine("--- End : Add ---");
double result = n1 + n2;
//CalculatorSubscription.NotifyClientsSomeThingHappe ned(result);
new LayerNotifier().SomeThingHappened(result);
return result;
}
#region IDisposable Members
public void Dispose()
{
Session.Close();
thisSession.Dispose();
thisSession = null;
Console.WriteLine("calc service shutting down");
}
#endregion
}
}