I am currently facing a problem with the "System.Net.Http" assembly that gives me a real headache.
Before going any further, I searched for hours on the Web reading and trying the solutions mentioned in several posts, like the following ones:
- 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"
Obviously, I am missing something, because I can't solve this problem.
Basically, I have a Solution composed of several projects all targeting .NET Framework 4.7.2. One of the is a simple console application and the others are class library project. One of them contains references to SharePoint CSOM and Office Dev PnP.
However, each time the following line is hit...
context.ExecuteQuery();
...I get the following error:
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."
The stack trace looks like this:
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
I installed Microsoft.SharePoint.Client, OfficeDevPnP.Core and SharePointPnPCore2019 using NuGet.
At first, there was only a reference to "System.Net.Http" in the "packages.config" file. So I tried to do the following things in the "app.config" file:
// 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" />
In the ".csproj" file, there were also the following lines:
<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>
I tried to play around with those, but without more success.
I also tried to remove "System.Net.Http" through the Package Manager and to add it through "References / Framework", but it didn't work either because I simply can't tick the check box and uninstalling the package raises dependency errors. I removed the "bin" and the "packages" folders, uninstalled and reinstalled packages fighting dependency errors, cleared NuGet cache, restarted Visual Studio a thousand times, completely messed up my Solution, did many "NuGet restore" to get back where I started.
To be honest, I never encountered this error before and I am a bit confused. I can't figure out what I am missing. I can add that I am using Visual Studio Professional 2019, 16.5.4.
Does anyone have an idea or can give me a hand on this? It would be much appreciated.
Best regards,
EDIT 1
Following Raju Joseph answer, I tried the following things :
- Uninstall every NuGet packages for the project
- Delete "bin" and "obj" folders
- Clean "app.config" and "packages.files"
- Clean "csproj" file
- Copy required DLL in another folder and add them manually as references
Unfortunately, it didn't work better.
So, I cleaned everything again and reinstalled NuGet packages. What I noticed is, under "References", the path for "System.Net.Http" targets my local "Program Files" while I have the following lines in my "csproj" file:
<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>
I have another "old" project, targeting .NET Framework 4.6.1 with almost the same dependencies and in this project, the path set for "System.Net.Http" targets the "packages" folder of my project.
In my current project, every time I try to change the reference path for "System.Net.Http" and select the DLL from my "packages" directory, it sets the path to "Program Files".
I also tried to build and run the project on another machine with the same result.
I am confused...
EDIT 2
I decided to make a copy of my whole Solution, then I cleaned every project and made them target .NET Framework 4.6.1. For now, it seems that I get rid of this error. Does someone have an explanation?
SOLUTION - WORKAROUND
After several tests, I confirm that going back to .NET Framework 4.6.1 for the whole Solution seems to work. All my projects compile, unit tests work and I can use SharePoint 2019 CSOM and SharePoint PnP Core in my code without getting any error.
It may not be a good solution, but in my case, this is acceptable.