Created
March 22, 2011 15:02
-
-
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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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