Pratip Bagchi
2018-11-12 17:43:18 UTC
I am in dire need of your help. I have migrated some legacy .Net web API
applications to .net core and get it all working except the Envers piece of
it. The major problem I am facing is to pass user name to the
RevisionListener. As RevisionListener does not support DI I am not able to
inject HttpContextAccessor to get the context. Alternatively if I use
static HttpContextAccessor I am running with same context from two
different request.
Even if I use "IHttpContextAccessor " due t static nature of the class
Revision table is holding the same context between two requests from 2
different users.
Any example will be appreciated!
Here is the code I am using to configure nHibernate
public static class NHibernateExtension
{
private static readonly ILog log =
LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static void AddNhibernate(this IServiceCollection services,
string connectionString,string schemaName )
{
services.AddSingleton((provider) =>
{
var cfg = new NHibernate.Cfg.Configuration();
cfg.Configure("hibernate.cfg.xml");
cfg.SetProperty("connection.connection_string",
connectionString);
cfg.AddProperties(new Dictionary<string, string>
{
{ NHibernate.Cfg.Environment.DefaultSchema, schemaName }
});
cfg.AddMapping(NHibernateConfig.GetMappings());
var enversConfiguration = GetEnversConfiguration();
cfg.SetEnversProperty(ConfigurationKey.DefaultSchema,
"Audit");
cfg.IntegrateWithEnvers(enversConfiguration);
return cfg;
});
services.AddSingleton((provider) =>
provider.GetService<NHibernate.Cfg.Configuration>().BuildSessionFactory());
services.AddScoped((provider) =>
provider.GetService<ISessionFactory>().OpenSession());
}
private static FluentConfiguration GetEnversConfiguration()
{
var enversConf = new FluentConfiguration();
var userId = HttpContext.Current?.Request?.Headers["user_name"];
var userName = HttpContext.Current?.Request?.Headers["name"];
RevisionListener rn = new RevisionListener(userName, userId);
enversConf.SetRevisionEntity<RevisionDetails>(x => x.Id, x =>
x.RevisionTimestamp, rn);
enversConf.Audit<PrinterMapping>().SetTableInfo(x => x.Value =
typeof(PrinterMapping).Name);
enversConf.Audit<Label>().SetTableInfo(x => x.Value =
typeof(Label).Name);
enversConf.Audit<Language>().SetTableInfo(x => x.Value =
typeof(Language).Name);
enversConf.Audit<Translation>().SetTableInfo(x => x.Value =
typeof(Translation).Name);
return enversConf;
}
}
//this provides HttpContext through IHttpContextAccessor
public static class HttpContext
{
private static IHttpContextAccessor _contextAccessor;
public static Microsoft.AspNetCore.Http.HttpContext Current =>
_contextAccessor.HttpContext;
internal static void Configure(IHttpContextAccessor contextAccessor)
{
_contextAccessor = contextAccessor;
}
}
***/Listener******/
public class RevisionListener : IRevisionListener {
private string _userName = string.Empty;
private string _userId = string.Empty;
public RevisionListener(string userName, string userId)
: base()
{
this._userName = userName;
this._userId = userId;
}
public void NewRevision(object revisionEntity)
{
var casted = revisionEntity as RevisionDetails;
if (casted != null)
{
casted.UserName = this._userName;
casted.UserId = this._userId;
}
}
}
applications to .net core and get it all working except the Envers piece of
it. The major problem I am facing is to pass user name to the
RevisionListener. As RevisionListener does not support DI I am not able to
inject HttpContextAccessor to get the context. Alternatively if I use
static HttpContextAccessor I am running with same context from two
different request.
Even if I use "IHttpContextAccessor " due t static nature of the class
Revision table is holding the same context between two requests from 2
different users.
Any example will be appreciated!
Here is the code I am using to configure nHibernate
public static class NHibernateExtension
{
private static readonly ILog log =
LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static void AddNhibernate(this IServiceCollection services,
string connectionString,string schemaName )
{
services.AddSingleton((provider) =>
{
var cfg = new NHibernate.Cfg.Configuration();
cfg.Configure("hibernate.cfg.xml");
cfg.SetProperty("connection.connection_string",
connectionString);
cfg.AddProperties(new Dictionary<string, string>
{
{ NHibernate.Cfg.Environment.DefaultSchema, schemaName }
});
cfg.AddMapping(NHibernateConfig.GetMappings());
var enversConfiguration = GetEnversConfiguration();
cfg.SetEnversProperty(ConfigurationKey.DefaultSchema,
"Audit");
cfg.IntegrateWithEnvers(enversConfiguration);
return cfg;
});
services.AddSingleton((provider) =>
provider.GetService<NHibernate.Cfg.Configuration>().BuildSessionFactory());
services.AddScoped((provider) =>
provider.GetService<ISessionFactory>().OpenSession());
}
private static FluentConfiguration GetEnversConfiguration()
{
var enversConf = new FluentConfiguration();
var userId = HttpContext.Current?.Request?.Headers["user_name"];
var userName = HttpContext.Current?.Request?.Headers["name"];
RevisionListener rn = new RevisionListener(userName, userId);
enversConf.SetRevisionEntity<RevisionDetails>(x => x.Id, x =>
x.RevisionTimestamp, rn);
enversConf.Audit<PrinterMapping>().SetTableInfo(x => x.Value =
typeof(PrinterMapping).Name);
enversConf.Audit<Label>().SetTableInfo(x => x.Value =
typeof(Label).Name);
enversConf.Audit<Language>().SetTableInfo(x => x.Value =
typeof(Language).Name);
enversConf.Audit<Translation>().SetTableInfo(x => x.Value =
typeof(Translation).Name);
return enversConf;
}
}
//this provides HttpContext through IHttpContextAccessor
public static class HttpContext
{
private static IHttpContextAccessor _contextAccessor;
public static Microsoft.AspNetCore.Http.HttpContext Current =>
_contextAccessor.HttpContext;
internal static void Configure(IHttpContextAccessor contextAccessor)
{
_contextAccessor = contextAccessor;
}
}
***/Listener******/
public class RevisionListener : IRevisionListener {
private string _userName = string.Empty;
private string _userId = string.Empty;
public RevisionListener(string userName, string userId)
: base()
{
this._userName = userName;
this._userId = userId;
}
public void NewRevision(object revisionEntity)
{
var casted = revisionEntity as RevisionDetails;
if (casted != null)
{
casted.UserName = this._userName;
casted.UserId = this._userId;
}
}
}
--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nhusers+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nhusers+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.