private Assembly OnAppDomainAssemblyResolve(object sender, ResolveEventArgs args)
            {
                if (handlerReturn == HandlerReturn.Exception)
                {
                    throw new BinderTestException("Exception in handler for AppDomain.AssemblyResolve");
                }

                var      assemblyName  = new AssemblyName(args.Name);
                var      customContext = new CustomALC(nameof(OnAppDomainAssemblyResolve));
                Assembly asm           = ResolveAssembly(customContext, assemblyName);
                var      invocation    = new HandlerInvocation()
                {
                    AssemblyName = assemblyName,
                    HandlerName  = nameof(OnAppDomainAssemblyResolve),
                };

                if (asm != null)
                {
                    invocation.ResultAssemblyName = asm.GetName();
                    invocation.ResultAssemblyPath = asm.Location;
                }

                Invocations.Add(invocation);
                return(asm);
            }
예제 #2
0
        private HandlerInvocation ParseHandlerInvokedEvent(Func <string, string> getDataString)
        {
            var handlerInvocation = new HandlerInvocation()
            {
                AssemblyName        = new AssemblyName(getDataString("AssemblyName")),
                HandlerName         = getDataString("HandlerName"),
                AssemblyLoadContext = getDataString("AssemblyLoadContext"),
                ResultAssemblyPath  = getDataString("ResultAssemblyPath")
            };
            string resultName = getDataString("ResultAssemblyName");

            if (!string.IsNullOrEmpty(resultName))
            {
                handlerInvocation.ResultAssemblyName = new AssemblyName(resultName);
            }

            return(handlerInvocation);
        }
            private Assembly OnAssemblyLoadContextResolving(AssemblyLoadContext context, AssemblyName assemblyName)
            {
                Assembly asm        = ResolveAssembly(context, assemblyName);
                var      invocation = new HandlerInvocation()
                {
                    AssemblyName        = assemblyName,
                    HandlerName         = nameof(OnAssemblyLoadContextResolving),
                    AssemblyLoadContext = context == AssemblyLoadContext.Default ? context.Name : context.ToString(),
                };

                if (asm != null)
                {
                    invocation.ResultAssemblyName = asm.GetName();
                    invocation.ResultAssemblyPath = asm.Location;
                }

                Invocations.Add(invocation);
                return(asm);
            }
            private Assembly OnAssemblyLoadContextResolving(AssemblyLoadContext context, AssemblyName assemblyName)
            {
                if (handlerReturn == HandlerReturn.Exception)
                {
                    throw new BinderTestException("Exception in handler for AssemblyLoadContext.Resolving");
                }

                Assembly asm        = ResolveAssembly(context, assemblyName);
                var      invocation = new HandlerInvocation()
                {
                    AssemblyName        = assemblyName,
                    HandlerName         = nameof(OnAssemblyLoadContextResolving),
                    AssemblyLoadContext = context == AssemblyLoadContext.Default ? context.Name : context.ToString(),
                };

                if (asm != null)
                {
                    invocation.ResultAssemblyName = asm.GetName();
                    invocation.ResultAssemblyPath = asm.Location;
                }

                Invocations.Add(invocation);
                return(asm);
            }
예제 #5
0
        protected override void OnEventWritten(EventWrittenEventArgs data)
        {
            if (data.EventSource.Name != "Microsoft-Windows-DotNETRuntime")
            {
                return;
            }

            object GetData(string name)
            {
                int index = data.PayloadNames.IndexOf(name);

                return(index >= 0 ? data.Payload[index] : null);
            };
            string GetDataString(string name) => GetData(name) as string;

            switch (data.EventName)
            {
            case "AssemblyLoadStart":
            {
                BindOperation bindOperation = ParseAssemblyLoadStartEvent(data, GetDataString);
                lock (eventsLock)
                {
                    Assert.IsTrue(!bindOperations.ContainsKey(data.ActivityId), "AssemblyLoadStart should not exist for same activity ID ");
                    bindOperation.Nested = bindOperations.ContainsKey(data.RelatedActivityId);
                    bindOperations.Add(data.ActivityId, bindOperation);
                    if (bindOperation.Nested)
                    {
                        bindOperations[data.RelatedActivityId].NestedBinds.Add(bindOperation);
                    }
                }
                break;
            }

            case "AssemblyLoadStop":
            {
                lock (eventsLock)
                {
                    Assert.IsTrue(bindOperations.ContainsKey(data.ActivityId), "AssemblyLoadStop should have a matching AssemblyBindStart");
                    BindOperation bind = bindOperations[data.ActivityId];
                    bind.Success = (bool)GetData("Success");
                    string resultName = GetDataString("ResultAssemblyName");
                    if (!string.IsNullOrEmpty(resultName))
                    {
                        bind.ResultAssemblyName = new AssemblyName(resultName);
                    }
                    bind.ResultAssemblyPath = GetDataString("ResultAssemblyPath");
                    bind.Cached             = (bool)GetData("Cached");
                    bind.Completed          = true;
                }
                break;
            }

            case "AssemblyLoadContextResolvingHandlerInvoked":
            {
                HandlerInvocation handlerInvocation = ParseHandlerInvokedEvent(GetDataString);
                lock (eventsLock)
                {
                    Assert.IsTrue(bindOperations.ContainsKey(data.ActivityId), "AssemblyLoadContextResolvingHandlerInvoked should have a matching AssemblyBindStart");
                    BindOperation bind = bindOperations[data.ActivityId];
                    bind.AssemblyLoadContextResolvingHandlers.Add(handlerInvocation);
                }
                break;
            }

            case "AppDomainAssemblyResolveHandlerInvoked":
            {
                HandlerInvocation handlerInvocation = ParseHandlerInvokedEvent(GetDataString);
                lock (eventsLock)
                {
                    Assert.IsTrue(bindOperations.ContainsKey(data.ActivityId), "AppDomainAssemblyResolveHandlerInvoked should have a matching AssemblyBindStart");
                    BindOperation bind = bindOperations[data.ActivityId];
                    bind.AppDomainAssemblyResolveHandlers.Add(handlerInvocation);
                }
                break;
            }
            }
        }
예제 #6
0
        protected override void OnEventWritten(EventWrittenEventArgs data)
        {
            if (data.EventSource.Name != "Microsoft-Windows-DotNETRuntime")
            {
                return;
            }

            object GetData(string name)
            {
                int index = data.PayloadNames.IndexOf(name);

                return(index >= 0 ? data.Payload[index] : null);
            };
            string GetDataString(string name) => GetData(name) as string;

            switch (data.EventName)
            {
            case "AssemblyLoadStart":
            {
                BindOperation bindOperation = ParseAssemblyLoadStartEvent(data, GetDataString);
                if (!IsLoadToTrack(bindOperation.AssemblyName.Name))
                {
                    return;
                }

                lock (eventsLock)
                {
                    Assert.IsTrue(!bindOperations.ContainsKey(data.ActivityId), "AssemblyLoadStart should not exist for same activity ID ");
                    bindOperation.Nested = bindOperations.ContainsKey(data.RelatedActivityId);
                    bindOperations.Add(data.ActivityId, bindOperation);
                    if (bindOperation.Nested)
                    {
                        bindOperations[data.RelatedActivityId].NestedBinds.Add(bindOperation);
                    }
                }
                break;
            }

            case "AssemblyLoadStop":
            {
                string assemblyName = GetDataString("AssemblyName");
                if (!IsLoadToTrack(new AssemblyName(assemblyName).Name))
                {
                    return;
                }

                bool   success    = (bool)GetData("Success");
                string resultName = GetDataString("ResultAssemblyName");
                lock (eventsLock)
                {
                    if (!bindOperations.ContainsKey(data.ActivityId))
                    {
                        Assert.Fail(GetMissingAssemblyBindStartMessage(data, $"Success={success}, Name={resultName}"));
                    }

                    BindOperation bind = bindOperations[data.ActivityId];
                    bind.Success = success;
                    if (!string.IsNullOrEmpty(resultName))
                    {
                        bind.ResultAssemblyName = new AssemblyName(resultName);
                    }
                    bind.ResultAssemblyPath = GetDataString("ResultAssemblyPath");
                    bind.Cached             = (bool)GetData("Cached");
                    bind.Completed          = true;
                }
                break;
            }

            case "ResolutionAttempted":
            {
                ResolutionAttempt attempt = ParseResolutionAttemptedEvent(GetData, GetDataString);
                if (!IsLoadToTrack(attempt.AssemblyName.Name))
                {
                    return;
                }

                lock (eventsLock)
                {
                    if (!bindOperations.ContainsKey(data.ActivityId))
                    {
                        Assert.Fail(GetMissingAssemblyBindStartMessage(data, attempt.ToString()));
                    }

                    BindOperation bind = bindOperations[data.ActivityId];
                    bind.ResolutionAttempts.Add(attempt);
                }
                break;
            }

            case "AssemblyLoadContextResolvingHandlerInvoked":
            {
                HandlerInvocation handlerInvocation = ParseHandlerInvokedEvent(GetDataString);
                if (!IsLoadToTrack(handlerInvocation.AssemblyName.Name))
                {
                    return;
                }

                lock (eventsLock)
                {
                    if (!bindOperations.ContainsKey(data.ActivityId))
                    {
                        Assert.Fail(GetMissingAssemblyBindStartMessage(data, handlerInvocation.ToString()));
                    }

                    BindOperation bind = bindOperations[data.ActivityId];
                    bind.AssemblyLoadContextResolvingHandlers.Add(handlerInvocation);
                }
                break;
            }

            case "AppDomainAssemblyResolveHandlerInvoked":
            {
                HandlerInvocation handlerInvocation = ParseHandlerInvokedEvent(GetDataString);
                if (!IsLoadToTrack(handlerInvocation.AssemblyName.Name))
                {
                    return;
                }

                lock (eventsLock)
                {
                    if (!bindOperations.ContainsKey(data.ActivityId))
                    {
                        Assert.Fail(GetMissingAssemblyBindStartMessage(data, handlerInvocation.ToString()));
                    }

                    BindOperation bind = bindOperations[data.ActivityId];
                    bind.AppDomainAssemblyResolveHandlers.Add(handlerInvocation);
                }
                break;
            }

            case "AssemblyLoadFromResolveHandlerInvoked":
            {
                LoadFromHandlerInvocation loadFrom = ParseLoadFromHandlerInvokedEvent(GetData, GetDataString);
                if (!IsLoadToTrack(loadFrom.AssemblyName.Name))
                {
                    return;
                }

                lock (eventsLock)
                {
                    if (!bindOperations.ContainsKey(data.ActivityId))
                    {
                        Assert.Fail(GetMissingAssemblyBindStartMessage(data, loadFrom.ToString()));
                    }

                    BindOperation bind = bindOperations[data.ActivityId];
                    bind.AssemblyLoadFromHandler = loadFrom;
                }
                break;
            }

            case "KnownPathProbed":
            {
                ProbedPath probedPath = ParseKnownPathProbedEvent(GetData, GetDataString);
                string     name       = System.IO.Path.GetFileNameWithoutExtension(probedPath.FilePath);
                if (!IsLoadToTrack(name.EndsWith(".ni") ? name.Remove(name.Length - 3) : name))
                {
                    return;
                }

                lock (eventsLock)
                {
                    if (!bindOperations.ContainsKey(data.ActivityId))
                    {
                        Assert.Fail(GetMissingAssemblyBindStartMessage(data, probedPath.ToString()));
                    }

                    BindOperation bind = bindOperations[data.ActivityId];
                    bind.ProbedPaths.Add(probedPath);
                }
                break;
            }
            }
        }