DLL does not load Dynamically

R

RSS

Hi everyone,
I have an app that uses some DLL's that are shared across couple applications. There are reasons existing that prevent me from putting them in to the GAC.

Each of these app's at some point in time loads a DLL dynamically in this fashion:

System.Reflection.Assembly assembly = null;

assembly = System.Reflection.Assembly.LoadFrom(_assemblyPath);

return assembly.CreateInstance(_typeName, false, System.Reflection.BindingFlags.Default, null, args, null, null);



_assemblyPath - DB driven that has a value for path. Now, if I have a path like this "C:\Mypath\My.dll" everything works fine. If I only have "My.dll" i get an error message saying the My.dll or one of its dependencies cannot be found. The exe and My.dll reside in the same folder and by default it should load when _assemblyPath = My.dll.





Any idea why its not working.

thanks a lot in advance.
 
A

AMDRIT

Try ".\\" + _assemblyPath;
"RSS" <q> wrote in message Hi everyone,
I have an app that uses some DLL's that are shared across couple applications. There are reasons existing that prevent me from putting them in to the GAC.

Each of these app's at some point in time loads a DLL dynamically in this fashion:

System.Reflection.Assembly assembly = null;

assembly = System.Reflection.Assembly.LoadFrom(_assemblyPath);

return assembly.CreateInstance(_typeName, false, System.Reflection.BindingFlags.Default, null, args, null, null);



_assemblyPath - DB driven that has a value for path. Now, if I have a path like this "C:\Mypath\My.dll" everything works fine. If I only have "My.dll" i get an error message saying the My.dll or one of its dependencies cannot be found. The exe and My.dll reside in the same folder and by default it should load when _assemblyPath = My.dll.





Any idea why its not working.

thanks a lot in advance.
 
B

Bruce Wood

Just a stab in the dark... when you start the application, what is the
"Start in" current directory?

If you're running from a CMD command line, do you switch directories to
C:\Mypath and then run the app, or run it from wherever the current
directory is?

If you're running it from a desktop shortcut, what is your "Start in"
path set to?

I would assume that Assembly.LoadFrom would attempt to load from the
current directory if no path is specified, which may not be the same as
the directory where the executing assembly resides.

If you always want the latter behaviour, have you tried getting the
location of the running assembly using

string exePath = Assembly.GetExecutingAssembly().Location;

and then building the DLL name like this:

string dllPath = Path.Combine(Path.Combine(Path.GetPathRoot(exePath),
Path.GetDirectoryName(exePath)), "My.dll");

?
 
W

Willy Denoyette [MVP]

Please post the exact exception message.

Willy.

"RSS" <q> wrote in message Hi everyone,
I have an app that uses some DLL's that are shared across couple applications. There are reasons existing that prevent me from putting them in to the GAC.

Each of these app's at some point in time loads a DLL dynamically in this fashion:

System.Reflection.Assembly assembly = null;

assembly = System.Reflection.Assembly.LoadFrom(_assemblyPath);

return assembly.CreateInstance(_typeName, false, System.Reflection.BindingFlags.Default, null, args, null, null);



_assemblyPath - DB driven that has a value for path. Now, if I have a path like this "C:\Mypath\My.dll" everything works fine. If I only have "My.dll" i get an error message saying the My.dll or one of its dependencies cannot be found. The exe and My.dll reside in the same folder and by default it should load when _assemblyPath = My.dll.





Any idea why its not working.

thanks a lot in advance.
 
R

RSS

An unexpected exception was handled while processing job requests.

System.IO.FileNotFoundException: File or assembly name My.Core.dll, or one of its dependencies, was not found.

File name: "My.Core.dll"

at System.Reflection.Assembly.nLoad(AssemblyName fileName, String codeBase, Boolean isStringized, Evidence assemblySecurity, Boolean throwOnFileNotFound, Assembly locationHint, StackCrawlMark& stackMark)

at System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Boolean stringized, Evidence assemblySecurity, StackCrawlMark& stackMark)

at System.Reflection.Assembly.LoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm)

at System.Activator.CreateInstanceFrom(String assemblyFile, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo)

at My.Core.Typing.TypeDef.Instance(IDataReader dr)

at My.Core.Typing.Type.CacheType(String typeName)

at My.Core.Typing.Type.GetById(String typeName, Int32 typeId)

at My.Core.Activities.ActivityType.GetById(Int32 id)

at My.Core.Activities.Activity.List(IDbConnection connection)

at My.Scheduling.SchedulingServer.Version2Processing()

at My.Scheduling.SchedulingServer.ProcessingLoop(Object stateInfo)

=== Pre-bind state information ===

LOG: Where-ref bind. Location = C:\WINDOWS\system32\My.Core.dll

LOG: Appbase = c:\program files\Mine\scheduleingsetup\

LOG: Initial PrivatePath = NULL

Calling assembly : (Unknown).

===

LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).

LOG: Attempting download of new URL file:///C:/WINDOWS/system32/My.Core.dll.



Please post the exact exception message.

Willy.

"RSS" <q> wrote in message Hi everyone,
I have an app that uses some DLL's that are shared across couple applications. There are reasons existing that prevent me from putting them in to the GAC.

Each of these app's at some point in time loads a DLL dynamically in this fashion:

System.Reflection.Assembly assembly = null;

assembly = System.Reflection.Assembly.LoadFrom(_assemblyPath);

return assembly.CreateInstance(_typeName, false, System.Reflection.BindingFlags.Default, null, args, null, null);



_assemblyPath - DB driven that has a value for path. Now, if I have a path like this "C:\Mypath\My.dll" everything works fine. If I only have "My.dll" i get an error message saying the My.dll or one of its dependencies cannot be found. The exe and My.dll reside in the same folder and by default it should load when _assemblyPath = My.dll.





Any idea why its not working.

thanks a lot in advance.
 
N

.neter

RSS said:
An unexpected exception was handled while processing job requests.
(...)

It is looking for the file in C:\windows\system32. Is your program a
windows service?
You might try the trick with Assembly.GetExecutingAssembly().Location
posted above.
 
R

RSS

Yes it is a windows service


.neter said:
(...)

It is looking for the file in C:\windows\system32. Is your program a
windows service?
You might try the trick with Assembly.GetExecutingAssembly().Location
posted above.
 
A

adi

hi,

the "best" solution (only my opinion)

1) keep the interfaces of your libs compatible => signatures of public
props+methods
(the best is only 1 version of your lib in gac + publisher policy)

2) deploy your libs in the GAC, but only with publisher policies!
this will redirect your "old" clients to the new versions of your libs

loading manualy your libs may not be good for the code maintenance of
your libs ...

adrian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top