Loading Assembly Into New Domain

NETCoder

Member
Joined
Aug 9, 2007
Messages
16
Programming Experience
1-3
Hi,

I am developing an add-in inwhich I need to create a new domain to load an assembly and retrieve vb component information. Though I could load the assembly to the current domain using system.reflection.assembly, there is no way I could unload it again. Because system.reflection.assembly does not have unload method. Following is the code which is generating security permission error

Dim pAppset As AppDomainSetup
pAppset = New AppDomainSetup

pappset.applicationbase = "C:\TESTING1\bin" ' Location of the assembly I am trying to load

pappset.ApplicationName = "new domain"
Dim adevidence As System.Security.Policy.Evidence = AppDomain.CurrentDomain.Evidence

Dim pmyEv As System.Security.Policy.Evidence
pmyEv = New System.Security.Policy.Evidence(adevidence)

pAppDom = AppDomain.CreateDomain("secondappdomain", pmyEv, pAppset)

Dim pAssmName As New AssemblyName
pAssmName = AssemblyName.GetAssemblyName("C:\TESTING1\bin\TESTING3.dll"

Dim pAssm As [Assembly]
pAssm = pAppDom.Load(pAssmName) 'here I am getting error
'error message is "Request for the permission of type System.Security.Permissions.FileIOPermission, mscorlib,.......... failed"

'if I add evidence as another parameter
pAssm = pAppDom.Load(pAssmName, pmyEv) 'Here the error is
"Request for the permission of type system.security.Permissions.securitypermission, mscorlib.......... failed"


Can someone help me fix this problem. Is there a easy way to load an assembly? I apprecaite your response.
 
Ran successful here, but I had to comment out your oneModule.GetTypes reflection loop because the assembly depends on (implements) a type not available to me (ICommand), but the AppDomain, MBRO and assembly loading works.
 
new inteface

you need to have another software installed on your machine for you have access to Icommand. So, I can understand that.

It so confusing for me as you could sucessfull run code and I could not.

Is it because I am running it in debug mode? or it has to do with code access security previlages?:confused:
 
Addin Registration

Hi

Can you please tell how you registered the addin. I have not included the setup project in it. Did you place addin dll and assembly to be loaded in one location?

I tried creating new appdomainsetup with applicationbase directory pointing to the addin dll location. with the same evidence as current appdomain.

Now error message says that "ATTEMPT TO ACCESS ASSEMBLY FAILED". What does this mean??
 
loading assembly into new domain

I have also tried removing the interfaces like you did and tried. But does not make any difference. Can you please provide me the modified sample you have worked on? Thanks.
 
I don't have addins, I just referenced the MyAddin class library and called the domain method. I didn't remove any interfaces, I just didn't ask for types (oneModule.GetTypes)
 
the following code should work for .net 1.1 frame work. Credit goes to Derek Smyth, another MSDN forum member, who helped me fix this problem. I just created the DLLTypeCheck class in the excecuting assembly itself, in my case it is an addin which would document the assemblies once the developer compiles them, and then I called the IsDLLManaged function from another class in the same addin to load the compiled assembly and document it. I also tried to recompile the assembly and found that the loaded assembly is successfully unloaded.



VB.NET:
Imports System.Reflection

<Serializable()> _

Public Class DLLTypeCheck

Dim assemblyToLoad as string 

Public Function IsDLLManaged(ByRef strDllPath1 As String) As Boolean

Dim l As Integer

Dim IsManaged As Boolean = True

Dim pAppset As AppDomainSetup

pAppset = New AppDomainSetup

 

'here I passed the location of the executing assembly because it was throwing error saying could not find the assembly

pAppset.ApplicationBase = System.IO.Path.GetDirectoryName(Me.GetType.Assembly.GetExecutingAssembly.Location)

 

newAppDomain = AppDomain.CreateDomain("staging", Nothing, pAppset)

Try

assemblyToLoad = strDllPath1

dim callback As New CrossAppDomainDelegate(AddressOf LoadAssemblyInDomain)

newAppDomain.DoCallBack(callback)

 

IsManaged = True

Catch ex As Exception

IsManaged = False

MsgBox(ex.Message)

Finally

AppDomain.Unload(newAppDomain)

End Try

Return IsManaged

End Function

Public Sub LoadAssemblyInDomain()

Dim pAssm As [Assembly] = [Assembly].LoadFile(assemblyToLoad)

'here I did some thing to document the assembly

End Sub

 

End Class
In another class I called IsDLLManaged function

VB.NET:
Dim pLoadAssm As New DLLTypeCheck

If pLoadAssm.IsDLLManaged(FtpConstants.strDeploy & FtpConstants.strMDll) = True Then

msgbox "loaded"

else

msgbox "failed to load"

end if
GIS.NET
 
Last edited by a moderator:
Back
Top