1

I'm trying to load an assembly at runtime. For that I'm using the following line:

System.Reflection.Assembly.Load(file.FullName);

file is a FileInfo object, so file.FullName returns the full path to the file on the filesystem, e.g.: c:\mywebsite\dlls\myassembly.dll

When I try to execute the line above I get the following error:

Could not load file or assembly 'c:\mywebsite\dlls\myassembly.dll' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)

I'm sure this isn't a dependency problem, and I don't know what's an "invalid codebase"

Note that if I put the assembly directly on the BIN folder of the site everything works correctly, I just can't make load dynamically.

Cœur
  • 37,241
  • 25
  • 195
  • 267
AlexCode
  • 4,055
  • 4
  • 33
  • 46

5 Answers5

3

To supplement Schroedingers Cat's answer, you might want to look at Assembly.LoadFile if you only have the file path. But you might get security issues when running from ASP.NET which may require configuring permissions on the file system, to allow your website to access the file.

Jason Evans
  • 28,906
  • 14
  • 90
  • 154
  • This question is answered, but the site still says it can't find a type that's inside the assembly that was dynamically loaded. How can I load this assembly to be used as if it is on the Bin folder? – AlexCode Jul 26 '11 at 14:21
  • It blows right on the first line of the ASCX: <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Entity1Item.ascx.cs" Inherits="App.Modules.ModuleA.Controls.Entity1Item" %> Could not load type 'App.Modules.ModuleA.Controls.Entity1Item'. – AlexCode Jul 26 '11 at 14:38
  • It loads the ascx markup but can't find the type that's defined on the dll. If I add the dll to the bin of the website it works correctly... – AlexCode Jul 26 '11 at 14:40
1

You need to use the "long form of the assembly name" not the file path to the code: see this link

Schroedingers Cat
  • 3,099
  • 1
  • 15
  • 33
1

This is a security/permissions issue - you can't load assemblies from outside bin without the proper permissions (depending on the account used to run)...

Yahia
  • 69,653
  • 9
  • 115
  • 144
  • I thought about this and to get this issue out of the way I gave my admin account to the app pool that this website is running on. So this is not the problem. – AlexCode Jul 26 '11 at 14:12
0

This is probably related to permissions, generally an asp.net website is not going to have access (particularly execute access) to files outside it's root directory.

CodingGorilla
  • 19,612
  • 4
  • 45
  • 65
0

That method expects the assembly name. What you want is Assembly.LoadFrom, although you will have to be carefull with permissions as you're calling this from an app.

See here for more info: How to call a Managed DLL File in C#?

Community
  • 1
  • 1
George Duckett
  • 31,770
  • 9
  • 95
  • 162