是的,我知道这个问题看起来类似于 C# - 'Resources' DLL failing to be loaded as it doesn't exist,但我的问题是关于一个 我不拥有的 DLL。 另一个问题涉及作者的拥有他们编写的 DLL。
编辑:解决问题后,事实证明是完全相同的场景。我已将自己的问题自我标记为重复。
我正在编写一个使用 Cosmos DB .NET SDK 3.32.2 的 C# 库。有时,这个库会抛出异常,因为它找不到
Microsoft.Azure.Cosmos.Direct.resources.dll
。这个文件一开始就不存在; Cosmos.Direct
DLL 带有 en-US
不变的文化资源 嵌入在 DLL 中。是的,我反编译了它来检查 - Cosmos.Direct
似乎是 Cosmos 团队没有作为开源发布的 SDK 的一部分。
明确地说,我不控制
Microsoft.Azure.Cosmos.Direct.dll
。那是我的依赖。仅供参考,Microsoft.Azure.Cosmos
的 NuGet 包附带四个 DLL,其中之一是 Microsoft.Azure.Cosmos.Direct.dll
.
FooBar.OurCustomExceptionType: One or more errors occurred. ---> System.AggregateException: One or more errors occurred. ---> System.IO.FileNotFoundException: Could not load file or assembly 'file:///C:\Foo\Bar\Microsoft.Azure.Cosmos.Direct.resources.dll' or one of its dependencies. The system cannot find the file specified.
at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalGetSatelliteAssembly(String name, CultureInfo culture, Version version, Boolean throwOnFileNotFound, StackCrawlMark& stackMark)
at System.Resources.ManifestBasedResourceGroveler.GetSatelliteAssembly(CultureInfo lookForCulture, StackCrawlMark& stackMark)
at System.Resources.ManifestBasedResourceGroveler.GrovelForResourceSet(CultureInfo culture, Dictionary`2 localResourceSets, Boolean tryParents, Boolean createIfNotExists, StackCrawlMark& stackMark)
at System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo requestedCulture, Boolean createIfNotExists, Boolean tryParents, StackCrawlMark& stackMark)
at System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo culture, Boolean createIfNotExists, Boolean tryParents)
at System.Resources.ResourceManager.GetString(String name, CultureInfo culture)
at Microsoft.Azure.Documents.StoreResult.CreateStoreResult(StoreResponse storeResponse, Exception responseException, Boolean requiresValidLsn, Boolean useLocalLSNBasedHeaders, Uri storePhysicalAddress)
at Microsoft.Azure.Documents.StoreReader.<ReadMultipleReplicasInternalAsync>d__14.MoveNext()
... long stack trace ...
at FooBar.OurCustomCosmosWrapper`1.<GetItemAsync>d__4.MoveNext()
查看堆栈跟踪,库正在搜索一个从未存在过的资源的“卫星程序集”,即使资源嵌入在 DLL 中。
我已将我自己的应用程序设置为在 AssemblyInfo.cs 中使用
[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.MainAssembly)]
,但显然这不会影响此 Cosmos 依赖库。它只影响我自己的依赖 Cosmos SDK 的 FooBar 程序集。
如何强制我的一个依赖 DLL(在本例中为 Microsoft.Azure.Cosmos.Direct.dll)使用其自己的嵌入式资源,而不是尝试查找不存在的单独
resources.dll
文件?
我打赌这可能是几个问题之一:
这不是Cosmos的错;我们实际上在其他地方注册了一个 AppDomain.AssemblyResolve 处理程序,它正在拦截程序集加载,即使它们是嵌入式程序集,并试图从特定位置将它们作为 .dll 文件加载。这个文件从来不存在,所以它会失败。
我最初在问题中提到的那个 SO 帖子? C# - “资源”DLL 无法加载,因为它不存在
确实解决了问题。只需在您的存储库中搜索
AssemblyResolve
,检查是否为其注册了处理程序函数,然后确保该处理程序在遇到您的嵌入式资源程序集之一时返回 null
(或 nullptr
,如果在 C++ 中)。