5

I am able to load the dll using this form -

System.Reflection.Assembly assembly =
System.Reflection.Assembly.LoadFile(@"C:\Users\amit.pandey\Documents\Visual Studio 2010\Projects\bin\Release\EUtility.dll");

However I need to load the dll from a shared network drive in the following manner -

System.Reflection.Assembly assembly =
System.Reflection.Assembly.LoadFile(@"\\falmumapp20\EUtility.dll");

I know it is caused because of trust problem. I have tried various code available but could not get it working. Can someone please help me with sample code for loading the dll from a network drive?

I want to do it in the code itself, without making change to any configuration file.

Amit Pandey
  • 1,436
  • 2
  • 24
  • 34
  • What .NET version are you on? What is the exact exception/error that you get? – Dirk Vollmar Nov 27 '12 at 14:19
  • am using .Net 4.0, the error I am getting is the same dll gets loaded from my local computer, but when I try to load it from a remote computer, it fails and says trusted domain problem. – Amit Pandey Nov 27 '12 at 14:21
  • Make sure you can access the share that you are storing the dll on. At work I always have to re-connect to the reference shares any time I shut a machine off because it loses the connection and will not allow use of the dlls. – wjhguitarman Nov 27 '12 at 14:23

3 Answers3

5

You need to tell the machine to trust that location.

You can do this with the caspol utility.

hometoast
  • 11,522
  • 5
  • 41
  • 58
  • 3
    IMPORTANT: In the .NET Framework version 4 and later versions, Caspol.exe does not affect CAS policy unless the element is set to true. For more information, see Security Changes in the .NET Framework 4. Please see http://msdn.microsoft.com/en-us/library/cb6t8dtz%28v=vs.100%29.aspx – Dirk Vollmar Nov 27 '12 at 14:43
  • Thank you for that disclaimer. (last time I had a trusted location issue was 2.0) – hometoast Nov 27 '12 at 14:51
  • thats right, and answer is acceptable, but what if I do not have to use the caspol utility and do it within the code itself. The issue is we have to deliver it to a client, and we would like the code to take care of loading, and no maintenance/modification on client side – Amit Pandey Nov 28 '12 at 07:04
  • 1
    I *think* it's something you'd have to handle in the installation of your app. – hometoast Nov 28 '12 at 11:53
  • 1
    What if I cannot change the code? Can I still somehow make the program trust the share? In my case, I am running a program from a VirtualBox host only network, so I would like Windows to trust the share fully. – Arne Sep 18 '17 at 10:47
4

Instead of loading the binary content you can use Assembly.UnsafeLoadFrom. See http://msdn.microsoft.com/en-us/library/system.reflection.assembly.unsafeloadfrom%28v=vs.110%29.aspx

This worked fine for me in all purposes.

David Gausmann
  • 1,570
  • 16
  • 20
3

Alternatively, loading the file from disk and calling Assembly.Load on the byte array seems to work (Framework 4):

        string Share = @"R:\Test\TestAssembly.dll";
        byte[] AssmBytes = File.ReadAllBytes(Share);
        Assembly a = Assembly.Load(AssmBytes);

        //Invoking a method from the loaded assembly
        var obj = a.CreateInstance("TestAssembly.Class1");
        obj.GetType().InvokeMember("HelloWorld", BindingFlags.InvokeMethod, null, obj, null);

Since MS blocks network loading in LoadFrom(), it would indicate that doing so is probably not best practices. However, this is similar to the process that is often used with embedding assemblies as resources in an EXE.

transistor1
  • 2,915
  • 26
  • 42