Skip to content

Instantly share code, notes, and snippets.

@etishor
Created March 22, 2011 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save etishor/881356 to your computer and use it in GitHub Desktop.
Save etishor/881356 to your computer and use it in GitHub Desktop.
Sample WCF message inspector to hook up nhibernate session binding/unbinding from the WcfOperationSessionContext
// this code was simplified for the purpose of beeing a sample
public class UowDispatchMessageInspector : IDispatchMessageInspector
{
private readonly ISessionFactory factory;
public UowDispatchMessageInspector(ISessionFactory factory)
{
this.factory = factory;
}
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
try
{
CurrentSessionContext.Bind(factory.OpenSession());
}
catch (Exception x)
{
// if we throw anything other than FaultException service crashes.
throw new FaultException(x.Message);
}
return request.Headers.Action;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
try
{
ISession session = CurrentSessionContext.Unbind(factory);
session.Dispose();
}
catch (Exception x)
{
if (log.IsErrorEnabled) log.Error("Error commiting Unit Of Work", x);
reply = Message.CreateMessage(reply.Version, new FaultCode("Error"),x.Message, reply.Headers.Action);
// rethrowing crashes the service.
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment