Luk*_*uka 6 nhibernate session wcf interceptor
我已经实现了NHibernate自定义上下文(ICurrentSessionContext).在这个上下文中,我注入NHibernate会话,所以我有每个调用模式设置Session.好的,现在我已经制作了一个拦截器,它接受当前登录用户的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;
}
Run Code Online (Sandbox Code Playgroud)
我的AuditLogInterceptor采用UserId,但我不知道如何(从哪里)获取此userId.
如果您的用户已通过身份验证,您可以使用:
OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1293 次 |
| 最近记录: |