1
votes

I have an Orchard CMS module that loads up some code which provides service functions. The service code is written to be host agnostic and has been used with ASP.NET and WCF previously. The service code uses MEF to load plugins. One such plugin is for audit.

In an attempt to allow access to the Orchard database for audit I have modified the service code to also allow the host to pass in an audit implementation instance. Thus my Orchard module can pass in an instance when the service starts with the intention that this instance writes audit data as records in the Orchard DB.

I have created a migration for my database:

    public int UpdateFrom5()
    {
        SchemaBuilder.CreateTable("AuditRecord",
            table => table
                .Column<int>("Id", c => c.PrimaryKey().Identity())
                .Column<int>("AuditPoint")
                .Column<DateTime>("EventTime")
                .Column("CampaignId", DbType.Guid)
                .Column("CallId", DbType.Guid)
                .Column<String>("Data")
                );
        return 6;
    }

I have created my AuditRecord model in Models:

namespace MyModule.Models
{
    public class AuditRecord
    {
        public virtual int Id { get; set; }
        public virtual int AuditPoint { get; set; }
        public virtual DateTime EventTime { get; set; }
        public virtual Guid CampaignId { get; set; }
        public virtual Guid CallId { get; set; }
        public virtual String Data { get; set; }
    }
}

I have added an IAuditWriter interface that derives from IDependency so that I can inject a new instance when my module starts.

public interface IAuditWriter : IDependency
{
    void WriteAuditRecord(AuditRecord data);
}

For my audit writer instance to work with the existing service code it must be derived from an abstract class FlowSinkAudit defined in the service library. The abstract class defines the Audit method. When the service needs to write audit it calls the audit method on all instances derived from the FlowAuditSink abstract class that have been instantiated either through MEF or by passing in an instance at startup.

public class AuditWriter : FlowAuditSink, IAuditWriter
{
    private readonly IComponentContext ctx;
    private readonly IRepository<AuditRecord> repo;
    public AuditWriter(IComponentContext ctx, IRepository<AuditRecord> repo)
    {
        this.ctx = ctx;
        this.repo = repo;
    }

    public void WriteAuditRecord(AuditRecord data)
    {
        // Get an audit repo
        //IRepository<AuditRecord> repo = (IRepository<AuditRecord>)ctx.Resolve(typeof(IRepository<AuditRecord>));
        using (System.Transactions.TransactionScope t = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Suppress))
        {
            this.repo.Create(data);
        }
    }

    public override void Audit(DateTime eventTime, AuditPoint auditPoint, Guid campaignId, Guid callId, IDictionary<String, Object> auditPointData)
    {
        // Add code here to write audit into the Orchard DB.
        AuditRecord ar = new AuditRecord();
        ar.AuditPoint = (int)auditPoint;
        ar.EventTime = eventTime;
        ar.CampaignId = campaignId;
        ar.CallId = callId;
        ar.Data = auditPointData.AsString();
        WriteAuditRecord(ar);
    }
}

My service code is started from a module level class that implements IOrchardShellEvents

public class Module : IOrchardShellEvents
{
    private readonly IAuditWriter audit;
    private readonly IRepository<ServiceSettingsPartRecord> settingsRepository;
    private readonly IScheduledTaskManager taskManager;
    private static readonly Object syncObject = new object();

    public ILogger logger { get; set; }

    public Module(IScheduledTaskManager taskManager, IRepository<ServiceSettingsPartRecord> settingsRepository, IAuditWriter audit)
    {
        this.audit = audit;
        this.settingsRepository = settingsRepository;
        this.taskManager = taskManager;
        logger = NullLogger.Instance;
    }
...

When the service is started during the "Activated" event, I pass this.Audit to the service instance.

    public void Activated()
    {
        lock (syncObject)
        {
            var settings = settingsRepository.Fetch(f => f.StorageProvider != null).FirstOrDefault();
            InitialiseServer();
            // Auto start the server
            if (!StartServer(settings))
            {
                // Auto start failed, setup a scheduled task to retry
                var tasks = taskManager.GetTasks(ServerAutostartTask.TaskType);
                if (tasks == null || tasks.Count() == 0)
                    taskManager.CreateTask(ServerAutostartTask.TaskType, DateTime.Now + TimeSpan.FromSeconds(60), null);
            }
        }
    }
...
    private void InitialiseServer()
    {
        if (!Server.IsInitialized)
        {
            var systemFolder = @"C:\Scratch\Plugins";
            if (!Directory.Exists(systemFolder))
                Directory.CreateDirectory(systemFolder);

            var cacheFolder = System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/MyModule/Cache");
            if (!Directory.Exists(cacheFolder))
                Directory.CreateDirectory(cacheFolder);

            Server.Initialise(systemFolder, cacheFolder, null, (FlowAuditSink)audit);
        }
    }

All of this works as expected and my service code calls the audit sink.

My problem is that when the audit sink is called and I try to write the audit to the database using this.repo.Create(data) nothing is written.

I have also attempted to create a new repository object by using the IComponentContext interface but this errors with object already disposed. I assume this is because the audit sink is a long lived object instance.

I have attempted both with and without the current transaction suspended which doesn't affect the result. I assume this is because the call is not coming through ASP.NET MVC but from a thread created by the service code.

Can anyone tell my how I can get my audit data to appear in the Orchard database?

Thanks

Chris.

1
how your repo.Create method works? can you write this method's implementation please if possible? - mehmet mecek
@Mecek - I have not implemented repo.Create, I simply call it. Have I missed something here? - C Hall
are you sure that Create method works correctly? - mehmet mecek
@Mecek - If I call repo.Create from an Http request thread in a controller it works fine. In such a case the repository instance is injected by Autofac into the constructor of the controller. The issue seems to be that the Orchard repository implementation doesn't complete the transaction until the repo object is disposed and doesn't provide any way for the application to achieve that. I have tried to to resolve a new repo object each time I need to create a record by implementing IShim but the IOrcharHostContainer that I receive returns null when I attempt to resolve IRepository<AuditRecord> - C Hall
If you are using nhibernate, your nhibernate session object may need to be flushed before it is disposed. What I need to know is which tool do you use for querying your db. May be you call create but your session is not flushed. - mehmet mecek

1 Answers

2
votes

Well, I have a solution, but as I'm not very familiar with Orchards architecture it may not be the best way.

After a good deal of delving into the Orchard sources it struck me that the crux of this issue can be summarised as

"how do I access the Orchard autofac injection mechanism from a thread that does not use the Http request pipeline".

I figured that this is what a scheduled task must do so I created a scheduled task and set a breakpoint in IScheduledTaskHandler.Process to discover how the task was executed. Looking at Orchard\Tasks\SweepGenerator.cs showed me the way.

I modified my AuditWriter thusly:

public interface IAuditWriter : ISingletonDependency
{
}

public class AuditWriter : FlowAuditSink, IAuditWriter
{
    private readonly IWorkContextAccessor _workContextAccessor;

    public AuditWriter(IWorkContextAccessor workContextAccessor)
    {
        _workContextAccessor = workContextAccessor;
    }

    public override void Audit(DateTime eventTime, AuditPoint auditPoint, Guid campaignId, Guid callId, IDictionary<String, Object> auditPointData)
    {
        // Add code here to write audit into the Orchard DB.
        AuditRecord ar = new AuditRecord();
        ar.AuditPoint = (int)auditPoint;
        ar.EventTime = eventTime;
        ar.CampaignId = campaignId;
        ar.CallId = callId;
        ar.Data = auditPointData.AsString();

        using (var scope = _workContextAccessor.CreateWorkContextScope())
        {
            // resolve the manager and invoke it
            var repo = scope.Resolve<IRepository<AuditRecord>>();
            repo.Create(ar);
            repo.Flush();
        }
    }
}

scope.Resolve works and my data is successfully written to the Orchard DB.

At the moment, I don't think my use of ISingletonDependency is working correctly as my constructor is only called when my module injects an AuditWriter instance in its constructor and it happens more than once.

Anyway it seems that to gain access to the Orchard autofac resolution mechanism from a non Http thread we use IWorkContextAccessor

Please let me know if this is not correct.