简体   繁体   中英

My project references another project which references a 3rd party assembly. How do I set the assembly path for this 3rd party assembly?

My project A references project B. Project B holds a reference to a 3rd party assembly. Project A is located in another folder than project B and the 3rd party assembly (due to integration with another software). I know I could have the assemblies in the integration folder, but I would like to have them in my own folder where I have all the other assemblies together with an executable used to diagnose and setup the module.

I have subscribed to MyResolveEventHandler in project A in the AppDomain.CurrentDomain , and can successfully set the correct assembly path of project B. The problem occurs when project A calls a method in project B where the 3rd party assembly is used. Then it throws FileNotFoundException for that assembly, and it display the path of the integration folder rather than the folder I set for project B in MyResolveEventHandler .

I've tried AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(@"pathOf3rdPartyAssembly")) , it will tell me it successfully loaded the assembly in the Appdomain , but will still throw the same FileNotFoundException mentioned above.

How can I set the assembly path of the 3rd party assembly used within project B from project A?


EDIT: Sorry, the problem was caused by a well hidden bug by myself. I implemented some dll hijacking countermeasures that were throwing FileNotFoundException. Actually when the correct assembly path for project B is set, this is also valid for the 3rd party application referenced in project B. Sorry for wasting your time... I can assure you I wasted a lot of time before asking this question, when asking the question, and after asking the question. Now I gotta move on... :) Thanks for all your suggestions! I will upvote both of you...

You can setup a custom probing path for your 3rd party assembly. Please note that this approach won't work with ASP.NET applications (they don't support custom probing paths).

try to use this inside the resolveEvent handler

return System.Reflection.Assembly.LoadFrom(@"pathOf3rdPartyAssembly")

the pattern i usually use is

Private Sub MyApplication_Startup(sender As Object, e As ApplicationServices.StartupEventArgs) Handles Me.Startup
    AddHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf Me.OnAssemblyResolveHandler
End Sub

Private Function OnAssemblyResolveHandler(sender As Object, args As ResolveEventArgs) As System.Reflection.Assembly
    If args.Name.StartsWith("assemblyName,") Then Return System.Reflection.Assembly.LoadFrom(@"pathOf3rdPartyAssembly")
    Return Nothing
End Function

because the resolve event handler is expecting you to return the found assembly, or it will go on with the default searching pattern and then throw the exception, i suggest you to implement a stronger way to identify the assembly instead of using only the name like i do!

Ps this is vb.net, but can be easily translated to c#

Private System.Reflection.Assembly OnAssemblyResolveHandler(Object sender, ResolveEventArgs args)
{
    if (args.Name.StartsWith("assemblyName,")) { Return System.Reflection.Assembly.LoadFrom(@"pathOf3rdPartyAssembly"); }
    return null;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM