How to use OnResolve method of Microsoft.VisualStudio.TestPlatform.Common.Utilities.AssemblyResolver class

Best Vstest code snippet using Microsoft.VisualStudio.TestPlatform.Common.Utilities.AssemblyResolver.OnResolve

AssemblyResolver.cs

Source:AssemblyResolver.cs Github

copy

Full Screen

...53 this.searchDirectories = new HashSet<string>(directories);54 }55 this.platformAssemblyResolver = new PlatformAssemblyResolver();56 this.platformAssemblyLoadContext = new PlatformAssemblyLoadContext();57 this.platformAssemblyResolver.AssemblyResolve += this.OnResolve;58 }59 /// <summary>60 /// Set the directories from which assemblies should be searched61 /// </summary>62 /// <param name="directories"> The search directories. </param>63 [System.Security.SecurityCritical]64 internal void AddSearchDirectories(IEnumerable<string> directories)65 {66 if (EqtTrace.IsInfoEnabled)67 {68 EqtTrace.Info($"AssemblyResolver.AddSearchDirectories: Adding more searchDirectories {string.Join(",", directories)}");69 }70 foreach (var directory in directories)71 {72 this.searchDirectories.Add(directory);73 }74 }75 /// <summary>76 /// Assembly Resolve event handler for App Domain - called when CLR loader cannot resolve assembly.77 /// </summary>78 /// <returns>79 /// The <see cref="Assembly"/>.80 /// </returns>81 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]82 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2001:AvoidCallingProblematicMethods", MessageId = "System.Reflection.Assembly.LoadFrom")]83 private Assembly OnResolve(object sender, AssemblyResolveEventArgs args)84 {85 if (string.IsNullOrEmpty(args?.Name))86 {87 Debug.Fail("AssemblyResolver.OnResolve: args.Name is null or empty.");88 return null;89 }90 if (this.searchDirectories == null || this.searchDirectories.Count == 0)91 {92 EqtTrace.Info("AssemblyResolver.OnResolve: {0}: There are no search directories, returning.", args.Name);93 return null;94 }95 EqtTrace.Info("AssemblyResolver.OnResolve: {0}: Resolving assembly.", args.Name);96 // args.Name is like: "Microsoft.VisualStudio.TestTools.Common, Version=[VersionMajor].0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".97 lock (this.resolvedAssemblies)98 {99 if (this.resolvedAssemblies.TryGetValue(args.Name, out var assembly))100 {101 EqtTrace.Info("AssemblyResolver.OnResolve: {0}: Resolved from cache.", args.Name);102 return assembly;103 }104 AssemblyName requestedName = null;105 try106 {107 // Can throw ArgumentException, FileLoadException if arg is empty/wrong format, etc. Should not return null.108 requestedName = new AssemblyName(args.Name);109 }110 catch (Exception ex)111 {112 if (EqtTrace.IsInfoEnabled)113 {114 EqtTrace.Info("AssemblyResolver.OnResolve: {0}: Failed to create assemblyName. Reason:{1} ", args.Name, ex);115 }116 this.resolvedAssemblies[args.Name] = null;117 return null;118 }119 Debug.Assert(requestedName != null && !string.IsNullOrEmpty(requestedName.Name), "AssemblyResolver.OnResolve: requested is null or name is empty!");120 foreach (var dir in this.searchDirectories)121 {122 if (string.IsNullOrEmpty(dir))123 {124 continue;125 }126 EqtTrace.Info("AssemblyResolver.OnResolve: {0}: Searching in: '{1}'.", args.Name, dir);127 foreach (var extension in SupportedFileExtensions)128 {129 var assemblyPath = Path.Combine(dir, requestedName.Name + extension);130 try131 {132 if (!File.Exists(assemblyPath))133 {134 EqtTrace.Info("AssemblyResolver.OnResolve: {0}: Assembly path does not exist: '{1}', returning.", args.Name, assemblyPath);135 continue;136 }137 AssemblyName foundName = this.platformAssemblyLoadContext.GetAssemblyNameFromPath(assemblyPath);138 if (!this.RequestedAssemblyNameMatchesFound(requestedName, foundName))139 {140 EqtTrace.Info("AssemblyResolver.OnResolve: {0}: File exists but version/public key is wrong. Try next extension.", args.Name);141 continue; // File exists but version/public key is wrong. Try next extension.142 }143 EqtTrace.Info("AssemblyResolver.OnResolve: {0}: Loading assembly '{1}'.", args.Name, assemblyPath);144 145 assembly = this.platformAssemblyLoadContext.LoadAssemblyFromPath(assemblyPath);146 this.resolvedAssemblies[args.Name] = assembly;147 EqtTrace.Info("AssemblyResolver.OnResolve: Resolved assembly: {0}, from path: {1}", args.Name, assemblyPath);148 return assembly;149 }150 catch (FileLoadException ex)151 {152 EqtTrace.Error("AssemblyResolver.OnResolve: {0}: Failed to load assembly. Reason:{1} ", args.Name, ex);153 // Re-throw FileLoadException, because this exception means that the assembly154 // was found, but could not be loaded. This will allow us to report a more155 // specific error message to the user for things like access denied.156 throw;157 }158 catch (Exception ex)159 {160 // For all other exceptions, try the next extension.161 EqtTrace.Info("AssemblyResolver.OnResolve: {0}: Failed to load assembly. Reason:{1} ", args.Name, ex);162 }163 }164 }165 if (EqtTrace.IsInfoEnabled)166 {167 EqtTrace.Info("AssemblyResolver.OnResolve: {0}: Failed to load assembly.", args.Name);168 }169 this.resolvedAssemblies[args.Name] = null;170 return null;171 }172 }173 /// <summary>174 /// Verifies that found assembly name matches requested to avoid security issues.175 /// Looks only at PublicKeyToken and Version, empty matches anything.176 /// VSWhidbey 415774.177 /// </summary>178 /// <returns>179 /// The <see cref="bool"/>.180 /// </returns>181 private bool RequestedAssemblyNameMatchesFound(AssemblyName requestedName, AssemblyName foundName)182 {183 Debug.Assert(requestedName != null);184 Debug.Assert(foundName != null);185 var requestedPublicKey = requestedName.GetPublicKeyToken();186 if (requestedPublicKey != null)187 {188 var foundPublicKey = foundName.GetPublicKeyToken();189 if (foundPublicKey == null)190 {191 return false;192 }193 for (var index = 0; index < requestedPublicKey.Length; ++index)194 {195 if (requestedPublicKey[index] != foundPublicKey[index])196 {197 return false;198 }199 }200 }201 if (requestedName.Version != null)202 {203 return requestedName.Version.Equals(foundName.Version);204 }205 return true;206 }207 ~AssemblyResolver()208 {209 this.Dispose(false);210 }211 public void Dispose()212 {213 this.Dispose(true);214 // Use SupressFinalize in case a subclass215 // of this type implements a finalizer.216 GC.SuppressFinalize(this);217 }218 [System.Security.SecurityCritical]219 protected virtual void Dispose(bool disposing)220 {221 if (!this.isDisposed)222 {223 if (disposing)224 {225 this.platformAssemblyResolver.AssemblyResolve -= this.OnResolve;226 this.platformAssemblyResolver.Dispose();227 }228 this.isDisposed = true;229 }230 }231 }232}...

Full Screen

Full Screen

OnResolve

Using AI Code Generation

copy

Full Screen

1using Microsoft.VisualStudio.TestPlatform.Common.Utilities;2using System;3using System.Collections.Generic;4using System.Linq;5using System.Text;6using System.Threading.Tasks;7{8{9static void Main(string[] args)10{11Console.WriteLine("Hello World!");12AssemblyResolver resolver = new AssemblyResolver();13resolver.OnResolve(null, null);14}15}16}17using Microsoft.VisualStudio.TestPlatform.Common.Utilities;18using System;19using System.Collections.Generic;20using System.Linq;21using System.Text;22using System.Threading.Tasks;23{24{25static void Main(string[] args)26{27Console.WriteLine("Hello World!");28AssemblyResolver resolver = new AssemblyResolver();29resolver.OnResolve(null, null);30}31}32}33using Microsoft.VisualStudio.TestPlatform.Common.Utilities;34using System;35using System.Collections.Generic;36using System.Linq;37using System.Text;38using System.Threading.Tasks;39{40{41static void Main(string[] args)42{43Console.WriteLine("Hello World!");44AssemblyResolver resolver = new AssemblyResolver();45resolver.OnResolve(null, null);46}47}48}49using Microsoft.VisualStudio.TestPlatform.Common.Utilities;50using System;51using System.Collections.Generic;52using System.Linq;53using System.Text;54using System.Threading.Tasks;55{56{57static void Main(string[] args)58{59Console.WriteLine("Hello World!");60AssemblyResolver resolver = new AssemblyResolver();61resolver.OnResolve(null, null);62}63}64}65using Microsoft.VisualStudio.TestPlatform.Common.Utilities;66using System;67using System.Collections.Generic;68using System.Linq;69using System.Text;70using System.Threading.Tasks;71{72{73static void Main(string[] args)74{75Console.WriteLine("Hello World!");76AssemblyResolver resolver = new AssemblyResolver();77resolver.OnResolve(null, null);78}79}80}81using Microsoft.VisualStudio.TestPlatform.Common.Utilities;82using System;83using System.Collections.Generic;

Full Screen

Full Screen

OnResolve

Using AI Code Generation

copy

Full Screen

1using System;2using System.Reflection;3using System.IO;4using Microsoft.VisualStudio.TestPlatform.Common.Utilities;5{6 {7 static void Main(string[] args)8 {9 Console.WriteLine("Hello World!");10 AssemblyResolver resolver = new AssemblyResolver();11 resolver.OnResolve += Resolver_OnResolve;12 Assembly assembly = Assembly.LoadFile(@"D:\Test\1.dll");13 Console.WriteLine(assembly.FullName);14 Console.ReadLine();15 }16 private static Assembly Resolver_OnResolve(object sender, ResolveEventArgs args)17 {18 Console.WriteLine("Resolving assembly: " + args.Name);19 return null;20 }21 }22}23using System;24using System.Reflection;25using System.IO;26using Microsoft.VisualStudio.TestPlatform.Common.Utilities;27{28 {29 static void Main(string[] args)30 {31 Console.WriteLine("Hello World!");32 AssemblyResolver resolver = new AssemblyResolver();33 resolver.OnResolve += Resolver_OnResolve;34 Assembly assembly = Assembly.LoadFile(@"D:\Test\1.dll");35 Console.WriteLine(assembly.FullName);36 Console.ReadLine();37 }38 private static Assembly Resolver_OnResolve(object sender, ResolveEventArgs args)39 {40 Console.WriteLine("Resolving assembly: " + args.Name);41 return null;42 }43 }44}45using System;46using System.Reflection;47using System.IO;48using Microsoft.VisualStudio.TestPlatform.Common.Utilities;49{50 {51 static void Main(string[] args)52 {53 Console.WriteLine("Hello World!");54 AssemblyResolver resolver = new AssemblyResolver();55 resolver.OnResolve += Resolver_OnResolve;56 Assembly assembly = Assembly.LoadFile(@"D:\Test\1.dll");57 Console.WriteLine(assembly.FullName);58 Console.ReadLine();59 }60 private static Assembly Resolver_OnResolve(object sender, ResolveEventArgs args)61 {62 Console.WriteLine("Resolving assembly: " + args.Name);63 return null;64 }65 }66}67using System;68using System.Reflection;69using System.IO;70using Microsoft.VisualStudio.TestPlatform.Common.Utilities;71{

Full Screen

Full Screen

OnResolve

Using AI Code Generation

copy

Full Screen

1using Microsoft.VisualStudio.TestPlatform.Common.Utilities;2using System;3using System.Reflection;4{5 {6 static void Main(string[] args)7 {8 AssemblyResolver resolver = new AssemblyResolver();9 resolver.OnResolve += Resolver_OnResolve;10 Assembly assembly = Assembly.Load("Microsoft.VisualStudio.TestPlatform.TestFramework");11 }12 private static Assembly Resolver_OnResolve(object sender, ResolveEventArgs args)13 {14 Assembly assembly = Assembly.Load("Microsoft.VisualStudio.TestPlatform.TestFramework");15 return assembly;16 }17 }18}

Full Screen

Full Screen

OnResolve

Using AI Code Generation

copy

Full Screen

1using System;2using System.Reflection;3using Microsoft.VisualStudio.TestPlatform.Common.Utilities;4using NUnit.Framework;5{6 {7 public void TestMethod1()8 {9 var assemblyResolver = new AssemblyResolver();10 assemblyResolver.OnResolve += AssemblyResolver_OnResolve;11 var assembly = Assembly.LoadFrom(@"C:\Users\user\Desktop\2.dll");12 var type = assembly.GetType("ClassLibrary1.Class1");13 var method = type.GetMethod("TestMethod1");14 var obj = Activator.CreateInstance(type);15 var result = method.Invoke(obj, null);16 Console.WriteLine(result);17 }18 private Assembly AssemblyResolver_OnResolve(object sender, ResolveEventArgs args)19 {20 return Assembly.LoadFrom(@"C:\Users\user\Desktop\1.dll");21 }22 }23}24{25 {26 public int TestMethod1()27 {28 return 1;29 }30 }31}32{33 {34 public int TestMethod2()35 {36 return 2;37 }38 }39}40using System;41using System.Reflection;42using Microsoft.VisualStudio.TestPlatform.Common.Utilities;43using NUnit.Framework;44{45 {46 public void TestMethod1()47 {48 var assemblyResolver = new AssemblyResolver();49 assemblyResolver.OnResolve += AssemblyResolver_OnResolve;50 var assembly = Assembly.LoadFrom(@"C:\Users\user\Desktop\2.dll");51 var type = assembly.GetType("ClassLibrary1.Class1");52 var method = type.GetMethod("TestMethod1");53 var obj = Activator.CreateInstance(type);54 var result = method.Invoke(obj, null);55 Console.WriteLine(result);56 }57 private Assembly AssemblyResolver_OnResolve(object sender, ResolveEventArgs args)58 {59 return Assembly.LoadFrom(@"C:\Users\user\Desktop\1.dll");60 }61 }62}63{64 {65 public int TestMethod1()66 {67 return 1;68 }69 }70}

Full Screen

Full Screen

OnResolve

Using AI Code Generation

copy

Full Screen

1using System;2using Microsoft.VisualStudio.TestPlatform.Common.Utilities;3{4 {5 static void Main(string[] args)6 {7 AssemblyResolver.OnResolve += AssemblyResolver_OnResolve;8 var assembly = System.Reflection.Assembly.LoadFile(@"C:\TestProject1\bin\Debug9etstandard2.0\TestProject2.dll");10 var type = assembly.GetType("TestProject2.Class1");11 var obj = Activator.CreateInstance(type);12 type.GetMethod("Method1").Invoke(obj, null);13 }14 private static System.Reflection.Assembly AssemblyResolver_OnResolve(object sender, System.Reflection.AssemblyName assemblyName)15 {16 return System.Reflection.Assembly.LoadFile(@"C:\TestProject1\bin\Debug17etstandard2.0\TestProject2.dll");18 }19 }20}21using System;22using Microsoft.VisualStudio.TestPlatform.Common.Utilities;23{24 {25 static void Main(string[] args)26 {27 AssemblyResolver.OnResolve += AssemblyResolver_OnResolve;28 var assembly = System.Reflection.Assembly.LoadFile(@"C:\TestProject1\bin\Debug29etstandard2.0\TestProject2.dll");30 var type = assembly.GetType("TestProject2.Class1");31 var obj = Activator.CreateInstance(type);32 type.GetMethod("Method1").Invoke(obj, null);33 }34 private static System.Reflection.Assembly AssemblyResolver_OnResolve(object sender, System.Reflection.AssemblyName assemblyName)35 {36 return System.Reflection.Assembly.LoadFile(@"C:\TestProject1\bin\Debug37etstandard2.0\TestProject2.dll");38 }39 }40}41using System;42using Microsoft.VisualStudio.TestPlatform.Common.Utilities;43{44 {45 static void Main(string[] args)46 {47 AssemblyResolver.OnResolve += AssemblyResolver_OnResolve;

Full Screen

Full Screen

OnResolve

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Microsoft.VisualStudio.TestPlatform.Common.Utilities;7{8 {9 static void Main(string[] args)10 {11 AssemblyResolver.OnResolve += AssemblyResolver_OnResolve;12 Console.WriteLine("Hello World");13 Console.ReadLine();14 }15 private static System.Reflection.Assembly AssemblyResolver_OnResolve(System.Reflection.AssemblyName assemblyName)16 {17 Console.WriteLine("AssemblyResolver_OnResolve");18 return null;19 }20 }21}22using System;23using System.Collections.Generic;24using System.Linq;25using System.Text;26using System.Threading.Tasks;27using Microsoft.VisualStudio.TestPlatform.Common.Utilities;28{29 {30 static void Main(string[] args)31 {32 AssemblyResolver.OnResolve += AssemblyResolver_OnResolve;33 Console.WriteLine("Hello World");34 Console.ReadLine();35 }36 private static System.Reflection.Assembly AssemblyResolver_OnResolve(System.Reflection.AssemblyName assemblyName)37 {38 Console.WriteLine("AssemblyResolver_OnResolve");39 return null;40 }41 }42}43using System;44using System.Collections.Generic;45using System.Linq;46using System.Text;47using System.Threading.Tasks;48using Microsoft.VisualStudio.TestPlatform.Common.Utilities;49{50 {51 static void Main(string[] args)52 {53 AssemblyResolver.OnResolve += AssemblyResolver_OnResolve;54 Console.WriteLine("Hello World");55 Console.ReadLine();56 }57 private static System.Reflection.Assembly AssemblyResolver_OnResolve(System.Reflection.AssemblyName assemblyName)58 {59 Console.WriteLine("AssemblyResolver_OnResolve");60 return null;61 }62 }63}64using System;65using System.Collections.Generic;66using System.Linq;67using System.Text;68using System.Threading.Tasks;69using Microsoft.VisualStudio.TestPlatform.Common.Utilities;70{71 {72 static void Main(string[] args)73 {74 AssemblyResolver.OnResolve += AssemblyResolver_OnResolve;

Full Screen

Full Screen

OnResolve

Using AI Code Generation

copy

Full Screen

1using System;2using System.IO;3using System.Reflection;4{5 {6 private AssemblyResolver()7 {8 AppDomain.CurrentDomain.AssemblyResolve += OnResolve;9 }10 public static AssemblyResolver Instance { get; } = new AssemblyResolver();11 private static Assembly OnResolve(object sender, ResolveEventArgs args)12 {13 var assemblyName = new AssemblyName(args.Name).Name;14 var assemblyPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), $"{assemblyName}.dll");15 return File.Exists(assemblyPath) ? Assembly.LoadFrom(assemblyPath) : null;16 }17 }18}19using System;20using System.IO;21using System.Reflection;22{23 {24 private AssemblyResolver()25 {26 AppDomain.CurrentDomain.AssemblyResolve += OnResolve;27 }28 public static AssemblyResolver Instance { get; } = new AssemblyResolver();29 private static Assembly OnResolve(object sender, ResolveEventArgs args)30 {31 var assemblyName = new AssemblyName(args.Name).Name;32 var assemblyPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), $"{assemblyName}.dll");33 return File.Exists(assemblyPath) ? Assembly.LoadFrom(assemblyPath) : null;34 }35 }36}37using System;38using System.IO;39using System.Reflection;40{41 {42 private AssemblyResolver()43 {44 AppDomain.CurrentDomain.AssemblyResolve += OnResolve;45 }46 public static AssemblyResolver Instance { get; } = new AssemblyResolver();47 private static Assembly OnResolve(object sender, ResolveEventArgs args)

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Vstest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful