public override string GetSavedLicenseKey(Type type, Assembly resourceAssembly)
{
if (savedLicenseKeys == null || savedLicenseKeys[type.AssemblyQualifiedName] == null)
{
Debug.WriteLineIf(s_runtimeLicenseContextSwitch.TraceVerbose, "savedLicenseKey is null or doesnt contain our type");
if (savedLicenseKeys == null)
{
savedLicenseKeys = new Hashtable();
}
Uri licenseFile = null;
// the AppDomain.CurrentDomain.SetupInformation.LicenseFile always returns null.
// This means we have to find the license file using the fallback logic below.
if (resourceAssembly == null)
{
resourceAssembly = Assembly.GetEntryAssembly();
}
if (resourceAssembly == null)
{
Debug.WriteLineIf(s_runtimeLicenseContextSwitch.TraceVerbose, "resourceAssembly is null");
// If Assembly.EntryAssembly returns null, then we will
// try everything!
//
foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
{
// Though, I could not repro this, we seem to be hitting an AssemblyBuilder
// when walking through all the assemblies in the current app domain. This throws an
// exception on Assembly.CodeBase and we bail out. Catching exceptions here is not a
// bad thing.
if (asm.IsDynamic)
continue;
// file://fullpath/foo.exe
//
string fileName;
fileName = GetLocalPath(asm.EscapedCodeBase);
fileName = new FileInfo(fileName).Name;
Stream s = asm.GetManifestResourceStream(fileName + ".licenses");
if (s == null)
{
//Since the casing may be different depending on how the assembly was loaded,
//we'll do a case insensitive lookup for this manifest resource stream...
s = CaseInsensitiveManifestResourceStreamLookup(asm, fileName + ".licenses");
}
if (s != null)
{
DesigntimeLicenseContextSerializer.Deserialize(s, fileName.ToUpper(CultureInfo.InvariantCulture), this);
break;
}
}
}
else if (!resourceAssembly.IsDynamic)
{ // EscapedCodeBase won't be supported by emitted assemblies anyway
Debug.WriteLineIf(s_runtimeLicenseContextSwitch.TraceVerbose, "resourceAssembly is not null");
string fileName;
fileName = GetLocalPath(resourceAssembly.EscapedCodeBase);
fileName = Path.GetFileName(fileName); // we don't want to use FileInfo here... it requests FileIOPermission that we
// might now have... see VSWhidbey 527758
string licResourceName = fileName + ".licenses";
// first try the filename
Stream s = resourceAssembly.GetManifestResourceStream(licResourceName);
if (s == null)
{
string resolvedName = null;
CompareInfo comparer = CultureInfo.InvariantCulture.CompareInfo;
string shortAssemblyName = resourceAssembly.GetName().Name;
// if the assembly has been renamed, we try our best to find a good match in the available resources
// by looking at the assembly name (which doesn't change even after a file rename) + ".exe.licenses" or + ".dll.licenses"
foreach (String existingName in resourceAssembly.GetManifestResourceNames())
{
if (comparer.Compare(existingName, licResourceName, CompareOptions.IgnoreCase) == 0 ||
comparer.Compare(existingName, shortAssemblyName + ".exe.licenses", CompareOptions.IgnoreCase) == 0 ||
comparer.Compare(existingName, shortAssemblyName + ".dll.licenses", CompareOptions.IgnoreCase) == 0)
{
resolvedName = existingName;
break;
}
}
if (resolvedName != null)
{
s = resourceAssembly.GetManifestResourceStream(resolvedName);
}
}
if (s != null)
{
DesigntimeLicenseContextSerializer.Deserialize(s, fileName.ToUpper(CultureInfo.InvariantCulture), this);
}
}
if (licenseFile != null)
{
Debug.WriteLineIf(s_runtimeLicenseContextSwitch.TraceVerbose, "licenseFile: " + licenseFile.ToString());
Debug.WriteLineIf(s_runtimeLicenseContextSwitch.TraceVerbose, "opening licenses file over URI " + licenseFile.ToString());
Stream s = OpenRead(licenseFile);
if (s != null)
{
string[] segments = licenseFile.Segments;
string licFileName = segments[segments.Length - 1];
string key = licFileName.Substring(0, licFileName.LastIndexOf("."));
DesigntimeLicenseContextSerializer.Deserialize(s, key.ToUpper(CultureInfo.InvariantCulture), this);
}
}
}
Debug.WriteLineIf(s_runtimeLicenseContextSwitch.TraceVerbose, "returning : " + (string)savedLicenseKeys[type.AssemblyQualifiedName]);
return (string)savedLicenseKeys[type.AssemblyQualifiedName];
}