Reference to type 'Task<>' claims it is defined in 'System.Runtime', but it could not be found

Hi, I’m liking the idea of the new Checkout API (it’s just what I need), and after following through the Quick Start guide using VS2019 and C#. I successfully tested in the sandbox and got back a response that I can use. This was a C# “Console Application” as per the example.

But, and here is my problem, I am trying to implement exactly the same code into an existing

ASP.NET Web Application (.NET Framework)

project.

I’ve added using NuGet the required ‘Square’ and ‘Microsoft.Extensions.Configuration’ and ‘Microsoft.Extensions.Configuration.JSON’ SDK’s.

I’ve made a namespace that has the same code as the Program.cs file.
I’ve made a Class1.cs file that is the same as the one I wrote in my Console application.

I also had to update my project to .NETFramework 4.7.2.
Plus I added a reference to .netstandard.dll v2.0 (as this was missing). I used the one from my Console App (this could be relevant to my issue?)

Now all is looking nearly there, but I have three errors when I build my Project. A far bit of Goggling makes me think that the Square SDK and some other dll (reference) are clashing, but I cannot work out which files and if so what versions to use.

Ideas are most welcome, please.

Issue 1:
var result = await client.CheckoutApi.CreatePaymentLinkAsync(body: body);

Gives the error = “Reference to type ‘Task<>’ claims it is defined in ‘System.Runtime’, but it could not be found”. Is there some clash with my version of System.Threading.Tasks?

Issue 2:

catch (ApiException e)
            {
                var errors = e.Errors;

Gives the error in relation to the keyword ‘Errors’ = “The type ‘List<>’ is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Collections, Version=4.1.2.0,” but the reference does exist…

Issue 3:
foreach (var item in httpContext.Request.Headers) {}

Gives the error in relation to the keyword ‘Headers’ = “The type ‘Dictionary<,>’ is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Collections, Version=4.1.2.0” but once again the reference does exist…

I already make use of Task awaiting, and LIST<> in this project before adding the Square SDK + Updates, and they did work.

Thanks.

:wave: What are the errors that you’re getting from Square? Also what’s your application Id? :slightly_smiling_face:

Hi Bryan, I don’t get as far as talking to the square server. My issue is inside VS2019, it looks like the Square SDK and some Microsoft Package/components are in conflict with each other.

Sorry, what do you mean by Application ID (where is this from)?

cheers

Do you have any more code we can look at? Or can you make a very small repro you can upload?
Also you said you updated to .NET Framework 4.7.2. What were you on before? :slightly_smiling_face:

Hi, I am going to try starting from a new WEB Application project and see where that gets me, and I will post the code for that (plus list dependants, etc.). Unfortunately can’t do that till tomorrow (but appreciate your quick reply).

I was on .NET Framework 4.6.x.

Sounds good! We’ll wait for the results. :slightly_smiling_face:

Hi, so the situation is no clearer to being sorted.

Following Quickstart exercise:

(Option 2) Square .NET SDK Quickstart (Windows platform)

I started a new VS2019 Project (Microsoft Visual Studio Professional 2019, Version 16.11.16. .NET Framework Version 4.8.04084). This is a Licensed version and I have no option to use a newer version of VS.

So, I choose to make a new project:

ASP.NET Web Application (.NET Framework) C#

In the default project I then performed the following:

  • Updated AspNet.ScriptManager.bootstrap to v4.6.0
  • Updated AspNet.ScriptManager.jQuery to v3.6.0
  • Updated Newtonsoft.Json to v13.0.1
  • Updated Microsoft.Web.Infrastructure to V2.0.0

Added NuGet packages

  • Square
  • Microsoft.Extensions.Configuration.Json
  • Microsoft.Extensions.Configuration

Then,

  • Added appsettings.json file.
  • Updated it with my access token.
  • Changed properties to “Copy If Newer”.

Then I added a Program.cs file inside App_Code (which I had just added to my project).
Inside this Program.cs file I copied the sample code and saved it.

I then built the Project and it has 10 Errors, which all relate to Program.cs. Each one is saying

Error	CS0012	The type 'xxxxxxx' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0,

where xxxxx is either Object, ENUM, Task<>, IList<>, Exception, Dictionary<,>.

I have tried to search for netstandard in NuGet (and installed NETStandard.Library V2.0 as this was the only option that came up).

I have also added a reference to the “netstandard.dll” attached to the .NET Core project that worked with your samples C# Console Application template.

But when I then test out my Project (Start Debugging) by running the debugger on the default projects Contact.aspx page, I get the usual compilation error screen. See below:

I’m not 100% saying the issue is with Square’s code but might be a consequence of some class / namespace your using that I don’t seem to be able to find / access.

I’m new to using Git Hub but have created a public repo of my Project here

You will need to add your own Access Token I guess.

Hope that helps.

Cheers.

Hi, i’m still no nearer to getting CheckoutAPI to work in a WebApplication…

To check if it was an issue with my Work Laptop, I created a new project on my personal PC running VS2019 community / C#.

After adding the NuGet packages and a few extra lines to my Web.Config, which I’ve found are needed to get rid of multiple missing references:

<compilation debug="true" targetFramework="4.7.2">
      <assemblies>
        <!-- This sorted one problem with Square not working -->
        <add assembly="netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" />
      </assemblies>
    </compilation>

and also

<system.codedom>
    <compilers>
      <!-- Needed to make force it to use version 3.6.0.0 to make some library's work -->
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=3.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
    </compilers>
  </system.codedom>

Plus I put the CheckoutAPI code into a Class.cs file which I reference on a Button Click.

Now I have code that builds ok, but when it runs, it gets to the following line:

var result = await client.CheckoutApi.CreatePaymentLinkAsync(body: body);

and then gives the following error

System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=Square_WebApplication2
StackTrace:
at Square_WebApplication2.CheckoutAPI.d__5.MoveNext() in C:\Users\djmre\source\repos\Square_WebApplication2\App_Start\CodeFile1.cs:line 101

I’ve checked and Body has this stored in it, so is not empty

{CreatePaymentLinkRequest : (this.IdempotencyKey = d9391d0a-f71d-4beb-9886-6483bea3ed4e, this.Description = null, this.QuickPay = QuickPay : (this.Name = Auto Detailing, this.PriceMoney = Money : (this.Amount = 12500, this.Currency = GBP), this.LocationId = LXNPQGX0RXNMV), this.Order = null, this.CheckoutOptions = null, this.PrePopulatedData = null, this.Source = null, this.PaymentNote = null)}

So, once again I’m stuck, so any help is really appreciated. Cannot see why it reports as Null.Exception error. Thanks

Hi, my entire solution is now broken, after installing the Square SDK, despite installing ‘System.Collections’ I cannot get past this error:-

System.BadImageFormatException: Cannot load a reference assembly for execution.

[BadImageFormatException: Could not load file or assembly ‘System.Collections’ or one of its dependencies.

I’ve tried all the suggestions on StackOverflow and Google…

We checked everything and the team didn’t find actionable things on our side, it’s very likely related to the set up of the solution. The team has two suggestions:

  1. Try .NET 6 instead of .NET 4.6. It seems you are trying to mix with .NET standard. They suggest that you create a clean project and try to import the Square SDK.
  2. Set up a minimal repro and share it with us. The repo you shared isn’t visible.
    :slightly_smiling_face:

Hi Bryan, ok will do. Thanks.

The repro had information in it I shouldn’t have shared, so made it private.

Hi @Bryan-Square, I have added to my Repro two applications.
One is a Console App (this works), and One is a Web App (this does not work). Both of them are the default application created in VS2019, with the Square SDK added (via NuGet), and have the same code in them for performing a CheckOutAPI call.

In the Console App, I created a Class1.cs file which has the code for the Payment Task in it.

The operation of it is as follows:

In Program.cs the Main function sets up the environment and access token, then calls Class1.cs to create the payment and submit it to the CheckOutAPI “CreatePaymentLinkAsync”. I made an improvement to the sample code so I can extract the required URL, otherwise it is as per the example.

CreatePaymentLinkResponse result = await client.CheckoutApi.CreatePaymentLinkAsync(body: body);
resultToReturn = result.PaymentLink.Url;

Then I created an ASP.Net WebApplication, which targets .NET Framework 4.7.2, took the default project, and added the same Class1.cs file to the project (with a minor tweak to make it an internal class as required). Note, I copy/pasted the same code into a newly created file, I did not just add the file from the other project.

I also had to add the following line to the Web.config file.

<add assembly="netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51"/>

Then I added a new WebForm called “SquareWebForm1.aspx” and in the code behind file added a new Task function that calls the Class1.cs code and should create the payment and submit it to the CheckOutAPI “CreatePaymentLinkAsync” (as it does in the Console App). Page_Load has a Wait call added to it to call the new Task on page load.

To call this new WebForm, in the default project, I added a new href link in the Site.master document. When clicked on goes to this new “SquareWebForm1.aspx” webform.

If you debug through the code when it’s running, it all seems to work till it gets to the submit line

CreatePaymentLinkResponse result = await client.CheckoutApi.CreatePaymentLinkAsync(body: body);

Where it just stops running the project (and the Output window shows the Thread exited with code = 0). Other than this Thread exiting error, it just does nothing more.

Why does it work in a Console application but not in a WebForms application, does your SDK work with .NET Frameworks? If so could you modify my Webform application please to demonstrate this.

Cheers

Repro’s are here →

The Access Token will need updating to your own.

Cheers

For anyone finding this thread down the line, the solution in this thread resolves the issue- .NET SDK v25.2.0 and 26.0.0 freeze/lock IIS/VS - #21 by Jordan-Square

1 Like