Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could not load file or assembly System.Net.Http Version=4.0.0.0 in .net core app #25540

Closed
rizwaans opened this issue Mar 20, 2018 · 12 comments
Closed
Labels
area-System.Net.Http question Answer questions and provide assistance, not an issue with source code or documentation.
Milestone

Comments

@rizwaans
Copy link

I've got this issue in .net core 2.0, just to give you more idea about what I'm trying to achieve. I'm trying to connect to VSTS and update the test status "Passed" or "Failed" based on my unit tests
I run of this "Could not load file or assembly System.Net.Http Version=4.0.0.0" issue where the code is trying to open a connection to vsts.
var connection = new VssConnection("https://.visualstudio.com", credentials);

@karelz
Copy link
Member

karelz commented Mar 20, 2018

Does it repro in brand new .NET Core 2.0 console app for you? Or does the repro require some additional dependencies?

@rizwaans
Copy link
Author

rizwaans commented Mar 20, 2018

I think you can reproduce it with out any additional dependencies. I just tried creating a new .net core console app and execute the below code.

        try
        {
            var u = new Uri("https://{My Account}.visualstudio.com");
            VssCredentials c = new VssCredentials(new Microsoft.VisualStudio.Services.Common.VssBasicCredential(username, password));
            var connection = new VssConnection(u, c);
            var testClient = connection.GetClient<TestManagementHttpClient>();
            int testpointid = 1;
            string teamProject = "MyProjectName";
            RunCreateModel run = new RunCreateModel(name: "TestCase Name", plan: new Microsoft.TeamFoundation.TestManagement.WebApi.ShallowReference("TestPlan Id"), pointIds: new int[] { testpointid });
            TestRun testrun = testClient.CreateTestRunAsync(run, teamProject).Result;

            TestCaseResult caseResult = new TestCaseResult() { State = "Completed", Outcome = "passed", Id = 100000 };

            var testResults = testClient.UpdateTestResultsAsync(new TestCaseResult[] { caseResult }, teamProject, testrun.Id).Result;
            RunUpdateModel runmodel = new RunUpdateModel(state: "Completed");
            TestRun testRunResult = testClient.UpdateTestRunAsync(runmodel, teamProject, testrun.Id, runmodel).Result;

        }
        catch (AggregateException e)
        {
            Console.WriteLine(e.InnerException.Message);
        }

[EDIT] Add C# syntax highlighting by @karelz

@karelz
Copy link
Member

karelz commented Mar 21, 2018

Where do you get these types from then?

  • Microsoft.VisualStudio.Services.Common.VssBasicCredential
  • Microsoft.TeamFoundation.TestManagement.WebApi.ShallowReference
    Can you post exact steps how to reproduce it? (start with new Console .NET Core 2.0 app)

Do you need more than the first 3 lines of code to reproduce it?

@rizwaans
Copy link
Author

ok here you go.
1.create a .net core 2.0 console app
2.add Microsoft.TeamFoundationServer.ExtendedClient (15.112.1) package from Nuget
3.put the sample code from above comment , try and build.

.visualstudio.com

hope this helps you to recreate the issue.

incase you're not able to add the extended client package. just add the below code in .csproj

portable-net451+win8

@davidsh
Copy link
Contributor

davidsh commented May 10, 2018

@rizwaans What version of Visual Studio are you using? Visual Studio 2017? And what update are you using? We recommend the latest Update 15.7 for Visual Studio 2017.

@SivakumarShan
Copy link

Hi I'm also getting the same error while using the below code.
var vssConnection = new Microsoft.VisualStudio.Services.WebApi.VssConnection(new Uri(tfsCollectionUri), new Microsoft.VisualStudio.Services.Common.VssBasicCredential("", ""));

I'm using .NET Core 2.0 version.

@karelz
Copy link
Member

karelz commented Jun 7, 2018

@SivakumarShan can you answer @davidsh's questions above please? (https://github.com/dotnet/corefx/issues/28267#issuecomment-388199656)

@SivakumarShan
Copy link

@karelz I'm using VS 2017 with 15.5.6 version. Let me update to the above mentioned version and try it out. Thanks :)

@SivakumarShan
Copy link

@karelz I've updated to VS Enterprise 2017 to 15.7.3 and still getting the same issue. I'm using netcoreapp2.0. Please help me to resolve this issue. Thanks.

@karelz
Copy link
Member

karelz commented Jun 11, 2018

I am getting slightly different error, which is by design - it seems that the NuGet packages you're using are not compatible with .NET Core and use .NET Framework API surface (from System.Net.Http.WebRequest.dll) which is not available on .NET Core.

Repro steps: (based on https://github.com/dotnet/corefx/issues/28267#issuecomment-374809539)

using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi;

class Program
{
    static void Main()
    {
        VssCredentials c = new VssCredentials(new Microsoft.VisualStudio.Services.Common.VssBasicCredential("my_name", "abc"));
        var connection = new VssConnection(new System.Uri("https://my_name.visualstudio.com"), c);
    }
}

Note build warnings - listing assemblies which target .NET Framework 4.6.1 instead of .NET Core 2.0:

warning NU1603: Microsoft.IdentityModel.Clients.ActiveDirectory 3.13.5 depends on System.Net.Http (>= 4.0.1) but System.Net.Http 4.0.1 was not found. An approximate best match of System.Net.Http 4.1.0 was resolved.
warning NU1603: Microsoft.IdentityModel.Clients.ActiveDirectory 3.13.5 depends on System.Text.RegularExpressions (>= 4.0.12) but System.Text.RegularExpressions 4.0.12 was not found. An approximate best match of System.Text.RegularExpressions 4.1.0 was resolved.
warning NU1701: Package 'Microsoft.AspNet.WebApi.Client 5.2.2' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.
warning NU1701: Package 'Microsoft.AspNet.WebApi.Core 5.2.2' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.
warning NU1701: Package 'Microsoft.TeamFoundationServer.Client 15.112.1' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.
warning NU1701: Package 'Microsoft.VisualStudio.Services.Client 15.112.1' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.
warning NU1701: Package 'Microsoft.VisualStudio.Services.InteractiveClient 15.112.1' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.
warning NU1701: Package 'Newtonsoft.Json 8.0.3' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.
warning NU1701: Package 'System.IdentityModel.Tokens.Jwt 4.0.2.206221351' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.

At runtime, I get exception explaining System.Net.Http.WebRequest is not available:

System.IO.FileNotFoundException: Could not load file or assembly 'System.Net.Http.WebRequest, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

Using .NET Framework assets on .NET Core is a bridge which helps to try libraries that didn't start targeting .NET Core / .NET Standard yet. It is not guaranteed to work and in this case, it won't work as the libraries have dependencies which cannot be satisfied on .NET Core.

Closing as by design - the libraries you use need to ship targeting .NET Core, or you can use them only on .NET Framework.

@karelz karelz closed this as completed Jun 11, 2018
@kaaoo
Copy link

kaaoo commented Sep 26, 2018

Hi everybody!
I had the same error. But I've found another issue which helped me.
I just changed the version of Vsts client to version 15.126.0-preview.
Hope it will help you!

@dsun-ge
Copy link

dsun-ge commented Aug 12, 2019

I had the same problem. I figured out that I created a wrong type of project. It should be Windows Console application that runs on .NET Framework,, not CoreApp console application that runs on CoreApp2.0. I am new to .NET and hope my experience helps those like me.

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 3.0 milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Net.Http question Answer questions and provide assistance, not an issue with source code or documentation.
Projects
None yet
Development

No branches or pull requests

7 participants