Search code examples
c#wcfasynchronouspostsharp

How to make PostSharp's OnMethodBoundaryAspect OnEntry method Asynchronous


I apologize if I missed something and this question doesn't make sense or the answer wasn't obvious to me when reading through the PostSharp documentation.

I have created a simple logging aspect that uses a custom logging service to put details about a WCF method call into a database. Once the logging aspect is called, it needs to continue on its own and not hold up the execution of the WCF method itself. I am curious if the OnEntry() method can be made asynchronous? The custom logging service uses asynchronous calls to put the logs into the database, but was hoping for more separation between the two.


Aspect:

[Serializable]
class LoggingAspect : OnMethodBoundaryAspect
{
    public override void OnEntry(MethodExecutionArgs args)
    {
        // Perform the logging
    }
}


WCF Method:

[LoggingAspect]
public String DoSomething(int param1, int param2)
{
    // Do stuff?
}



EDIT: Solution Thanks to the info below I was able to come up with a solution that works for me. I was able to test this by writing some timestamps to a file for DoSomething() and the OnEntry(). I added a Thread.Sleep(30000) followed by another writing of a timestamp to PerformLog(). My DoSomething() method was able to finish up before PerformLog() finished off writing to my logging service :)


Aspect:

[Serializable]
class LoggingAspect : OnMethodBoundaryAspect
{
    public override void OnEntry(MethodExecutionArgs args)
    {
        // Write timestamp to file.
        Task.Run( () => PerformLog(args) );
    }

    private void PerformLog(MethodExecutiionArgs args)
    {
        // Thread.Sleep(30000);
        // Write timestamp to file.
        // Perform the logging
    } 
}


WCF Method:

[LoggingAspect]
public String DoSomething(int param1, int param2)
{
    // Write timestamp to file.
    // Do stuff?
}

Solution

  • Why don't you just start a Task for your logging logic and let the OnEntry method return?