【发布时间】:2020-05-05 05:58:54
【问题描述】:
我目前正面临“System.Net.Http”程序集的问题,这让我非常头疼。
在继续之前,我在网上搜索了几个小时,阅读并尝试了几篇文章中提到的解决方案,如下所示:
- Strange issue with System.Net.Http 4.2.0.0 not found
- https://github.com/dotnet/runtime/issues/26131
- Could not load file or assembly 'System.Net.Http, Version=4.2.0.0, Culture=neutral on IIS
- https://github.com/Microsoft/dotnet/blob/master/releases/net472/KnownIssues/613745%20-%20Single-name%20references%20are%20removed%20by%20the%20SDK%20when%20targeting%204.7.2.md
- Could not load file or assembly 'System.Net.Http
- https://www.reddit.com/r/csharp/comments/99zuzs/systemnethttp_is_driving_me_insane/
- Could not load file or assembly "System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
显然,我错过了一些东西,因为我无法解决这个问题。
基本上,我有一个由多个项目组成的解决方案,这些项目都针对 .NET Framework 4.7.2。其中一个是简单的控制台应用程序,其他的是类库项目。其中之一包含对 SharePoint CSOM 和 Office Dev PnP 的引用。
但是,每次点击以下行...
context.ExecuteQuery();
...我收到以下错误:
Exception thrown: 'System.IO.FileNotFoundException' in mscorlib.dll
Message "Could not load file or assembly 'System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified."
堆栈跟踪如下所示:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at System.ServiceModel.Security.IWSTrustContract.Issue(Message message)
at System.ServiceModel.Security.WSTrustChannel.Issue(RequestSecurityToken rst, RequestSecurityTokenResponse& rstr)
at System.ServiceModel.Security.WSTrustChannel.Issue(RequestSecurityToken rst)
at OfficeDevPnP.Core.IdentityModel.TokenProviders.ADFS.UsernameMixed.RequestToken(String userName, String passWord, Uri userNameMixed, String relyingPartyIdentifier)
at OfficeDevPnP.Core.IdentityModel.TokenProviders.ADFS.UsernameMixed.GetFedAuthCookie(String siteUrl, String userName, String password, Uri userNameMixed, String relyingPartyIdentifier, Int32 logonTokenCacheExpirationWindow)
at OfficeDevPnP.Core.AuthenticationManager.<>c__DisplayClass42_0.<GetADFSUserNameMixedAuthenticatedContext>b__0(Object oSender, WebRequestEventArgs webRequestEventArgs)
at Microsoft.SharePoint.Client.ClientRuntimeContext.OnExecutingWebRequest(WebRequestEventArgs args)
at Microsoft.SharePoint.Client.ClientContext.GetWebRequestExecutor()
at Microsoft.SharePoint.Client.ClientContext.GetFormDigestInfoPrivate()
at Microsoft.SharePoint.Client.ClientContext.EnsureFormDigest()
at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()
at SP19ProvisioningAppDomain.Repository.WebRepository.ProvisionWeb(String template) in ... line 105
我使用 NuGet 安装了 Microsoft.SharePoint.Client、OfficeDevPnP.Core 和 SharePointPnPCore2019。
起初,“packages.config”文件中只有“System.Net.Http”的引用。所以我尝试在“app.config”文件中做以下事情:
// 1
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.1.1.3" />
// 2
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" />
// 3
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.0.0.0" />
// 4
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
在“.csproj”文件中,还有以下几行:
<Reference Include="System.Net" />
<Reference Include="System.Net.Http, Version=4.1.1.3, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Net.Http.4.3.4\lib\net46\System.Net.Http.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
我试着玩弄这些,但没有更多成功。
我还尝试通过包管理器删除“System.Net.Http”并通过“References / Framework”添加它,但它也不起作用,因为我根本无法勾选复选框并且卸载包会引发依赖错误。我删除了“bin”和“packages”文件夹,卸载并重新安装了解决依赖错误的包,清除了 NuGet 缓存,重新启动了 Visual Studio一千次,彻底搞砸了我的解决方案,做了很多“NuGet restore”来回到我开始的地方。
说实话,我以前从未遇到过这个错误,我有点困惑。我无法弄清楚我错过了什么。我可以补充一下,我正在使用 Visual Studio Professional 2019, 16.5.4。
有没有人有想法或可以帮我解决这个问题?将不胜感激。
最好的问候,
编辑 1
按照 Raju Joseph 的回答,我尝试了以下方法:
- 卸载项目的每个 NuGet 包
- 删除“bin”和“obj”文件夹
- 清理“app.config”和“packages.files”
- 清理“csproj”文件
- 将所需的 DLL 复制到另一个文件夹并手动添加它们作为参考
不幸的是,它并没有更好地工作。
所以,我再次清理了所有内容并重新安装了 NuGet 包。我注意到,在“References”下,“System.Net.Http”的路径以我的本地“Program Files”为目标,而我在我的“csproj”文件中有以下几行:
<Reference Include="System.Net.Http, Version=4.1.1.3, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Net.Http.4.3.4\lib\net46\System.Net.Http.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
我有另一个“旧”项目,目标是 .NET Framework 4.6.1,具有几乎相同的依赖项,在这个项目中,路径设置为“System.Net.Http em>”以我的项目的“packages”文件夹为目标。
在我当前的项目中,每次我尝试更改“System.Net.Http”的引用路径并从我的“packages”目录中选择 DLL,它将路径设置为“Program Files”。
我还尝试在另一台机器上构建和运行项目,结果相同。
我很困惑……
编辑 2
我决定复制我的整个解决方案,然后我清理了每个项目并让它们以 .NET Framework 4.6.1 为目标。现在,我似乎摆脱了这个错误。有人解释一下吗?
解决方案 - 变通方法
经过多次测试,我确认回到 .NET Framework 4.6.1 整个解决方案似乎可行。我的所有项目都编译,单元测试工作,我可以在我的代码中使用 SharePoint 2019 CSOM 和 SharePoint PnP Core 而不会出现任何错误。
这可能不是一个好的解决方案,但就我而言,这是可以接受的。
【问题讨论】:
标签: c# .net visual-studio sharepoint nuget