472,364 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,364 software developers and data experts.

dynamically adding assembly reference to executing assembly

Hi,
I have been searching around to try adding reference assembly to
another assembly during runtime, programatically. Is this possible
in .Net?

The reason for this is because i am having trouble using a library
that creates an instance of a Type that i specified, and it failed the
locate the Type during runtime, if i do not reference it during
compile time.

Thanks,
chage
Jun 27 '08 #1
7 9734
Well, you can't add a reference - but you can just use Assembly.Load /
Assembly.LoadFrom to force loading a certain assembly. Alternatively,
you can handle AppDomain.AssemblyResolve or AppDomain.TypeResolve to
do a few things...

But first I'd try to figure out why resolution is failing... if the
dll isn't in the GAC, it must be part of the overall application "bin"
output.

Marc
Jun 27 '08 #2
chage wrote:
Hi,
I have been searching around to try adding reference assembly to
another assembly during runtime, programatically. Is this possible
in .Net?

The reason for this is because i am having trouble using a library
that creates an instance of a Type that i specified, and it failed the
locate the Type during runtime, if i do not reference it during
compile time.

Thanks,
chage
You can load an assembly at runtime either by specifically calling Load
methods on the Assembly class, or you can just reference a type in it
before the library needs it.

Type t = typeof(SomeTypeInThatAssembly);

should work.

--
Lasse Vågsæther Karlsen
mailto:la***@vkarlsen.no
http://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3
Jun 27 '08 #3
On May 19, 4:26*am, Marc Gravell <marc.grav...@gmail.comwrote:
Well, you can't add a reference - but you can just use Assembly.Load /
Assembly.LoadFrom to force loading a certain assembly. Alternatively,
you can handle AppDomain.AssemblyResolve or AppDomain.TypeResolve to
do a few things...

But first I'd try to figure out why resolution is failing... if the
dll isn't in the GAC, it must be part of the overall application "bin"
output.

Marc
Hi Marc,
My use case here will be .Net remoting, where the server is unable to
load assembly that wasnt reference during compile time.

the dll isnt in GAC, it is resided somewhere else, and i use
Assembly.Load to load it into the executing app domain. The assembly
loaded without issue.
Assembly someDllAsm = System.Reflection.Assembly.LoadFile(@"C:\SomePath
\SomeDll.dll");
AppDomain.CurrentDomain.Load(someDllAsm.GetName()) ;

But when I have a client that call a method to the server using
remoting, exception was thrown with below stack:
System.IO.FileNotFoundException: Could not load file or assembly
'SomeDll, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or
one of its dependencies. The system cannot find the file specified.
File name: 'SomeDll, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null'

Server stack trace:
at System.Reflection.Assembly._nLoad(AssemblyName fileName, String
codeBase, Evidence assemblySecurity, Assembly locationHint,
StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean
forIntrospection)
at System.Reflection.Assembly.nLoad(AssemblyName fileName, String
codeBase, Evidence assemblySecurity, Assembly locationHint,
StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean
forIntrospection)
at System.Reflection.Assembly.InternalLoad(AssemblyNa me
assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark,
Boolean forIntrospection)
at System.Reflection.Assembly.InternalLoad(String assemblyString,
Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean
forIntrospection)
at System.Reflection.Assembly.Load(String assemblyString)
at System.UnitySerializationHolder.GetRealObject(Stre amingContext
context)
at
System.Runtime.Serialization.ObjectManager.Resolve ObjectReference(ObjectHolder
holder)
at System.Runtime.Serialization.ObjectManager.DoFixup s()
at
System.Runtime.Serialization.Formatters.Binary.Obj ectReader.Deserialize(HeaderHandler
handler, __BinaryParser serParser, Boolean fCheck, Boolean
isCrossAppDomain, IMethodCallMessage methodCallMessage)
at
System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter.Deserialize(Stream
serializationStream, HeaderHandler handler, Boolean fCheck, Boolean
isCrossAppDomain, IMethodCallMessage methodCallMessage)
at
System.Runtime.Remoting.Channels.CoreChannel.Deser ializeBinaryRequestMessage(String
objectUri, Stream inputStream, Boolean bStrictBinding, TypeFilterLevel
securityLevel)
at
System.Runtime.Remoting.Channels.BinaryServerForma tterSink.ProcessMessage(IServerChannelSinkStack
sinkStack, IMessage requestMsg, ITransportHeaders requestHeaders,
Stream requestStream, IMessage& responseMsg, ITransportHeaders&
responseHeaders, Stream& responseStream)

Exception rethrown at [0]:
at
System.Runtime.Remoting.Proxies.RealProxy.HandleRe turnMessage(IMessage
reqMsg, IMessage retMsg)
at
System.Runtime.Remoting.Proxies.RealProxy.PrivateI nvoke(MessageData&
msgData, Int32 type)
I am not too sure how AppDomain.TypeResolve works, I will try to look
into that after this. If you have any other thoughts, do let me know.
Thanks!

-chage
Jun 27 '08 #4
On May 19, 4:22*am, Lasse Vågsæther Karlsen <la...@vkarlsen.nowrote:
chage wrote:
Hi,
I have been searching around to try adding reference assembly to
another assembly during runtime, programatically. Is this possible
in .Net?
The reason for this is because i am having trouble using a library
that creates an instance of a Type that i specified, and it failed the
locate the Type during runtime, if i do not reference it during
compile time.
Thanks,
chage

You can load an assembly at runtime either by specifically calling Load
methods on the Assembly class, or you can just reference a type in it
before the library needs it.

Type t = typeof(SomeTypeInThatAssembly);

should work.

--
Lasse Vågsæther Karlsen
mailto:la...@vkarlsen.nohttp://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3
Hi Lasse,
thanks for your reply. Yes, that will work if I have control over how
the type can be locate (I use this method in my client side code,
please refer to my previous post about the use case).
But the problem occurs when i use remoting, where the Type being
serialized and deserialized by .Net framework, which i do not have
control over how they locate the Type, other than i have the dll
referenced during compile time.

I might be missing something, as I have just started to make use of
Remoting. Any other suggestions will be appreciated. Thanks.

-chage
Jun 27 '08 #5
Hi Chage,

Just guessing here (nowhere near being an expert on this).

First thing I notice is that you are using the "LoadFile()" method passing
the "C:\SomePath\SomeDll.dll" path to get a reference to the assembly and
*then* you use the "Load()" method of the AppDomain to actually load the
assembly into the AppDomain. The key point here of course, is that the
assembly that you are searching for is not on the application base path but
under "C:\SomePath" path which I assume is not the application base path.

I am quite surprised that this actually loads the assembly correctly because
I don't believe that the AppDomain.Load() method searches the "C:\SomePath"
path looking for the assembly. My guess would be that you have a copy of the
"SomeDll.dll" file on the application base path and that is the reason the
assembly will load there but maybe that is not the case on the server and
that why is fails???

I google "Fussion Log" and came up with the following link:

http://blogs.msdn.com/suzcook/archiv.../29/57120.aspx

I just took a quick glance at this so I am not sure it will help you but it
looks like something that would help you solve your problem.

Sorry I can't do any better!!! Good luck solving your problem and if you do
find an answer please post back so we can all see the what going on!!

Cheers.
Jun 27 '08 #6
Hi Marc,
you are right, i got it work by handling the AssemblyResolve myself to
load the dll i wanted. Now i can really work on dynamically loading
the external dll without having to rebuild my server and client code.
Thanks!

To Rene,
thanks for your offering, appreciate it. I did read thru the link you
provides, it is a good one. Regarding your comment on the dll path, I
got it work because when we do AppDomain.Load, we specify the full
path to the assembly we intended to load, so it should be find.

with this, i got my case close for now, thanks to all who offered
their help.

-chage
On May 19, 8:10*am, chage <eric...@gmail.comwrote:
On May 19, 4:26*am, Marc Gravell <marc.grav...@gmail.comwrote:
Well, you can't add a reference - but you can just use Assembly.Load /
Assembly.LoadFrom to force loading a certain assembly. Alternatively,
you can handle AppDomain.AssemblyResolve or AppDomain.TypeResolve to
do a few things...
But first I'd try to figure out why resolution is failing... if the
dll isn't in the GAC, it must be part of the overall application "bin"
output.
Marc

Hi Marc,
My use case here will be .Net remoting, where the server is unable to
load assembly that wasnt reference during compile time.

the dll isnt in GAC, it is resided somewhere else, and i use
Assembly.Load to load it into the executing app domain. The assembly
loaded without issue.
Assembly someDllAsm = System.Reflection.Assembly.LoadFile(@"C:\SomePath
\SomeDll.dll");
AppDomain.CurrentDomain.Load(someDllAsm.GetName()) ;

But when I have a client that call a method to the server using
remoting, exception was thrown with below stack:
System.IO.FileNotFoundException: Could not load file or assembly
'SomeDll, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or
one of its dependencies. The system cannot find the file specified.
File name: 'SomeDll, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null'

Server stack trace:
* *at System.Reflection.Assembly._nLoad(AssemblyName fileName, String
codeBase, Evidence assemblySecurity, Assembly locationHint,
StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean
forIntrospection)
* *at System.Reflection.Assembly.nLoad(AssemblyName fileName, String
codeBase, Evidence assemblySecurity, Assembly locationHint,
StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean
forIntrospection)
* *at System.Reflection.Assembly.InternalLoad(AssemblyNa me
assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark,
Boolean forIntrospection)
* *at System.Reflection.Assembly.InternalLoad(String assemblyString,
Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean
forIntrospection)
* *at System.Reflection.Assembly.Load(String assemblyString)
* *at System.UnitySerializationHolder.GetRealObject(Stre amingContext
context)
* *at
System.Runtime.Serialization.ObjectManager.Resolve ObjectReference(ObjectHol*der
holder)
* *at System.Runtime.Serialization.ObjectManager.DoFixup s()
* *at
System.Runtime.Serialization.Formatters.Binary.Obj ectReader.Deserialize(Hea*derHandler
handler, __BinaryParser serParser, Boolean fCheck, Boolean
isCrossAppDomain, IMethodCallMessage methodCallMessage)
* *at
System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter.Deserialize(*Stream
serializationStream, HeaderHandler handler, Boolean fCheck, Boolean
isCrossAppDomain, IMethodCallMessage methodCallMessage)
* *at
System.Runtime.Remoting.Channels.CoreChannel.Deser ializeBinaryRequestMessag*e(String
objectUri, Stream inputStream, Boolean bStrictBinding, TypeFilterLevel
securityLevel)
* *at
System.Runtime.Remoting.Channels.BinaryServerForma tterSink.ProcessMessage(I*ServerChannelSinkStack
sinkStack, IMessage requestMsg, ITransportHeaders requestHeaders,
Stream requestStream, IMessage& responseMsg, ITransportHeaders&
responseHeaders, Stream& responseStream)

Exception rethrown at [0]:
* *at
System.Runtime.Remoting.Proxies.RealProxy.HandleRe turnMessage(IMessage
reqMsg, IMessage retMsg)
* *at
System.Runtime.Remoting.Proxies.RealProxy.PrivateI nvoke(MessageData&
msgData, Int32 type)

I am not too sure how AppDomain.TypeResolve works, I will try to look
into that after this. If you have any other thoughts, do let me know.
Thanks!

-chage
Jun 27 '08 #7
Good to hear it is sorted! Actually, I have to admit that this event
is one of those things I've heard about but never used... glad it
worked ;-p

Marc
Jun 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Thomas W. Brown | last post by:
If I am using the CSharpCodeProvider to dynamically compile an in-memory assembly from some C# source, do I need to worry about signing this assembly if I'm doing the compilation, instantiation,...
7
by: Wysiwyg | last post by:
Is there any way to add an embedded resource to a project without copying it to the project's directory? I have shared resources and don't want each project using the images, xml files, etc. to...
15
by: Amit D.Shinde | last post by:
I am adding a new picturebox control at runtime on the form How can i create click event handler for this control Amit Shinde
7
by: pmclinn | last post by:
I was wondering if it is possible to dynamically create a structure. Something like this: public sub main sql = "Select Col1, Col2 from Table a" dim al as new arraylist al =...
2
by: IcEzZz | last post by:
Hi, I have a problem here, but I do not know how to go about it. I'm doing a project on a website. I'm the middleman who will retrieve informations from many different suppliers. I would be...
3
by: _DS | last post by:
The two obvious methods for ref'ing assemblies are: Add a reference and 'Browse' for the actual DLL OR Add existing project to the solution, then add a ref to 'Project'. 1: I'd like to...
2
by: Smithers | last post by:
Using 3.5, I am stuck in attempting to: 1. Dynamically load an assembly 2. Instantiate a class from that assembly (the client code is in a different namespace than the namespace of the...
9
by: =?Utf-8?B?VGhvbWFzIFcuIEJyb3du?= | last post by:
"parez" wrote: Assembly.LoadWithPartialName does indeed work!! Now, why is this incredibly useful routine deprecated and we are instructed to use Assembly.Load instead which does not provide...
0
by: =?Utf-8?B?Y211cmFsaQ==?= | last post by:
I am serializing/deserializing a class (Class1) using the XmlSerializer object in the System.Xml.Serialization namespace in .Net 2.0. Class1 has some strings and ints and two lists of other simple...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.