in Education by
I have been implementing a [] attribute for some of our larger .NET solutions that will allow configurable analytics to be easily added to any functions/methods that are considered important. I'm using Fody and the MethodBoundaryAspect to intercept the entry and exit of each function and record metrics. This works well for synchronous functions, and for methods that return Task there is a workable solution with Task.ContinueWith, but for F# Async-returning functions, the OnExit from the MethodBoundaryAspect runs as soon as the Async is returned (rather than when the Async is actually executed). In order to capture correct metrics for F# Async-returning functions, I was trying to come up with an equivalent solution to using Task.ContinueWith, but the closest thing I could think of was to create a new Async that binds the first one, runs the metric-capturing functions, and then returns the original result. This is further complicated by the fact that the F# Async return value I'm intercepting is presented only as an obj, and I have to do everything thereafter reflectively, as there is no non-generic version of Async like there is with Task that I can use without knowing the exact return type. My best solution so far looks roughly like this: open System open System.Diagnostics open FSharp.Reflection open MethodBoundaryAspect.Fody.Attributes [] [] type TraceAttribute () = inherit OnMethodBoundaryAspect() let traceEvent (args: MethodExecutionArgs) (timestamp: int64) = // Capture metrics here () override __.OnEntry (args) = Stopwatch.GetTimestamp() |> traceEvent args override __.OnExit (args) = let exit () = Stopwatch.GetTimestamp() |> traceEvent args match args.ReturnValue with | :? System.Threading.Tasks.Task as task -> task.ContinueWith(fun _ -> exit()) |> ignore | other -> // Here's where I could use some help let clrType = other.GetType() if clrType.IsGenericType && clrType.GetGenericTypeDefinition() = typedefof> then // If the return type is an F# Async, replace it with a new Async that calls exit after the original return value is computed let returnType = clrType.GetGenericArguments().[0] let functionType = FSharpType.MakeFunctionType(returnType, typedefof>.MakeGenericType([| returnType |])) let f = FSharpValue.MakeFunction(functionType, (fun _ -> exit(); other)) let result = typeof.GetMethod("Bind").MakeGenericMethod([|returnType; returnType|]).Invoke(async, [|other; f|]) args.ReturnValue <- result else exit() Unfortunately, this solution is not only quite messy, but I believe the reflective construction of an Async computation is adding a non-trivial amount of overhead, especially when I'm trying to trace functions that are called in a loop or have deeply-nested Async calls. Is there a better way to achieve the same result of running a given function immediately after an Async computation is actually evaluated? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Something like this is probably what you need: let traceAsync (a:Async<_>) = async { trace() // trace start of async let! r = a trace() // trace end of async return r } Consider that when a function returns an async that does not mean the async has started. An async is more like a function, it can be invoked several times or none at all. Which means you need to check if the return value is an Async also in your OnEntry method.

Related questions

0 votes
    Where request is a HttpRequestMessage from System.Net.Http, I'm trying to use pattern matching to determine ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    Given the following F# snippet: type A(children: A list) = member val P1 = "" member val P2 = " ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 6, 2022 in Education by JackTerrance
0 votes
    What is the added value for learning F# when you are already familiar with LISP? JavaScript questions and ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    After installing the F# September CTP (1.9.6.2), Visual Studio 2008 frequently gives an error "Microsoft ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 10, 2022 in Education by JackTerrance
0 votes
    I have learned a Machine Learning course using Matlab as a prototyping tool. Since I got addicted to F#, I ... of resources? Thanks. Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    URL is http://*.*.*.*/100/?id=1&version=1 params is {"cityId": "110000", "query": {" ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 27, 2022 in Education by JackTerrance
0 votes
    I am trying to use ES2017 async/await syntax with Babel. In package.json, I have "babel": { " ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 14, 2022 in Education by JackTerrance
0 votes
    I am trying to use ES2017 async/await syntax with Babel. In package.json, I have "babel": { " ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    I have questions regarding the execution order of async jobs. I will ask my question with example because ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    I am trying to use ES2017 async/await syntax with Babel. In package.json, I have "babel": { " ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    I have questions regarding the execution order of async jobs. I will ask my question with example because ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 8, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: asynchronous python itertools chain multiple generators (2 answers) Closed 2 ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    I am trying to make a simple useQuery request but the variables which I am dependent on for the request are ... data } = useQuery(GET_USER_DATA, { variables: userVariables, //...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    I'm having an annoying problem registering a javascript event from inside a user control within a formview ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 13, 2022 in Education by JackTerrance
0 votes
    What is the type of datatype the async attribute optionally accepts? (a) Integer (b) String (c) ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 23, 2021 in Education by JackTerrance
...