Skip to content

Commit

Permalink
Fix IsolatedStorage initialization on Android (#56534)
Browse files Browse the repository at this point in the history
Android doesn't set an entry assembly so we need to handle that case.

Fixes #52332
  • Loading branch information
akoeplinger authored Jul 29, 2021
1 parent eb0b60e commit beeafa0
Showing 1 changed file with 26 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,35 +46,37 @@ internal static void GetDefaultIdentityAndHash(out object identity, out string h
// by pulling directly from EntryAssembly.
//
// Note that it is possible that there won't be an EntryAssembly, which is something the .NET Framework doesn't
// have to deal with and shouldn't be likely on .NET Core due to a single AppDomain. Without Evidence
// to pull from we'd have to dig into the use case to try and find a reasonable solution should we
// run into this in the wild.
// have to deal with and isn't likely on .NET Core due to a single AppDomain. The exception is Android which
// doesn't set an EntryAssembly.

Assembly? assembly = Assembly.GetEntryAssembly();
string? location = null;

if (assembly == null)
throw new IsolatedStorageException(SR.IsolatedStorage_Init);

AssemblyName assemblyName = assembly.GetName();

hash = IdentityHelper.GetNormalizedStrongNameHash(assemblyName)!;
if (hash != null)
if (assembly != null)
{
hash = "StrongName" + separator + hash;
identity = assemblyName;
}
else
{
string? location = assembly.Location;
// In case of SingleFile deployment, Assembly.Location is empty.
if (location == string.Empty)
location = Environment.ProcessPath;
if (string.IsNullOrEmpty(location))
throw new IsolatedStorageException(SR.IsolatedStorage_Init);
Uri locationUri = new Uri(location);
hash = "Url" + separator + IdentityHelper.GetNormalizedUriHash(locationUri);
identity = locationUri;
AssemblyName assemblyName = assembly.GetName();

hash = IdentityHelper.GetNormalizedStrongNameHash(assemblyName)!;
if (hash != null)
{
hash = "StrongName" + separator + hash;
identity = assemblyName;
return;
}
else
{
location = assembly.Location;
}
}

// In case of SingleFile deployment, Assembly.Location is empty. On Android there is no entry assembly.
if (string.IsNullOrEmpty(location))
location = Environment.ProcessPath;
if (string.IsNullOrEmpty(location))
throw new IsolatedStorageException(SR.IsolatedStorage_Init);
Uri locationUri = new Uri(location);
hash = "Url" + separator + IdentityHelper.GetNormalizedUriHash(locationUri);
identity = locationUri;
}

internal static string GetRandomDirectory(string rootDirectory, IsolatedStorageScope scope)
Expand Down

0 comments on commit beeafa0

Please sign in to comment.