0

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:

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.

Lancelot
  • 573
  • 2
  • 5
  • 27

2 Answers2

0

Have a look at this github repo. I had to work with SharePoint CSOM and .NET Core a few years back and I had faced simliar issues. Note that the code uses .NET Core and not .NET Framework. Hope it will help.

Raju Joseph
  • 493
  • 1
  • 6
  • 13
  • Thanks for your answer and your link. I checked your repo and I saw that you use SharePoint CSOM, but unfortunately, I did not see how you handle the System.Net.Http problem. – Lancelot May 05 '20 at 07:42
  • Check the binaries in src\Lib folder. I had to manually add references to those assemblies rather than using the NuGet package since there were issues with the package. Have a look at the Guild.Repository csproj file, you will see that I have explicitly added references to the SharePoint assemblies in the Lib folder. – Raju Joseph May 05 '20 at 08:29
  • The NuGet package I am talking about is the SharePoint CSOM package. – Raju Joseph May 05 '20 at 08:40
  • Following your answer, I updated my question with more information. – Lancelot May 05 '20 at 09:57
  • Can you isolate the origin of the issue? Meaning, can you remove the project that references CSOM and Office Dev from the solution. Then do you get the error? It took me a few days also to resolve issues related to CSOM. In my case, things went south the moment I added the CSOM package. Without seeing the code, its quite difficult to find the root cause. Sorry. – Raju Joseph May 05 '20 at 10:51
  • Yes, the problem occurs only with the project referencing SharePoint CSOM. I also tried to build the project on another machine with the same result. – Lancelot May 05 '20 at 11:38
0

The solution/workaround was to go back to .NET Framework 4.6.1. See initial question for more information.

Lancelot
  • 573
  • 2
  • 5
  • 27