I have a desktop application where i need to load some dll using reflection in a different AppDomain, call a method and unload the AppDomain. I have correctly developed it, however the number of assemblies i need to load and unload has been increased recently, and now, after loading 3 dll, when i try to load the 4th dll I get this exception:
System.TypeInitializationException: An exception occurred on startup type '<Module>'. ---> System.AccessViolationException: Attempted to read or write protected memory. This often indicates that other memory is corrupt.
en .cctor()
--- End of monitoring the inner exception stack ---
en System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
en System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
en System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
en System.Activator.CreateInstance(Type type, Boolean nonPublic)
en System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)
en System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
en System.Reflection.Assembly.CreateInstance(String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
en BankIntegration.ReflectionCaller.CallInternal(String dll, String typename, String method, Object[] parameters)
en BankIntegration.ReflectionCaller.CallInternal(String dll, String typename, String method, Object[] parameters)
en BankIntegration.ReflectionCaller.Call(String pathToDLL, String typename, String method, Object[] parameters)
en BankIntegration.DllManager.init_bank_conectors(Server service)
en BankIntegration.frmClient..ctor(Server service, String dllPath, Int64 idclient)
en AVISync.frmInit.Button_clicked(Object sender, ItemClickEventArgs e)
After searching on the net, i can't even figure what's the problem. If I debug the application on visual studio it works properly. It only appear on the published application. This is the relevant code:
'DLLManager:
For Each bean As BankIntegration.AppUtil.bankBean In aux
Dim dllPath As String = Path.Combine(DLL_FILES_DIR, bean.DllName)
Dim download As Boolean = False
If File.Exists(dllPath) Then
Dim info As BankInterfaces.beans.DLLInfo = ReflectionCaller.Call(dllPath, bean.DllNameSpace & ".Connector", BankInterfaces.Util.BANKINTERFACE_NAMES.getDLLInfo, Nothing)
'Irrelevant code here'
info = Nothing
'Irrelevant code here'
End If
'Irrelevant code here'
GC.Collect()
Next
'ReflectionCaller
Public Shared Function [Call](ByVal pathToDLL As String, ByVal typename As String, ByVal method As String, ByVal ParamArray parameters As Object()) As Object
Dim permissions As New Security.PermissionSet(Security.Permissions.PermissionState.Unrestricted)
Dim adSetup As New AppDomainSetup()
adSetup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory
Dim dom As AppDomain = AppDomain.CreateDomain(Guid.NewGuid.ToString, AppDomain.CurrentDomain.Evidence, adSetup, permissions)
Dim ld As ReflectionCaller = DirectCast(dom.CreateInstanceAndUnwrap(Reflection.Assembly.GetExecutingAssembly().FullName, GetType(ReflectionCaller).FullName), ReflectionCaller)
Dim result As Object = ld.CallInternal(pathToDLL, typename, method, parameters)
AppDomain.Unload(dom)
dom = Nothing
ld = Nothing
GC.Collect()
Return result
End Function
Private Function CallInternal(dll As String, typename As String, method As String, parameters As Object()) As Object
Dim a As Reflection.Assembly = Reflection.Assembly.LoadFile(dll)
Dim o As Object = a.CreateInstance(typename) '<-- This line throws the exception after a few iterations'
Dim t As Type = o.[GetType]()
Dim m As Reflection.MethodInfo = t.GetMethod(method)
Dim result As Object = m.Invoke(o, parameters)
a = Nothing
o = Nothing
t = Nothing
m = Nothing
GC.Collect()
Return result
End Function
Aucun commentaire:
Enregistrer un commentaire