日期:2014-05-20  浏览次数:21077 次

Assembly.LoadFrom
Assembly.LoadFrom求救    
在微软中国社区的聊天室看到以下聊天    

[Q]   关于Assembly.LoadFrom一直没有做实验,我可以LoadFrom( " "http://url/path/assembly.dll " "),那么我想知道的是,如果我不在线会发生什么情况?并且本地其实已经有它的副本了,在GAC全局缓冲中?    
[A]   LoadFrom应该会直接从URL那里取,而跳过GAC。    

对于LoadFrom的工作方式,网上有很多不一致的说法,请大家给我说明一下它的工作方式.    
我最近做一个项目,就是使用Assembly.LoadFrom,从服务器上取得DLL,但有时候会出错.    
说:DLL找不到,其实是可以找到的,把程序关了,在运行,就可以了.    

我快疯了!!!,请大家帮忙

------解决方案--------------------
你试试把assembly存到本地 比如放到C:\ 看还有这种问题没
如果没有 就和服务器有关
如果有 但也不可能和GAC有关 可能放到其它的地方缓存
------解决方案--------------------
public static Assembly LoadFrom(string assemblyFile)
{
return LoadFrom(assemblyFile, null);
}


public static Assembly LoadFrom(string assemblyFile, Evidence securityEvidence)
{
return LoadFrom(assemblyFile, securityEvidence, null, AssemblyHashAlgorithm.None);
}


public static Assembly LoadFrom(string assemblyFile, Evidence securityEvidence, byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm)
{
if (assemblyFile == null)
{
throw new ArgumentNullException( "assemblyFile ");
}
AssemblyName assemblyRef = new AssemblyName();
assemblyRef.CodeBase = assemblyFile;
assemblyRef.SetHashControl(hashValue, hashAlgorithm);
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return InternalLoad(assemblyRef, false, securityEvidence, ref stackMark);
}


internal static Assembly InternalLoad(AssemblyName assemblyRef, bool stringized, Evidence assemblySecurity, ref StackCrawlMark stackMark)
{
if (assemblyRef == null)
{
throw new ArgumentNullException( "assemblyRef ");
}
assemblyRef = (AssemblyName) assemblyRef.Clone();
if (assemblySecurity != null)
{
new SecurityPermission(SecurityPermissionFlag.ControlEvidence).Demand();
}
string strA = VerifyCodeBase(assemblyRef.CodeBase);
if (strA != null)
{
if (string.Compare(strA, 0, "file: ", 0, 5, true, CultureInfo.InvariantCulture) != 0)
{
IPermission permission = CreateWebPermission(assemblyRef.EscapedCodeBase);
if (permission == null)
{
return null;
}
permission.Demand();
}
else
{
URLString text2 = new URLString(strA, true);
new FileIOPermission(FileIOPermissionAccess.PathDiscovery | FileIOPermissionAccess.Read, text2.GetFileName()).Demand();
}
}
return nLoad(assemblyRef, strA, stringized, assemblySecurity, true, null, ref stackMark);
}




自己看