I'm trying to load UserControl from sharepoint url, but i don't have referenced assembly in GAC. Instead i'm loading it's content from database to current AppDomain using Assembly.Load(byte[]). I have also added AssemblyResolver event that returns my loaded dll, so it can get proper context. Code works fine for normal library dlls. When i'm trying to load UserControl i'm getting 'can't load assembly or one of it's references' error when only the main dll is in GAC or null reference at System.CodeDom.Compiler.CodeDomProvider.TryGetProbableCoreAssemblyFilePath(CompilerParameters parameters, String& coreAssemblyFilePath) when both dlls are from database and just ascx is in sharepoint layouts.
What i'm trying to achieve is loading ascx control in runtime with dynamically loaded assemblies, or with just main assembly in sharepoint GAC and the referenced one from reflection.
Here is my code: Loading assembly to appdomain.
if (files.Query.Load())
{
if (_filesFullNameId.ContainsKey(files.Current.FullName))
return result;
Assembly.Load(files.Current.Content);
result.Add(new KeyValuePair<string, int>(files.Current.FullName, fileId));
}
Resolver event added in singleton cache constructor to current appdomain:
private Assembly AssemblyResolver(object sender, ResolveEventArgs args)
{
var assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.FullName == args.Name);
if (assembly == null && args.RequestingAssembly != null)
{
LoggingProvider.Logger.WriteToEventLog(new WriteToSystemLogParams
{
//log
});
}
return assembly;
}
And LoadControl executed on sharepoint service:
SPSecurity.RunWithElevatedPrivileges(
() =>
{
PluginAssemblyCache.Instance.LoadPackageById(MainContext.Instance.GetCurrentDbInfo(), packageId);
var control = page.LoadControl(url);
if (control != null)
{
var type = control.GetType().BaseType;
metadata.SpTypeName = type.FullName;
metadata.AssemblyName = type.Assembly.FullName;
}
});
Aucun commentaire:
Enregistrer un commentaire