Time out while using the DotNet SDK

I followed the example on the Video introducing DotNet API. I am familiar with API,S and dot net for years.

I wrote the following code and it loops forever on the last line. Can you help ? Give me a hint ?

Dim mySquareClient As SquareClient = New SquareClient.Builder().Environment(Square.Environment.Production).AccessToken(myAccessToken).Build()
Dim myLocationApi = mySquareClient.LocationsApi (Until here, it works)
Dim myLocations = myLocationApi.ListLocations().Locations (when I reach this line. Loop forever…)

I see that my request is logged on Square Api Log Page and that it generates a correct response.

Help or hints would be really appreciated.

Is that the entire snippet of code that your using? Also since your using the SDKs have you looked at using our API Explorer to build snippets of code to call our APIs?

try
{
  var result = await client.LocationsApi.ListLocationsAsync();
}
catch (ApiException e)
{
  Console.WriteLine("Failed to make the request");
  Console.WriteLine($"Response Code: {e.ResponseCode}");
  Console.WriteLine($"Exception: {e.Message}");
}

:slightly_smiling_face:

Thank you for your reply.

Yes, I went to the API explorer. When I make my requests, they go through your server and the API log is filled up and returns a 200 status level. The Response contains the expected info in the log.

I tested different API’s and they all work according to the log. Was even able to launch the terminal API and make a payment that is confirmed in my Dashboard and in the API log.

But the thing is I stay “stucked” on the request line in my code, awaiting the response.

I put a Try Except and no error is raised up.

I installed all the recommended Microsoft nuget packages.

No error in scripts with debugger on Firefox or Chrome.

Reading the response is my only missing part to celebrate Easter :astonished:)

Hmm, that is odd. What’s the snippet of code your using to call the API? :slightly_smiling_face:

I do not use Snippets. I was only using the SDK like I showed you.

Isnt’it supposed to work ?
Do I really need to go Through Snippets ?

In my Web Projects, I use many other API’s like Paypal or Paysafe and they al work without snippets…

I also tested the Square .Net Api with a Windows App, and same behavior.

On the same machine, if I use the powershell with this code, it works all through. So this is not browser or firewall related or something like this…

My Code to drive the Terminal I just Bought :

Set-Variable -Name “ACCESS_TOKEN” -Value “MY TOKEN”
Set-Variable -Name “DEVICE_ID” -Value “232CS145B2002854”
Set-Variable -Name “UUID_var” -Value “9a19f245-d225-4a54-9cb4-f48c7e63e58b”

curl https://connect.squareup.com/v2/terminals/checkouts -X POST -H ‘Square-Version: 2023-03-15’ -H “Authorization: Bearer ${ACCESS_TOKEN}” -H ‘Content-Type: application/json’ -d “{‘checkout’: {‘amount_money’ : {‘amount’ : 100, ‘currency’: ‘CAD’}, ‘device_options’: {‘device_id’: ‘${DEVICE_ID}’}}, ‘idempotency_key’: ‘${UUID_var}’}”

No, you don’t need snippets. The snippet that I’m referring to is the the code that your running in that’s causing the duplicates. For example this is what I’m running:

using System;
using Square;
using Square.Models;
using Square.Exceptions;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;

namespace SquareListLocations
{
    public class Program
    {
        private static ISquareClient client;
        private static IConfigurationRoot config;

        static void Main(string[] args)
        {
var builder = new ConfigurationBuilder()
 .AddJsonFile($"/Users/SDK/dot-net/SDK-update/SquareListLocations/appsettings.json", true, true);

config = builder.Build();
var accessToken = config["AppSettings:AccessToken"];
client = new SquareClient.Builder()
    .Environment(Square.Environment.Sandbox)
    .AccessToken(accessToken)
    .Build();

RetrieveLocationsAsync().Wait();
        }

        static async Task RetrieveLocationsAsync()
        {
try
{
    ListLocationsResponse response;
    response = await client.LocationsApi.ListLocationsAsync();
    foreach (Location location in response.Locations)
    {
        Console.WriteLine("location:\n  name = {1}",
location.Country, location.Name);
    }
}
catch (ApiException e)
{
    var errors = e.Errors;
    var statusCode = e.ResponseCode;
    var httpContext = e.HttpContext;
    Console.WriteLine("ApiException occurred:");
    Console.WriteLine("Headers:");
    foreach (var item in httpContext.Request.Headers)
    {
        //Display all the headers except Authorization
        if (item.Key != "Authorization")
        {
Console.WriteLine("\t{0}:", item.Key, item.Value);
        }
    }
    Console.WriteLine("Status Code: \t{0}", statusCode);
    foreach (Error error in errors)
    {
        Console.WriteLine("Error Category:{0} Code:{1} Detail:{2}", error.Category, error.Code, error.Detail);
    }

    // Your error handling code
}
catch (Exception e)
{
    Console.WriteLine("Exception occurred");
    // Your error handling code
}
        }
    }
}

Same behavior.

Code stucked on this line :
response = await client.LocationsApi.ListLocationsAsync();

No error, nothing goes to catch…

A wiz friend of mine tried the API on his machine and same behavior.

He is on it. We will see.

Interesting, if you walk through the code using a debugger, are their any low-level stuff things causing the issue? :slightly_smiling_face:

Hello Bryan,

We went through it. Here is my working Sub()

Protected Async Sub BtnTestClick()
Dim myAccessToken As String = “MY_TOKEN”

    Dim mySquareClient As SquareClient = New SquareClient.Builder().Environment(Square.Environment.Production).AccessToken(myAccessToken).Build()

    Try
        lblInfo.Text = "Calling..."
        Dim myLocation = Await mySquareClient.LocationsApi.RetrieveLocationAsync("JVW3XXCXD6R3M")
        lblInfo.Text = myLocation.Location.BusinessName
    Catch ex As ApiException
        lblInfo.Text = ex.ResponseCode.ToString + " - " + ex.Message
    End Try

End Sub

But according to the documentation and the videos on your site, the API should word in “non async” mode too. It does not.

We gave up and will use the async mode.

Hello Bryan.

I did some tests and with version 16 of Square SDK for .Net, I can use LocationApi.RetrieveLocation in real time (not async).

With latest release, I cannot. It freezes.

Same code. Very simple and basic code.

So maybe your team should look at it.

Private Sub RadButton4_Click(sender As Object, e As EventArgs) Handles RadButton4.Click
    Dim myAccessToken As String = "MY_TOKEN"

    Dim mySquareClient As SquareClient = New SquareClient.Builder().Environment(Square.Environment.Production).AccessToken(myAccessToken).Build()
    Dim myLocationApi = mySquareClient.LocationsApi

    Try
        Label1.Text = "Calling..."
        Dim myLocation = myLocationApi.RetrieveLocation("JVW3XXCXD6R3M")
        Label1.Text = myLocation.Location.BusinessName

    Catch ex As ApiException

        Label1.Text = ex.ResponseCode.ToString + " - " + ex.Message

    End Try
End Sub

The team was able to get it to work with the latest version of the SDK with a few changes.

Module Program
    Sub Main(args As String())

        Dim myAccessToken As String = "YOUR_ACCESS_TOKEN"
        Dim mySquareClient As SquareClient = New SquareClient.Builder().Environment(Square.Environment.Sandbox).AccessToken(myAccessToken).Build()
        Console.WriteLine("Square SDK version: " + mySquareClient.SdkVersion)
        Dim myLocationApi = mySquareClient.LocationsApi

        Try
            Console.WriteLine("Calling...")
            Dim myLocation = myLocationApi.RetrieveLocation("LOCATION_ID")
            Console.WriteLine(myLocation.Location.BusinessName)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try

    End Sub
End Module

:slightly_smiling_face:

Do you mean a few changes on your side to the API ?

Because the code seems the same to me. And it sill hooks on my side. What are the “few changes” ?

What version number of the API should I expect or use

We were able to get it to work on 25 as expected. Is there anything different about your environment so we can try and replicate this? :slightly_smiling_face:

I have 25.2. I do not know what in my environment could make this behavior.

You test under Visual studio ? Version Version 17.0.4 64 bit I have.

Only thing I can say is that in the same environment, version 16 of the API works without Async and that version 25.2 was not working also at my friend’s place.

So to me, the answer is in the API version.

I sent much time around that. And finally use only Async, which s not my first choice for everything, but at least, I can go forward in my project.

Thanks I’ll share this with the team. :slightly_smiling_face: