Runtime: could not find file 'System.Net.Http.resources'

Created on 26 Apr 2019  Â·  11Comments  Â·  Source: dotnet/runtime

_From @Rabbit021 on Friday, 26 April 2019 07:49:19_

Describe the bug

My App Crashe with Unhandled Exception: could not find file 'System.Net.Http.resources'.
I use independent publish, and I use class HttpClient to request data.
Some times it crash. when my network is not fine, it crashed frequently

Enviment

  1. ASP.NET Core 2.0 in Window Server 2012 R2
  2. I publish app with dotnet publish my.csproj -c debug -r win7-x64 -o %~dp0publish

Expected behavior

Exception

Unhandled Exception: System.IO.FileNotFoundException: Could not find file 'Syste
m.Net.Http.resources'.
   at System.Reflection.RuntimeAssembly.InternalGetSatelliteAssembly(String name
, CultureInfo culture, Version version, Boolean throwOnFileNotFound, StackCrawlM
ark& stackMark)
   at System.Resources.ManifestBasedResourceGroveler.GetSatelliteAssembly(Cultur
eInfo lookForCulture, StackCrawlMark& stackMark)
   at System.Resources.ManifestBasedResourceGroveler.GrovelForResourceSet(Cultur
eInfo culture, Dictionary`2 localResourceSets, Boolean tryParents, Boolean creat
eIfNotExists, StackCrawlMark& stackMark)
   at System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo reques
tedCulture, Boolean createIfNotExists, Boolean tryParents, StackCrawlMark& stack
Mark)
   at System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo cultur
e, Boolean createIfNotExists, Boolean tryParents)
   at System.Resources.ResourceManager.GetString(String name, CultureInfo cultur
e)
   at System.SR.GetResourceString(String resourceKey, String defaultString)
   at System.Net.Http.WinHttpHandler.HandleAsyncException(WinHttpRequestState st
ate, Exception ex)
   at System.Net.Http.WinHttpHandler.<StartRequest>d__105.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, C
ontextCallback callback, Object state)
   at System.Threading.ThreadPoolWorkQueue.Dispatch()

_Copied from original issue: aspnet/AspNetCore#9771_

area-System.Runtime question

All 11 comments

@Rabbit021 thanks for the report. In order for us to help, we will need a way to minimally reproduce this, starting from scratch. Maybe a github repo to clone on which we can then run the above command. Also details of dotnet --info

I solve it , i dynamic load dotnet core assebmly . it some time cannt found System.Net.Http.resources
image . in my develop machine .

@rabbit021 is this issue resolved then? I am not sure how to read your comment.

I an sorry, I am a little puzzle. I am dynamic load assembly. I use that code and resolver dependency.

var rst = new List<Assembly>();
rst.Add(GetType().Assembly);
AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;
if (plugins != null)
{
    foreach (var itr in plugins)
    {
        var file = Path.Combine(Environment.CurrentDirectory, "Plugins", itr.path);
        if (!File.Exists(file)) continue;
        var assembly = Assembly.LoadFile(file);
        rst.Add(assembly);
        PublicDatas.Plugins.Add(assembly.FullName, itr);
        mvcBuilder.AddApplicationPart(assembly);
    }
}

private Assembly OnAssemblyResolve(object sender, ResolveEventArgs args)
{
    var assemblyName = new AssemblyName(args.Name);
    var location = args.RequestingAssembly.Location;
    LoggerManager.Instance.Error($"{assemblyName} {location}");

    var file = Path.Combine(Path.GetDirectoryName(location), $"{assemblyName.Name}.dll");
    var assembly = args.RequestingAssembly;
    if (File.Exists(file))
    {
        assembly = Assembly.LoadFile(file);
    }
    else if (File.Exists(Path.Combine(AppContext.BaseDirectory, $"{assemblyName.Name}.dll")))
    {
        assembly = Assembly.LoadFile(file);
    }
    return assembly;
}

but when i am reference plugin assembly to my start up project . It is ok.

@tarekgh any idea bout this one?

@sdmaclea may know better here for assembly loading.

@Rabbit021 From the exception in the top post. It looks like an exception was thrown, then a handler tried to get the localized version of an exception related message.

The System.Net.Http.resources is a satellite assembly. It would normally be located in a culture specific directory next to <path>/System.Net.Http.dll. So for culture name en, it would be in <path>/en/System.Net.Http.resources.dll. It looks like the ResolveEventArgs does not have sufficient info to determine the culture name. So your OnAssemblyResolve isn't handling it. The AssemblyLoadContext.Resolving event does have the CultureInfo within the AssemblyName. It can be used to resolve satellite assemblies.

The default satellite assembly probing for custom AssemblyLoadContext was missing until very recently. It was just added, and should be available in .NET Core preview6 daily builds shortly. The probing code is here.
https://github.com/dotnet/coreclr/blob/07b3afc27304800f00975c8fd4836b319aaa8820/src/System.Private.CoreLib/shared/System/Runtime/Loader/AssemblyLoadContext.cs#L559-L594. It is not written to be a AssemblyLoadContext.Resolving handler, but it wouldn't take much effort to restructure the code.

The FileNotFoundException seems to be coming from your OnAssemblyResolve handler. There appear to be two functional errors...
C# private Assembly OnAssemblyResolve(object sender, ResolveEventArgs args) { ... var file = Path.Combine(Path.GetDirectoryName(location), $"{assemblyName.Name}.dll"); // This is not a reasonable default return value... var assembly = args.RequestingAssembly; if (File.Exists(file)) ... else if (File.Exists(Path.Combine(AppContext.BaseDirectory, $"{assemblyName.Name}.dll"))) { // ERROR file NEVER exists here assembly = Assembly.LoadFile(file); } // ERROR should return null when not found return assembly; }

@Rabbit021 per @sdmaclea comment the issue looks in the event handler code. Closing this issue but feel free to reply back with any more questions.

I publish project,but i not found System.Net.Http.resources.dll in my program folder.
when i change my plugin assembly project`s output type to console ,like this
image. it seem working fine,my OnAssemblyResolve code not changed.
System.Net.Http.resources.dll was not found in start directory.

i not found System.Net.Http.resources.dll in my program folder.

This is probably normal. The satellite resource fallback algorithm looks for the the specific culture, then its parent cultures, then will look for the language neutral resources in the main assembly. Likely there is no localized version of System.Net.Http, so not found is normal expected behavior.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bencz picture bencz  Â·  3Comments

yahorsi picture yahorsi  Â·  3Comments

iCodeWebApps picture iCodeWebApps  Â·  3Comments

GitAntoinee picture GitAntoinee  Â·  3Comments

btecu picture btecu  Â·  3Comments