private ResolutionAttempt ParseResolutionAttemptedEvent(Func <string, object> getData, Func <string, string> getDataString)
        {
            var attempt = new ResolutionAttempt()
            {
                AssemblyName        = new AssemblyName(getDataString("AssemblyName")),
                Stage               = (ResolutionAttempt.ResolutionStage)getData("Stage"),
                AssemblyLoadContext = getDataString("AssemblyLoadContext"),
                Result              = (ResolutionAttempt.ResolutionResult)getData("Result"),
                ResultAssemblyPath  = getDataString("ResultAssemblyPath"),
                ErrorMessage        = getDataString("ErrorMessage")
            };
            string resultName = getDataString("ResultAssemblyName");

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

            return(attempt);
        }
Beispiel #2
0
        private static ResolutionAttempt GetResolutionAttempt(AssemblyName assemblyName, ResolutionStage stage, AssemblyLoadContext alc, ResolutionResult result, AssemblyName resultAssemblyName, string resultAssemblyPath, string errorMessage = null)
        {
            var attempt = new ResolutionAttempt()
            {
                AssemblyName        = assemblyName,
                Stage               = stage,
                AssemblyLoadContext = alc == AssemblyLoadContext.Default ? DefaultALC : alc.ToString(),
                Result              = result,
                ResultAssemblyName  = resultAssemblyName,
                ResultAssemblyPath  = resultAssemblyPath
            };

            if (!string.IsNullOrEmpty(errorMessage))
            {
                attempt.ErrorMessage = errorMessage;
            }
            else
            {
                switch (result)
                {
                case ResolutionAttempt.ResolutionResult.AssemblyNotFound:
                    attempt.ErrorMessage = "Could not locate assembly";
                    break;

                case ResolutionAttempt.ResolutionResult.IncompatibleVersion:
                    attempt.ErrorMessage = $"Requested version {assemblyName.Version} is incompatible with found version {resultAssemblyName.Version}";
                    break;

                case ResolutionAttempt.ResolutionResult.MismatchedAssemblyName:
                    attempt.ErrorMessage = $"Requested assembly name '{assemblyName.FullName}' does not match found assembly name '{resultAssemblyName.FullName}'";
                    break;
                }
            }

            return(attempt);
        }
        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), $"{data.EventName} 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 "ResolutionAttempted":
            {
                ResolutionAttempt attempt = ParseResolutionAttemptedEvent(GetData, GetDataString);
                lock (eventsLock)
                {
                    Assert.IsTrue(bindOperations.ContainsKey(data.ActivityId), $"{data.EventName} should have a matching AssemblyBindStart");
                    BindOperation bind = bindOperations[data.ActivityId];
                    bind.ResolutionAttempts.Add(attempt);
                }
                break;
            }

            case "AssemblyLoadContextResolvingHandlerInvoked":
            {
                HandlerInvocation handlerInvocation = ParseHandlerInvokedEvent(GetDataString);
                lock (eventsLock)
                {
                    Assert.IsTrue(bindOperations.ContainsKey(data.ActivityId), $"{data.EventName} 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), $"{data.EventName} should have a matching AssemblyBindStart");
                    BindOperation bind = bindOperations[data.ActivityId];
                    bind.AppDomainAssemblyResolveHandlers.Add(handlerInvocation);
                }
                break;
            }

            case "AssemblyLoadFromResolveHandlerInvoked":
            {
                LoadFromHandlerInvocation loadFrom = ParseLoadFromHandlerInvokedEvent(GetData, GetDataString);
                lock (eventsLock)
                {
                    Assert.IsTrue(bindOperations.ContainsKey(data.ActivityId), $"{data.EventName} should have a matching AssemblyBindStart");
                    BindOperation bind = bindOperations[data.ActivityId];
                    bind.AssemblyLoadFromHandler = loadFrom;
                }
                break;
            }

            case "KnownPathProbed":
            {
                ProbedPath probedPath = ParseKnownPathProbedEvent(GetData, GetDataString);
                lock (eventsLock)
                {
                    Assert.IsTrue(bindOperations.ContainsKey(data.ActivityId), $"{data.EventName} should have a matching AssemblyBindStart");
                    BindOperation bind = bindOperations[data.ActivityId];
                    bind.ProbedPaths.Add(probedPath);
                }
                break;
            }
            }
        }
Beispiel #4
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;
            }
            }
        }