我已经实现了 NHibernate 自定义上下文 (ICurrentSessionContext)。在这种情况下,我注入了 NHibernate 会话,因此我有每个呼叫模式设置的会话。好的,现在我制作了一个拦截器,它获取当前登录用户的 userId。现在我这样做:
public ISession CurrentSession()
{
// Get the WCF InstanceContext:
var contextManager = OperationContext.Current.InstanceContext.Extensions.Find<NHibernateContextManager>();
if (contextManager == null)
{
throw new InvalidOperationException(
@"There is no context manager available.
Check whether the NHibernateContextManager is added as InstanceContext extension.
Make sure the service is being created with the NhServiceHostFactory.
This Session Provider is intended only for WCF services.");
}
var session = contextManager.Session;
AuditLogInterceptor interceptor = new AuditLogInterceptor();
if (session == null)
{
session = this._factory.OpenSession(interceptor);
interceptor.Session = session;
contextManager.Session = session;
}
return contextManager.Session;
}
我的 AuditLogInterceptor 采用 UserId,但我不知道如何(从哪里)获取此 userId。